]> git.proxmox.com Git - qemu.git/blob - audio/audio.c
Add missing [SU]32
[qemu.git] / audio / audio.c
1 /*
2 * QEMU Audio subsystem
3 *
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include "hw/hw.h"
25 #include "audio.h"
26 #include "console.h"
27 #include "qemu-timer.h"
28 #include "sysemu.h"
29
30 #define AUDIO_CAP "audio"
31 #include "audio_int.h"
32
33 /* #define DEBUG_PLIVE */
34 /* #define DEBUG_LIVE */
35 /* #define DEBUG_OUT */
36 /* #define DEBUG_CAPTURE */
37
38 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
39
40 static struct audio_driver *drvtab[] = {
41 #ifdef CONFIG_OSS
42 &oss_audio_driver,
43 #endif
44 #ifdef CONFIG_ALSA
45 &alsa_audio_driver,
46 #endif
47 #ifdef CONFIG_COREAUDIO
48 &coreaudio_audio_driver,
49 #endif
50 #ifdef CONFIG_DSOUND
51 &dsound_audio_driver,
52 #endif
53 #ifdef CONFIG_FMOD
54 &fmod_audio_driver,
55 #endif
56 #ifdef CONFIG_SDL
57 &sdl_audio_driver,
58 #endif
59 #ifdef CONFIG_ESD
60 &esd_audio_driver,
61 #endif
62 &no_audio_driver,
63 &wav_audio_driver
64 };
65
66 struct fixed_settings {
67 int enabled;
68 int nb_voices;
69 int greedy;
70 audsettings_t settings;
71 };
72
73 static struct {
74 struct fixed_settings fixed_out;
75 struct fixed_settings fixed_in;
76 union {
77 int hz;
78 int64_t ticks;
79 } period;
80 int plive;
81 int log_to_monitor;
82 } conf = {
83 { /* DAC fixed settings */
84 1, /* enabled */
85 1, /* nb_voices */
86 1, /* greedy */
87 {
88 44100, /* freq */
89 2, /* nchannels */
90 AUD_FMT_S16, /* fmt */
91 AUDIO_HOST_ENDIANNESS
92 }
93 },
94
95 { /* ADC fixed settings */
96 1, /* enabled */
97 1, /* nb_voices */
98 1, /* greedy */
99 {
100 44100, /* freq */
101 2, /* nchannels */
102 AUD_FMT_S16, /* fmt */
103 AUDIO_HOST_ENDIANNESS
104 }
105 },
106
107 { 0 }, /* period */
108 0, /* plive */
109 0 /* log_to_monitor */
110 };
111
112 static AudioState glob_audio_state;
113
114 volume_t nominal_volume = {
115 0,
116 #ifdef FLOAT_MIXENG
117 1.0,
118 1.0
119 #else
120 1ULL << 32,
121 1ULL << 32
122 #endif
123 };
124
125 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
126 /* http://www.multi-platforms.com/Tips/PopCount.htm */
127 uint32_t popcount (uint32_t u)
128 {
129 u = ((u&0x55555555) + ((u>>1)&0x55555555));
130 u = ((u&0x33333333) + ((u>>2)&0x33333333));
131 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
132 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
133 u = ( u&0x0000ffff) + (u>>16);
134 return u;
135 }
136
137 inline uint32_t lsbindex (uint32_t u)
138 {
139 return popcount ((u&-u)-1);
140 }
141
142 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
143 #error No its not
144 #else
145 int audio_bug (const char *funcname, int cond)
146 {
147 if (cond) {
148 static int shown;
149
150 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
151 if (!shown) {
152 shown = 1;
153 AUD_log (NULL, "Save all your work and restart without audio\n");
154 AUD_log (NULL, "Please send bug report to malc@pulsesoft.com\n");
155 AUD_log (NULL, "I am sorry\n");
156 }
157 AUD_log (NULL, "Context:\n");
158
159 #if defined AUDIO_BREAKPOINT_ON_BUG
160 # if defined HOST_I386
161 # if defined __GNUC__
162 __asm__ ("int3");
163 # elif defined _MSC_VER
164 _asm _emit 0xcc;
165 # else
166 abort ();
167 # endif
168 # else
169 abort ();
170 # endif
171 #endif
172 }
173
174 return cond;
175 }
176 #endif
177
178 static inline int audio_bits_to_index (int bits)
179 {
180 switch (bits) {
181 case 8:
182 return 0;
183
184 case 16:
185 return 1;
186
187 case 32:
188 return 2;
189
190 default:
191 audio_bug ("bits_to_index", 1);
192 AUD_log (NULL, "invalid bits %d\n", bits);
193 return 0;
194 }
195 }
196
197 void *audio_calloc (const char *funcname, int nmemb, size_t size)
198 {
199 int cond;
200 size_t len;
201
202 len = nmemb * size;
203 cond = !nmemb || !size;
204 cond |= nmemb < 0;
205 cond |= len < size;
206
207 if (audio_bug ("audio_calloc", cond)) {
208 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
209 funcname);
210 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
211 return NULL;
212 }
213
214 return qemu_mallocz (len);
215 }
216
217 static char *audio_alloc_prefix (const char *s)
218 {
219 const char qemu_prefix[] = "QEMU_";
220 size_t len;
221 char *r;
222
223 if (!s) {
224 return NULL;
225 }
226
227 len = strlen (s);
228 r = qemu_malloc (len + sizeof (qemu_prefix));
229
230 if (r) {
231 size_t i;
232 char *u = r + sizeof (qemu_prefix) - 1;
233
234 strcpy (r, qemu_prefix);
235 strcat (r, s);
236
237 for (i = 0; i < len; ++i) {
238 u[i] = toupper (u[i]);
239 }
240 }
241 return r;
242 }
243
244 static const char *audio_audfmt_to_string (audfmt_e fmt)
245 {
246 switch (fmt) {
247 case AUD_FMT_U8:
248 return "U8";
249
250 case AUD_FMT_U16:
251 return "U16";
252
253 case AUD_FMT_S8:
254 return "S8";
255
256 case AUD_FMT_S16:
257 return "S16";
258
259 case AUD_FMT_U32:
260 return "U32";
261
262 case AUD_FMT_S32:
263 return "S32";
264 }
265
266 dolog ("Bogus audfmt %d returning S16\n", fmt);
267 return "S16";
268 }
269
270 static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
271 int *defaultp)
272 {
273 if (!strcasecmp (s, "u8")) {
274 *defaultp = 0;
275 return AUD_FMT_U8;
276 }
277 else if (!strcasecmp (s, "u16")) {
278 *defaultp = 0;
279 return AUD_FMT_U16;
280 }
281 else if (!strcasecmp (s, "u32")) {
282 *defaultp = 0;
283 return AUD_FMT_U32;
284 }
285 else if (!strcasecmp (s, "s8")) {
286 *defaultp = 0;
287 return AUD_FMT_S8;
288 }
289 else if (!strcasecmp (s, "s16")) {
290 *defaultp = 0;
291 return AUD_FMT_S16;
292 }
293 else if (!strcasecmp (s, "s32")) {
294 *defaultp = 0;
295 return AUD_FMT_S32;
296 }
297 else {
298 dolog ("Bogus audio format `%s' using %s\n",
299 s, audio_audfmt_to_string (defval));
300 *defaultp = 1;
301 return defval;
302 }
303 }
304
305 static audfmt_e audio_get_conf_fmt (const char *envname,
306 audfmt_e defval,
307 int *defaultp)
308 {
309 const char *var = getenv (envname);
310 if (!var) {
311 *defaultp = 1;
312 return defval;
313 }
314 return audio_string_to_audfmt (var, defval, defaultp);
315 }
316
317 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
318 {
319 int val;
320 char *strval;
321
322 strval = getenv (key);
323 if (strval) {
324 *defaultp = 0;
325 val = atoi (strval);
326 return val;
327 }
328 else {
329 *defaultp = 1;
330 return defval;
331 }
332 }
333
334 static const char *audio_get_conf_str (const char *key,
335 const char *defval,
336 int *defaultp)
337 {
338 const char *val = getenv (key);
339 if (!val) {
340 *defaultp = 1;
341 return defval;
342 }
343 else {
344 *defaultp = 0;
345 return val;
346 }
347 }
348
349 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
350 {
351 if (conf.log_to_monitor) {
352 if (cap) {
353 term_printf ("%s: ", cap);
354 }
355
356 term_vprintf (fmt, ap);
357 }
358 else {
359 if (cap) {
360 fprintf (stderr, "%s: ", cap);
361 }
362
363 vfprintf (stderr, fmt, ap);
364 }
365 }
366
367 void AUD_log (const char *cap, const char *fmt, ...)
368 {
369 va_list ap;
370
371 va_start (ap, fmt);
372 AUD_vlog (cap, fmt, ap);
373 va_end (ap);
374 }
375
376 static void audio_print_options (const char *prefix,
377 struct audio_option *opt)
378 {
379 char *uprefix;
380
381 if (!prefix) {
382 dolog ("No prefix specified\n");
383 return;
384 }
385
386 if (!opt) {
387 dolog ("No options\n");
388 return;
389 }
390
391 uprefix = audio_alloc_prefix (prefix);
392
393 for (; opt->name; opt++) {
394 const char *state = "default";
395 printf (" %s_%s: ", uprefix, opt->name);
396
397 if (opt->overriddenp && *opt->overriddenp) {
398 state = "current";
399 }
400
401 switch (opt->tag) {
402 case AUD_OPT_BOOL:
403 {
404 int *intp = opt->valp;
405 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
406 }
407 break;
408
409 case AUD_OPT_INT:
410 {
411 int *intp = opt->valp;
412 printf ("integer, %s = %d\n", state, *intp);
413 }
414 break;
415
416 case AUD_OPT_FMT:
417 {
418 audfmt_e *fmtp = opt->valp;
419 printf (
420 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
421 state,
422 audio_audfmt_to_string (*fmtp)
423 );
424 }
425 break;
426
427 case AUD_OPT_STR:
428 {
429 const char **strp = opt->valp;
430 printf ("string, %s = %s\n",
431 state,
432 *strp ? *strp : "(not set)");
433 }
434 break;
435
436 default:
437 printf ("???\n");
438 dolog ("Bad value tag for option %s_%s %d\n",
439 uprefix, opt->name, opt->tag);
440 break;
441 }
442 printf (" %s\n", opt->descr);
443 }
444
445 qemu_free (uprefix);
446 }
447
448 static void audio_process_options (const char *prefix,
449 struct audio_option *opt)
450 {
451 char *optname;
452 const char qemu_prefix[] = "QEMU_";
453 size_t preflen;
454
455 if (audio_bug (AUDIO_FUNC, !prefix)) {
456 dolog ("prefix = NULL\n");
457 return;
458 }
459
460 if (audio_bug (AUDIO_FUNC, !opt)) {
461 dolog ("opt = NULL\n");
462 return;
463 }
464
465 preflen = strlen (prefix);
466
467 for (; opt->name; opt++) {
468 size_t len, i;
469 int def;
470
471 if (!opt->valp) {
472 dolog ("Option value pointer for `%s' is not set\n",
473 opt->name);
474 continue;
475 }
476
477 len = strlen (opt->name);
478 /* len of opt->name + len of prefix + size of qemu_prefix
479 * (includes trailing zero) + zero + underscore (on behalf of
480 * sizeof) */
481 optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1);
482 if (!optname) {
483 dolog ("Could not allocate memory for option name `%s'\n",
484 opt->name);
485 continue;
486 }
487
488 strcpy (optname, qemu_prefix);
489
490 /* copy while upper-casing, including trailing zero */
491 for (i = 0; i <= preflen; ++i) {
492 optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]);
493 }
494 strcat (optname, "_");
495 strcat (optname, opt->name);
496
497 def = 1;
498 switch (opt->tag) {
499 case AUD_OPT_BOOL:
500 case AUD_OPT_INT:
501 {
502 int *intp = opt->valp;
503 *intp = audio_get_conf_int (optname, *intp, &def);
504 }
505 break;
506
507 case AUD_OPT_FMT:
508 {
509 audfmt_e *fmtp = opt->valp;
510 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
511 }
512 break;
513
514 case AUD_OPT_STR:
515 {
516 const char **strp = opt->valp;
517 *strp = audio_get_conf_str (optname, *strp, &def);
518 }
519 break;
520
521 default:
522 dolog ("Bad value tag for option `%s' - %d\n",
523 optname, opt->tag);
524 break;
525 }
526
527 if (!opt->overriddenp) {
528 opt->overriddenp = &opt->overridden;
529 }
530 *opt->overriddenp = !def;
531 qemu_free (optname);
532 }
533 }
534
535 static void audio_print_settings (audsettings_t *as)
536 {
537 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
538
539 switch (as->fmt) {
540 case AUD_FMT_S8:
541 AUD_log (NULL, "S8");
542 break;
543 case AUD_FMT_U8:
544 AUD_log (NULL, "U8");
545 break;
546 case AUD_FMT_S16:
547 AUD_log (NULL, "S16");
548 break;
549 case AUD_FMT_U16:
550 AUD_log (NULL, "U16");
551 break;
552 case AUD_FMT_S32:
553 AUD_log (NULL, "S32");
554 break;
555 case AUD_FMT_U32:
556 AUD_log (NULL, "U32");
557 break;
558 default:
559 AUD_log (NULL, "invalid(%d)", as->fmt);
560 break;
561 }
562
563 AUD_log (NULL, " endianness=");
564 switch (as->endianness) {
565 case 0:
566 AUD_log (NULL, "little");
567 break;
568 case 1:
569 AUD_log (NULL, "big");
570 break;
571 default:
572 AUD_log (NULL, "invalid");
573 break;
574 }
575 AUD_log (NULL, "\n");
576 }
577
578 static int audio_validate_settings (audsettings_t *as)
579 {
580 int invalid;
581
582 invalid = as->nchannels != 1 && as->nchannels != 2;
583 invalid |= as->endianness != 0 && as->endianness != 1;
584
585 switch (as->fmt) {
586 case AUD_FMT_S8:
587 case AUD_FMT_U8:
588 case AUD_FMT_S16:
589 case AUD_FMT_U16:
590 case AUD_FMT_S32:
591 case AUD_FMT_U32:
592 break;
593 default:
594 invalid = 1;
595 break;
596 }
597
598 invalid |= as->freq <= 0;
599 return invalid ? -1 : 0;
600 }
601
602 static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
603 {
604 int bits = 8, sign = 0;
605
606 switch (as->fmt) {
607 case AUD_FMT_S8:
608 sign = 1;
609 case AUD_FMT_U8:
610 break;
611
612 case AUD_FMT_S16:
613 sign = 1;
614 case AUD_FMT_U16:
615 bits = 16;
616 break;
617
618 case AUD_FMT_S32:
619 sign = 1;
620 case AUD_FMT_U32:
621 bits = 32;
622 break;
623 }
624 return info->freq == as->freq
625 && info->nchannels == as->nchannels
626 && info->sign == sign
627 && info->bits == bits
628 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
629 }
630
631 void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as)
632 {
633 int bits = 8, sign = 0, shift = 0;
634
635 switch (as->fmt) {
636 case AUD_FMT_S8:
637 sign = 1;
638 case AUD_FMT_U8:
639 break;
640
641 case AUD_FMT_S16:
642 sign = 1;
643 case AUD_FMT_U16:
644 bits = 16;
645 shift = 1;
646 break;
647
648 case AUD_FMT_S32:
649 sign = 1;
650 case AUD_FMT_U32:
651 bits = 32;
652 shift = 2;
653 break;
654 }
655
656 info->freq = as->freq;
657 info->bits = bits;
658 info->sign = sign;
659 info->nchannels = as->nchannels;
660 info->shift = (as->nchannels == 2) + shift;
661 info->align = (1 << info->shift) - 1;
662 info->bytes_per_second = info->freq << info->shift;
663 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
664 }
665
666 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
667 {
668 if (!len) {
669 return;
670 }
671
672 if (info->sign) {
673 memset (buf, 0x00, len << info->shift);
674 }
675 else {
676 switch (info->bits) {
677 case 8:
678 memset (buf, 0x80, len << info->shift);
679 break;
680
681 case 16:
682 {
683 int i;
684 uint16_t *p = buf;
685 int shift = info->nchannels - 1;
686 short s = INT16_MAX;
687
688 if (info->swap_endianness) {
689 s = bswap16 (s);
690 }
691
692 for (i = 0; i < len << shift; i++) {
693 p[i] = s;
694 }
695 }
696 break;
697
698 case 32:
699 {
700 int i;
701 uint32_t *p = buf;
702 int shift = info->nchannels - 1;
703 int32_t s = INT32_MAX;
704
705 if (info->swap_endianness) {
706 s = bswap32 (s);
707 }
708
709 for (i = 0; i < len << shift; i++) {
710 p[i] = s;
711 }
712 }
713 break;
714
715 default:
716 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
717 info->bits);
718 break;
719 }
720 }
721 }
722
723 /*
724 * Capture
725 */
726 static void noop_conv (st_sample_t *dst, const void *src,
727 int samples, volume_t *vol)
728 {
729 (void) src;
730 (void) dst;
731 (void) samples;
732 (void) vol;
733 }
734
735 static CaptureVoiceOut *audio_pcm_capture_find_specific (
736 AudioState *s,
737 audsettings_t *as
738 )
739 {
740 CaptureVoiceOut *cap;
741
742 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
743 if (audio_pcm_info_eq (&cap->hw.info, as)) {
744 return cap;
745 }
746 }
747 return NULL;
748 }
749
750 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
751 {
752 struct capture_callback *cb;
753
754 #ifdef DEBUG_CAPTURE
755 dolog ("notification %d sent\n", cmd);
756 #endif
757 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
758 cb->ops.notify (cb->opaque, cmd);
759 }
760 }
761
762 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
763 {
764 if (cap->hw.enabled != enabled) {
765 audcnotification_e cmd;
766 cap->hw.enabled = enabled;
767 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
768 audio_notify_capture (cap, cmd);
769 }
770 }
771
772 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
773 {
774 HWVoiceOut *hw = &cap->hw;
775 SWVoiceOut *sw;
776 int enabled = 0;
777
778 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
779 if (sw->active) {
780 enabled = 1;
781 break;
782 }
783 }
784 audio_capture_maybe_changed (cap, enabled);
785 }
786
787 static void audio_detach_capture (HWVoiceOut *hw)
788 {
789 SWVoiceCap *sc = hw->cap_head.lh_first;
790
791 while (sc) {
792 SWVoiceCap *sc1 = sc->entries.le_next;
793 SWVoiceOut *sw = &sc->sw;
794 CaptureVoiceOut *cap = sc->cap;
795 int was_active = sw->active;
796
797 if (sw->rate) {
798 st_rate_stop (sw->rate);
799 sw->rate = NULL;
800 }
801
802 LIST_REMOVE (sw, entries);
803 LIST_REMOVE (sc, entries);
804 qemu_free (sc);
805 if (was_active) {
806 /* We have removed soft voice from the capture:
807 this might have changed the overall status of the capture
808 since this might have been the only active voice */
809 audio_recalc_and_notify_capture (cap);
810 }
811 sc = sc1;
812 }
813 }
814
815 static int audio_attach_capture (AudioState *s, HWVoiceOut *hw)
816 {
817 CaptureVoiceOut *cap;
818
819 audio_detach_capture (hw);
820 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
821 SWVoiceCap *sc;
822 SWVoiceOut *sw;
823 HWVoiceOut *hw_cap = &cap->hw;
824
825 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
826 if (!sc) {
827 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
828 sizeof (*sc));
829 return -1;
830 }
831
832 sc->cap = cap;
833 sw = &sc->sw;
834 sw->hw = hw_cap;
835 sw->info = hw->info;
836 sw->empty = 1;
837 sw->active = hw->enabled;
838 sw->conv = noop_conv;
839 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
840 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
841 if (!sw->rate) {
842 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
843 qemu_free (sw);
844 return -1;
845 }
846 LIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
847 LIST_INSERT_HEAD (&hw->cap_head, sc, entries);
848 #ifdef DEBUG_CAPTURE
849 asprintf (&sw->name, "for %p %d,%d,%d",
850 hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
851 dolog ("Added %s active = %d\n", sw->name, sw->active);
852 #endif
853 if (sw->active) {
854 audio_capture_maybe_changed (cap, 1);
855 }
856 }
857 return 0;
858 }
859
860 /*
861 * Hard voice (capture)
862 */
863 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
864 {
865 SWVoiceIn *sw;
866 int m = hw->total_samples_captured;
867
868 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
869 if (sw->active) {
870 m = audio_MIN (m, sw->total_hw_samples_acquired);
871 }
872 }
873 return m;
874 }
875
876 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
877 {
878 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
879 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
880 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
881 return 0;
882 }
883 return live;
884 }
885
886 /*
887 * Soft voice (capture)
888 */
889 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
890 {
891 HWVoiceIn *hw = sw->hw;
892 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
893 int rpos;
894
895 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
896 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
897 return 0;
898 }
899
900 rpos = hw->wpos - live;
901 if (rpos >= 0) {
902 return rpos;
903 }
904 else {
905 return hw->samples + rpos;
906 }
907 }
908
909 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
910 {
911 HWVoiceIn *hw = sw->hw;
912 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
913 st_sample_t *src, *dst = sw->buf;
914
915 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
916
917 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
918 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
919 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
920 return 0;
921 }
922
923 samples = size >> sw->info.shift;
924 if (!live) {
925 return 0;
926 }
927
928 swlim = (live * sw->ratio) >> 32;
929 swlim = audio_MIN (swlim, samples);
930
931 while (swlim) {
932 src = hw->conv_buf + rpos;
933 isamp = hw->wpos - rpos;
934 /* XXX: <= ? */
935 if (isamp <= 0) {
936 isamp = hw->samples - rpos;
937 }
938
939 if (!isamp) {
940 break;
941 }
942 osamp = swlim;
943
944 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
945 dolog ("osamp=%d\n", osamp);
946 return 0;
947 }
948
949 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
950 swlim -= osamp;
951 rpos = (rpos + isamp) % hw->samples;
952 dst += osamp;
953 ret += osamp;
954 total += isamp;
955 }
956
957 sw->clip (buf, sw->buf, ret);
958 sw->total_hw_samples_acquired += total;
959 return ret << sw->info.shift;
960 }
961
962 /*
963 * Hard voice (playback)
964 */
965 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
966 {
967 SWVoiceOut *sw;
968 int m = INT_MAX;
969 int nb_live = 0;
970
971 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
972 if (sw->active || !sw->empty) {
973 m = audio_MIN (m, sw->total_hw_samples_mixed);
974 nb_live += 1;
975 }
976 }
977
978 *nb_livep = nb_live;
979 return m;
980 }
981
982 int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
983 {
984 int smin;
985
986 smin = audio_pcm_hw_find_min_out (hw, nb_live);
987
988 if (!*nb_live) {
989 return 0;
990 }
991 else {
992 int live = smin;
993
994 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
995 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
996 return 0;
997 }
998 return live;
999 }
1000 }
1001
1002 int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
1003 {
1004 int nb_live;
1005 int live;
1006
1007 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1008 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1009 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1010 return 0;
1011 }
1012 return live;
1013 }
1014
1015 /*
1016 * Soft voice (playback)
1017 */
1018 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1019 {
1020 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1021 int ret = 0, pos = 0, total = 0;
1022
1023 if (!sw) {
1024 return size;
1025 }
1026
1027 hwsamples = sw->hw->samples;
1028
1029 live = sw->total_hw_samples_mixed;
1030 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1031 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1032 return 0;
1033 }
1034
1035 if (live == hwsamples) {
1036 #ifdef DEBUG_OUT
1037 dolog ("%s is full %d\n", sw->name, live);
1038 #endif
1039 return 0;
1040 }
1041
1042 wpos = (sw->hw->rpos + live) % hwsamples;
1043 samples = size >> sw->info.shift;
1044
1045 dead = hwsamples - live;
1046 swlim = ((int64_t) dead << 32) / sw->ratio;
1047 swlim = audio_MIN (swlim, samples);
1048 if (swlim) {
1049 sw->conv (sw->buf, buf, swlim, &sw->vol);
1050 }
1051
1052 while (swlim) {
1053 dead = hwsamples - live;
1054 left = hwsamples - wpos;
1055 blck = audio_MIN (dead, left);
1056 if (!blck) {
1057 break;
1058 }
1059 isamp = swlim;
1060 osamp = blck;
1061 st_rate_flow_mix (
1062 sw->rate,
1063 sw->buf + pos,
1064 sw->hw->mix_buf + wpos,
1065 &isamp,
1066 &osamp
1067 );
1068 ret += isamp;
1069 swlim -= isamp;
1070 pos += isamp;
1071 live += osamp;
1072 wpos = (wpos + osamp) % hwsamples;
1073 total += osamp;
1074 }
1075
1076 sw->total_hw_samples_mixed += total;
1077 sw->empty = sw->total_hw_samples_mixed == 0;
1078
1079 #ifdef DEBUG_OUT
1080 dolog (
1081 "%s: write size %d ret %d total sw %d\n",
1082 SW_NAME (sw),
1083 size >> sw->info.shift,
1084 ret,
1085 sw->total_hw_samples_mixed
1086 );
1087 #endif
1088
1089 return ret << sw->info.shift;
1090 }
1091
1092 #ifdef DEBUG_AUDIO
1093 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1094 {
1095 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1096 cap, info->bits, info->sign, info->freq, info->nchannels);
1097 }
1098 #endif
1099
1100 #define DAC
1101 #include "audio_template.h"
1102 #undef DAC
1103 #include "audio_template.h"
1104
1105 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1106 {
1107 int bytes;
1108
1109 if (!sw) {
1110 /* XXX: Consider options */
1111 return size;
1112 }
1113
1114 if (!sw->hw->enabled) {
1115 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1116 return 0;
1117 }
1118
1119 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1120 return bytes;
1121 }
1122
1123 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1124 {
1125 int bytes;
1126
1127 if (!sw) {
1128 /* XXX: Consider options */
1129 return size;
1130 }
1131
1132 if (!sw->hw->enabled) {
1133 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1134 return 0;
1135 }
1136
1137 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1138 return bytes;
1139 }
1140
1141 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1142 {
1143 return sw->hw->samples << sw->hw->info.shift;
1144 }
1145
1146 void AUD_set_active_out (SWVoiceOut *sw, int on)
1147 {
1148 HWVoiceOut *hw;
1149
1150 if (!sw) {
1151 return;
1152 }
1153
1154 hw = sw->hw;
1155 if (sw->active != on) {
1156 SWVoiceOut *temp_sw;
1157 SWVoiceCap *sc;
1158
1159 if (on) {
1160 hw->pending_disable = 0;
1161 if (!hw->enabled) {
1162 hw->enabled = 1;
1163 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
1164 }
1165 }
1166 else {
1167 if (hw->enabled) {
1168 int nb_active = 0;
1169
1170 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1171 temp_sw = temp_sw->entries.le_next) {
1172 nb_active += temp_sw->active != 0;
1173 }
1174
1175 hw->pending_disable = nb_active == 1;
1176 }
1177 }
1178
1179 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1180 sc->sw.active = hw->enabled;
1181 if (hw->enabled) {
1182 audio_capture_maybe_changed (sc->cap, 1);
1183 }
1184 }
1185 sw->active = on;
1186 }
1187 }
1188
1189 void AUD_set_active_in (SWVoiceIn *sw, int on)
1190 {
1191 HWVoiceIn *hw;
1192
1193 if (!sw) {
1194 return;
1195 }
1196
1197 hw = sw->hw;
1198 if (sw->active != on) {
1199 SWVoiceIn *temp_sw;
1200
1201 if (on) {
1202 if (!hw->enabled) {
1203 hw->enabled = 1;
1204 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
1205 }
1206 sw->total_hw_samples_acquired = hw->total_samples_captured;
1207 }
1208 else {
1209 if (hw->enabled) {
1210 int nb_active = 0;
1211
1212 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1213 temp_sw = temp_sw->entries.le_next) {
1214 nb_active += temp_sw->active != 0;
1215 }
1216
1217 if (nb_active == 1) {
1218 hw->enabled = 0;
1219 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1220 }
1221 }
1222 }
1223 sw->active = on;
1224 }
1225 }
1226
1227 static int audio_get_avail (SWVoiceIn *sw)
1228 {
1229 int live;
1230
1231 if (!sw) {
1232 return 0;
1233 }
1234
1235 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1236 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1237 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1238 return 0;
1239 }
1240
1241 ldebug (
1242 "%s: get_avail live %d ret %" PRId64 "\n",
1243 SW_NAME (sw),
1244 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1245 );
1246
1247 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1248 }
1249
1250 static int audio_get_free (SWVoiceOut *sw)
1251 {
1252 int live, dead;
1253
1254 if (!sw) {
1255 return 0;
1256 }
1257
1258 live = sw->total_hw_samples_mixed;
1259
1260 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1261 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1262 return 0;
1263 }
1264
1265 dead = sw->hw->samples - live;
1266
1267 #ifdef DEBUG_OUT
1268 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1269 SW_NAME (sw),
1270 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1271 #endif
1272
1273 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1274 }
1275
1276 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1277 {
1278 int n;
1279
1280 if (hw->enabled) {
1281 SWVoiceCap *sc;
1282
1283 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1284 SWVoiceOut *sw = &sc->sw;
1285 int rpos2 = rpos;
1286
1287 n = samples;
1288 while (n) {
1289 int till_end_of_hw = hw->samples - rpos2;
1290 int to_write = audio_MIN (till_end_of_hw, n);
1291 int bytes = to_write << hw->info.shift;
1292 int written;
1293
1294 sw->buf = hw->mix_buf + rpos2;
1295 written = audio_pcm_sw_write (sw, NULL, bytes);
1296 if (written - bytes) {
1297 dolog ("Could not mix %d bytes into a capture "
1298 "buffer, mixed %d\n",
1299 bytes, written);
1300 break;
1301 }
1302 n -= to_write;
1303 rpos2 = (rpos2 + to_write) % hw->samples;
1304 }
1305 }
1306 }
1307
1308 n = audio_MIN (samples, hw->samples - rpos);
1309 mixeng_clear (hw->mix_buf + rpos, n);
1310 mixeng_clear (hw->mix_buf, samples - n);
1311 }
1312
1313 static void audio_run_out (AudioState *s)
1314 {
1315 HWVoiceOut *hw = NULL;
1316 SWVoiceOut *sw;
1317
1318 while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
1319 int played;
1320 int live, free, nb_live, cleanup_required, prev_rpos;
1321
1322 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1323 if (!nb_live) {
1324 live = 0;
1325 }
1326
1327 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1328 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1329 continue;
1330 }
1331
1332 if (hw->pending_disable && !nb_live) {
1333 SWVoiceCap *sc;
1334 #ifdef DEBUG_OUT
1335 dolog ("Disabling voice\n");
1336 #endif
1337 hw->enabled = 0;
1338 hw->pending_disable = 0;
1339 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1340 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1341 sc->sw.active = 0;
1342 audio_recalc_and_notify_capture (sc->cap);
1343 }
1344 continue;
1345 }
1346
1347 if (!live) {
1348 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1349 if (sw->active) {
1350 free = audio_get_free (sw);
1351 if (free > 0) {
1352 sw->callback.fn (sw->callback.opaque, free);
1353 }
1354 }
1355 }
1356 continue;
1357 }
1358
1359 prev_rpos = hw->rpos;
1360 played = hw->pcm_ops->run_out (hw);
1361 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1362 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1363 hw->rpos, hw->samples, played);
1364 hw->rpos = 0;
1365 }
1366
1367 #ifdef DEBUG_OUT
1368 dolog ("played=%d\n", played);
1369 #endif
1370
1371 if (played) {
1372 hw->ts_helper += played;
1373 audio_capture_mix_and_clear (hw, prev_rpos, played);
1374 }
1375
1376 cleanup_required = 0;
1377 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1378 if (!sw->active && sw->empty) {
1379 continue;
1380 }
1381
1382 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1383 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1384 played, sw->total_hw_samples_mixed);
1385 played = sw->total_hw_samples_mixed;
1386 }
1387
1388 sw->total_hw_samples_mixed -= played;
1389
1390 if (!sw->total_hw_samples_mixed) {
1391 sw->empty = 1;
1392 cleanup_required |= !sw->active && !sw->callback.fn;
1393 }
1394
1395 if (sw->active) {
1396 free = audio_get_free (sw);
1397 if (free > 0) {
1398 sw->callback.fn (sw->callback.opaque, free);
1399 }
1400 }
1401 }
1402
1403 if (cleanup_required) {
1404 SWVoiceOut *sw1;
1405
1406 sw = hw->sw_head.lh_first;
1407 while (sw) {
1408 sw1 = sw->entries.le_next;
1409 if (!sw->active && !sw->callback.fn) {
1410 #ifdef DEBUG_PLIVE
1411 dolog ("Finishing with old voice\n");
1412 #endif
1413 audio_close_out (s, sw);
1414 }
1415 sw = sw1;
1416 }
1417 }
1418 }
1419 }
1420
1421 static void audio_run_in (AudioState *s)
1422 {
1423 HWVoiceIn *hw = NULL;
1424
1425 while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
1426 SWVoiceIn *sw;
1427 int captured, min;
1428
1429 captured = hw->pcm_ops->run_in (hw);
1430
1431 min = audio_pcm_hw_find_min_in (hw);
1432 hw->total_samples_captured += captured - min;
1433 hw->ts_helper += captured;
1434
1435 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1436 sw->total_hw_samples_acquired -= min;
1437
1438 if (sw->active) {
1439 int avail;
1440
1441 avail = audio_get_avail (sw);
1442 if (avail > 0) {
1443 sw->callback.fn (sw->callback.opaque, avail);
1444 }
1445 }
1446 }
1447 }
1448 }
1449
1450 static void audio_run_capture (AudioState *s)
1451 {
1452 CaptureVoiceOut *cap;
1453
1454 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1455 int live, rpos, captured;
1456 HWVoiceOut *hw = &cap->hw;
1457 SWVoiceOut *sw;
1458
1459 captured = live = audio_pcm_hw_get_live_out (hw);
1460 rpos = hw->rpos;
1461 while (live) {
1462 int left = hw->samples - rpos;
1463 int to_capture = audio_MIN (live, left);
1464 st_sample_t *src;
1465 struct capture_callback *cb;
1466
1467 src = hw->mix_buf + rpos;
1468 hw->clip (cap->buf, src, to_capture);
1469 mixeng_clear (src, to_capture);
1470
1471 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1472 cb->ops.capture (cb->opaque, cap->buf,
1473 to_capture << hw->info.shift);
1474 }
1475 rpos = (rpos + to_capture) % hw->samples;
1476 live -= to_capture;
1477 }
1478 hw->rpos = rpos;
1479
1480 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1481 if (!sw->active && sw->empty) {
1482 continue;
1483 }
1484
1485 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1486 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1487 captured, sw->total_hw_samples_mixed);
1488 captured = sw->total_hw_samples_mixed;
1489 }
1490
1491 sw->total_hw_samples_mixed -= captured;
1492 sw->empty = sw->total_hw_samples_mixed == 0;
1493 }
1494 }
1495 }
1496
1497 static void audio_timer (void *opaque)
1498 {
1499 AudioState *s = opaque;
1500
1501 audio_run_out (s);
1502 audio_run_in (s);
1503 audio_run_capture (s);
1504
1505 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1506 }
1507
1508 static struct audio_option audio_options[] = {
1509 /* DAC */
1510 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1511 "Use fixed settings for host DAC", NULL, 0},
1512
1513 {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
1514 "Frequency for fixed host DAC", NULL, 0},
1515
1516 {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
1517 "Format for fixed host DAC", NULL, 0},
1518
1519 {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
1520 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1521
1522 {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
1523 "Number of voices for DAC", NULL, 0},
1524
1525 /* ADC */
1526 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1527 "Use fixed settings for host ADC", NULL, 0},
1528
1529 {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
1530 "Frequency for fixed host ADC", NULL, 0},
1531
1532 {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
1533 "Format for fixed host ADC", NULL, 0},
1534
1535 {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
1536 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1537
1538 {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
1539 "Number of voices for ADC", NULL, 0},
1540
1541 /* Misc */
1542 {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hz,
1543 "Timer period in HZ (0 - use lowest possible)", NULL, 0},
1544
1545 {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1546 "(undocumented)", NULL, 0},
1547
1548 {"LOG_TO_MONITOR", AUD_OPT_BOOL, &conf.log_to_monitor,
1549 "print logging messages to monitor instead of stderr", NULL, 0},
1550
1551 {NULL, 0, NULL, NULL, NULL, 0}
1552 };
1553
1554 static void audio_pp_nb_voices (const char *typ, int nb)
1555 {
1556 switch (nb) {
1557 case 0:
1558 printf ("Does not support %s\n", typ);
1559 break;
1560 case 1:
1561 printf ("One %s voice\n", typ);
1562 break;
1563 case INT_MAX:
1564 printf ("Theoretically supports many %s voices\n", typ);
1565 break;
1566 default:
1567 printf ("Theoretically supports upto %d %s voices\n", nb, typ);
1568 break;
1569 }
1570
1571 }
1572
1573 void AUD_help (void)
1574 {
1575 size_t i;
1576
1577 audio_process_options ("AUDIO", audio_options);
1578 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1579 struct audio_driver *d = drvtab[i];
1580 if (d->options) {
1581 audio_process_options (d->name, d->options);
1582 }
1583 }
1584
1585 printf ("Audio options:\n");
1586 audio_print_options ("AUDIO", audio_options);
1587 printf ("\n");
1588
1589 printf ("Available drivers:\n");
1590
1591 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1592 struct audio_driver *d = drvtab[i];
1593
1594 printf ("Name: %s\n", d->name);
1595 printf ("Description: %s\n", d->descr);
1596
1597 audio_pp_nb_voices ("playback", d->max_voices_out);
1598 audio_pp_nb_voices ("capture", d->max_voices_in);
1599
1600 if (d->options) {
1601 printf ("Options:\n");
1602 audio_print_options (d->name, d->options);
1603 }
1604 else {
1605 printf ("No options\n");
1606 }
1607 printf ("\n");
1608 }
1609
1610 printf (
1611 "Options are settable through environment variables.\n"
1612 "Example:\n"
1613 #ifdef _WIN32
1614 " set QEMU_AUDIO_DRV=wav\n"
1615 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1616 #else
1617 " export QEMU_AUDIO_DRV=wav\n"
1618 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1619 "(for csh replace export with setenv in the above)\n"
1620 #endif
1621 " qemu ...\n\n"
1622 );
1623 }
1624
1625 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1626 {
1627 if (drv->options) {
1628 audio_process_options (drv->name, drv->options);
1629 }
1630 s->drv_opaque = drv->init ();
1631
1632 if (s->drv_opaque) {
1633 audio_init_nb_voices_out (s, drv);
1634 audio_init_nb_voices_in (s, drv);
1635 s->drv = drv;
1636 return 0;
1637 }
1638 else {
1639 dolog ("Could not init `%s' audio driver\n", drv->name);
1640 return -1;
1641 }
1642 }
1643
1644 static void audio_vm_change_state_handler (void *opaque, int running)
1645 {
1646 AudioState *s = opaque;
1647 HWVoiceOut *hwo = NULL;
1648 HWVoiceIn *hwi = NULL;
1649 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1650
1651 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1652 hwo->pcm_ops->ctl_out (hwo, op);
1653 }
1654
1655 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1656 hwi->pcm_ops->ctl_in (hwi, op);
1657 }
1658 }
1659
1660 static void audio_atexit (void)
1661 {
1662 AudioState *s = &glob_audio_state;
1663 HWVoiceOut *hwo = NULL;
1664 HWVoiceIn *hwi = NULL;
1665
1666 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1667 SWVoiceCap *sc;
1668
1669 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1670 hwo->pcm_ops->fini_out (hwo);
1671
1672 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1673 CaptureVoiceOut *cap = sc->cap;
1674 struct capture_callback *cb;
1675
1676 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1677 cb->ops.destroy (cb->opaque);
1678 }
1679 }
1680 }
1681
1682 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1683 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1684 hwi->pcm_ops->fini_in (hwi);
1685 }
1686
1687 if (s->drv) {
1688 s->drv->fini (s->drv_opaque);
1689 }
1690 }
1691
1692 static void audio_save (QEMUFile *f, void *opaque)
1693 {
1694 (void) f;
1695 (void) opaque;
1696 }
1697
1698 static int audio_load (QEMUFile *f, void *opaque, int version_id)
1699 {
1700 (void) f;
1701 (void) opaque;
1702
1703 if (version_id != 1) {
1704 return -EINVAL;
1705 }
1706
1707 return 0;
1708 }
1709
1710 void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card)
1711 {
1712 card->audio = s;
1713 card->name = qemu_strdup (name);
1714 memset (&card->entries, 0, sizeof (card->entries));
1715 LIST_INSERT_HEAD (&s->card_head, card, entries);
1716 }
1717
1718 void AUD_remove_card (QEMUSoundCard *card)
1719 {
1720 LIST_REMOVE (card, entries);
1721 card->audio = NULL;
1722 qemu_free (card->name);
1723 }
1724
1725 AudioState *AUD_init (void)
1726 {
1727 size_t i;
1728 int done = 0;
1729 const char *drvname;
1730 AudioState *s = &glob_audio_state;
1731
1732 LIST_INIT (&s->hw_head_out);
1733 LIST_INIT (&s->hw_head_in);
1734 LIST_INIT (&s->cap_head);
1735 atexit (audio_atexit);
1736
1737 s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1738 if (!s->ts) {
1739 dolog ("Could not create audio timer\n");
1740 return NULL;
1741 }
1742
1743 audio_process_options ("AUDIO", audio_options);
1744
1745 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1746 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1747
1748 if (s->nb_hw_voices_out <= 0) {
1749 dolog ("Bogus number of playback voices %d, setting to 1\n",
1750 s->nb_hw_voices_out);
1751 s->nb_hw_voices_out = 1;
1752 }
1753
1754 if (s->nb_hw_voices_in <= 0) {
1755 dolog ("Bogus number of capture voices %d, setting to 0\n",
1756 s->nb_hw_voices_in);
1757 s->nb_hw_voices_in = 0;
1758 }
1759
1760 {
1761 int def;
1762 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1763 }
1764
1765 if (drvname) {
1766 int found = 0;
1767
1768 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1769 if (!strcmp (drvname, drvtab[i]->name)) {
1770 done = !audio_driver_init (s, drvtab[i]);
1771 found = 1;
1772 break;
1773 }
1774 }
1775
1776 if (!found) {
1777 dolog ("Unknown audio driver `%s'\n", drvname);
1778 dolog ("Run with -audio-help to list available drivers\n");
1779 }
1780 }
1781
1782 if (!done) {
1783 for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1784 if (drvtab[i]->can_be_default) {
1785 done = !audio_driver_init (s, drvtab[i]);
1786 }
1787 }
1788 }
1789
1790 if (!done) {
1791 done = !audio_driver_init (s, &no_audio_driver);
1792 if (!done) {
1793 dolog ("Could not initialize audio subsystem\n");
1794 }
1795 else {
1796 dolog ("warning: Using timer based audio emulation\n");
1797 }
1798 }
1799
1800 if (done) {
1801 VMChangeStateEntry *e;
1802
1803 if (conf.period.hz <= 0) {
1804 if (conf.period.hz < 0) {
1805 dolog ("warning: Timer period is negative - %d "
1806 "treating as zero\n",
1807 conf.period.hz);
1808 }
1809 conf.period.ticks = 1;
1810 }
1811 else {
1812 conf.period.ticks = ticks_per_sec / conf.period.hz;
1813 }
1814
1815 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1816 if (!e) {
1817 dolog ("warning: Could not register change state handler\n"
1818 "(Audio can continue looping even after stopping the VM)\n");
1819 }
1820 }
1821 else {
1822 qemu_del_timer (s->ts);
1823 return NULL;
1824 }
1825
1826 LIST_INIT (&s->card_head);
1827 register_savevm ("audio", 0, 1, audio_save, audio_load, s);
1828 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1829 return s;
1830 }
1831
1832 CaptureVoiceOut *AUD_add_capture (
1833 AudioState *s,
1834 audsettings_t *as,
1835 struct audio_capture_ops *ops,
1836 void *cb_opaque
1837 )
1838 {
1839 CaptureVoiceOut *cap;
1840 struct capture_callback *cb;
1841
1842 if (!s) {
1843 /* XXX suppress */
1844 s = &glob_audio_state;
1845 }
1846
1847 if (audio_validate_settings (as)) {
1848 dolog ("Invalid settings were passed when trying to add capture\n");
1849 audio_print_settings (as);
1850 goto err0;
1851 }
1852
1853 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1854 if (!cb) {
1855 dolog ("Could not allocate capture callback information, size %zu\n",
1856 sizeof (*cb));
1857 goto err0;
1858 }
1859 cb->ops = *ops;
1860 cb->opaque = cb_opaque;
1861
1862 cap = audio_pcm_capture_find_specific (s, as);
1863 if (cap) {
1864 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1865 return cap;
1866 }
1867 else {
1868 HWVoiceOut *hw;
1869 CaptureVoiceOut *cap;
1870
1871 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1872 if (!cap) {
1873 dolog ("Could not allocate capture voice, size %zu\n",
1874 sizeof (*cap));
1875 goto err1;
1876 }
1877
1878 hw = &cap->hw;
1879 LIST_INIT (&hw->sw_head);
1880 LIST_INIT (&cap->cb_head);
1881
1882 /* XXX find a more elegant way */
1883 hw->samples = 4096 * 4;
1884 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1885 sizeof (st_sample_t));
1886 if (!hw->mix_buf) {
1887 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1888 hw->samples);
1889 goto err2;
1890 }
1891
1892 audio_pcm_init_info (&hw->info, as);
1893
1894 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1895 if (!cap->buf) {
1896 dolog ("Could not allocate capture buffer "
1897 "(%d samples, each %d bytes)\n",
1898 hw->samples, 1 << hw->info.shift);
1899 goto err3;
1900 }
1901
1902 hw->clip = mixeng_clip
1903 [hw->info.nchannels == 2]
1904 [hw->info.sign]
1905 [hw->info.swap_endianness]
1906 [audio_bits_to_index (hw->info.bits)];
1907
1908 LIST_INSERT_HEAD (&s->cap_head, cap, entries);
1909 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1910
1911 hw = NULL;
1912 while ((hw = audio_pcm_hw_find_any_out (s, hw))) {
1913 audio_attach_capture (s, hw);
1914 }
1915 return cap;
1916
1917 err3:
1918 qemu_free (cap->hw.mix_buf);
1919 err2:
1920 qemu_free (cap);
1921 err1:
1922 qemu_free (cb);
1923 err0:
1924 return NULL;
1925 }
1926 }
1927
1928 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1929 {
1930 struct capture_callback *cb;
1931
1932 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1933 if (cb->opaque == cb_opaque) {
1934 cb->ops.destroy (cb_opaque);
1935 LIST_REMOVE (cb, entries);
1936 qemu_free (cb);
1937
1938 if (!cap->cb_head.lh_first) {
1939 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
1940
1941 while (sw) {
1942 SWVoiceCap *sc = (SWVoiceCap *) sw;
1943 #ifdef DEBUG_CAPTURE
1944 dolog ("freeing %s\n", sw->name);
1945 #endif
1946
1947 sw1 = sw->entries.le_next;
1948 if (sw->rate) {
1949 st_rate_stop (sw->rate);
1950 sw->rate = NULL;
1951 }
1952 LIST_REMOVE (sw, entries);
1953 LIST_REMOVE (sc, entries);
1954 qemu_free (sc);
1955 sw = sw1;
1956 }
1957 LIST_REMOVE (cap, entries);
1958 qemu_free (cap);
1959 }
1960 return;
1961 }
1962 }
1963 }
1964
1965 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
1966 {
1967 if (sw) {
1968 sw->vol.mute = mute;
1969 sw->vol.l = nominal_volume.l * lvol / 255;
1970 sw->vol.r = nominal_volume.r * rvol / 255;
1971 }
1972 }
1973
1974 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
1975 {
1976 if (sw) {
1977 sw->vol.mute = mute;
1978 sw->vol.l = nominal_volume.l * lvol / 255;
1979 sw->vol.r = nominal_volume.r * rvol / 255;
1980 }
1981 }