]> Git Repo - qemu.git/blob - audio/audio.c
wav finalization fix (malc)
[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 "vl.h"
25
26 #define AUDIO_CAP "audio"
27 #include "audio_int.h"
28
29 /* #define DEBUG_PLIVE */
30 /* #define DEBUG_LIVE */
31 /* #define DEBUG_OUT */
32
33 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
34
35 static struct audio_driver *drvtab[] = {
36 #ifdef CONFIG_OSS
37     &oss_audio_driver,
38 #endif
39 #ifdef CONFIG_ALSA
40     &alsa_audio_driver,
41 #endif
42 #ifdef CONFIG_COREAUDIO
43     &coreaudio_audio_driver,
44 #endif
45 #ifdef CONFIG_DSOUND
46     &dsound_audio_driver,
47 #endif
48 #ifdef CONFIG_FMOD
49     &fmod_audio_driver,
50 #endif
51 #ifdef CONFIG_SDL
52     &sdl_audio_driver,
53 #endif
54     &no_audio_driver,
55     &wav_audio_driver
56 };
57
58 struct fixed_settings {
59     int enabled;
60     int nb_voices;
61     int greedy;
62     audsettings_t settings;
63 };
64
65 static struct {
66     struct fixed_settings fixed_out;
67     struct fixed_settings fixed_in;
68     union {
69         int hz;
70         int64_t ticks;
71     } period;
72     int plive;
73     int log_to_monitor;
74 } conf = {
75     {                           /* DAC fixed settings */
76         1,                      /* enabled */
77         1,                      /* nb_voices */
78         1,                      /* greedy */
79         {
80             44100,              /* freq */
81             2,                  /* nchannels */
82             AUD_FMT_S16         /* fmt */
83         }
84     },
85
86     {                           /* ADC fixed settings */
87         1,                      /* enabled */
88         1,                      /* nb_voices */
89         1,                      /* greedy */
90         {
91             44100,              /* freq */
92             2,                  /* nchannels */
93             AUD_FMT_S16         /* fmt */
94         }
95     },
96
97     { 0 },                      /* period */
98     0,                          /* plive */
99     0
100 };
101
102 static AudioState glob_audio_state;
103
104 volume_t nominal_volume = {
105     0,
106 #ifdef FLOAT_MIXENG
107     1.0,
108     1.0
109 #else
110     UINT_MAX,
111     UINT_MAX
112 #endif
113 };
114
115 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
116 /* http://www.multi-platforms.com/Tips/PopCount.htm */
117 uint32_t popcount (uint32_t u)
118 {
119     u = ((u&0x55555555) + ((u>>1)&0x55555555));
120     u = ((u&0x33333333) + ((u>>2)&0x33333333));
121     u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
122     u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
123     u = ( u&0x0000ffff) + (u>>16);
124     return u;
125 }
126
127 inline uint32_t lsbindex (uint32_t u)
128 {
129     return popcount ((u&-u)-1);
130 }
131
132 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
133 #error No its not
134 #else
135 int audio_bug (const char *funcname, int cond)
136 {
137     if (cond) {
138         static int shown;
139
140         AUD_log (NULL, "Error a bug that was just triggered in %s\n", funcname);
141         if (!shown) {
142             shown = 1;
143             AUD_log (NULL, "Save all your work and restart without audio\n");
144             AUD_log (NULL, "Please send bug report to [email protected]\n");
145             AUD_log (NULL, "I am sorry\n");
146         }
147         AUD_log (NULL, "Context:\n");
148
149 #if defined AUDIO_BREAKPOINT_ON_BUG
150 #  if defined HOST_I386
151 #    if defined __GNUC__
152         __asm__ ("int3");
153 #    elif defined _MSC_VER
154         _asm _emit 0xcc;
155 #    else
156         abort ();
157 #    endif
158 #  else
159         abort ();
160 #  endif
161 #endif
162     }
163
164     return cond;
165 }
166 #endif
167
168 void *audio_calloc (const char *funcname, int nmemb, size_t size)
169 {
170     int cond;
171     size_t len;
172
173     len = nmemb * size;
174     cond = !nmemb || !size;
175     cond |= nmemb < 0;
176     cond |= len < size;
177
178     if (audio_bug ("audio_calloc", cond)) {
179         AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
180                  funcname);
181         AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
182         return NULL;
183     }
184
185     return qemu_mallocz (len);
186 }
187
188 static char *audio_alloc_prefix (const char *s)
189 {
190     const char qemu_prefix[] = "QEMU_";
191     size_t len;
192     char *r;
193
194     if (!s) {
195         return NULL;
196     }
197
198     len = strlen (s);
199     r = qemu_malloc (len + sizeof (qemu_prefix));
200
201     if (r) {
202         size_t i;
203         char *u = r + sizeof (qemu_prefix) - 1;
204
205         strcpy (r, qemu_prefix);
206         strcat (r, s);
207
208         for (i = 0; i < len; ++i) {
209             u[i] = toupper (u[i]);
210         }
211     }
212     return r;
213 }
214
215 const char *audio_audfmt_to_string (audfmt_e fmt)
216 {
217     switch (fmt) {
218     case AUD_FMT_U8:
219         return "U8";
220
221     case AUD_FMT_U16:
222         return "U16";
223
224     case AUD_FMT_S8:
225         return "S8";
226
227     case AUD_FMT_S16:
228         return "S16";
229     }
230
231     dolog ("Bogus audfmt %d returning S16\n", fmt);
232     return "S16";
233 }
234
235 audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval, int *defaultp)
236 {
237     if (!strcasecmp (s, "u8")) {
238         *defaultp = 0;
239         return AUD_FMT_U8;
240     }
241     else if (!strcasecmp (s, "u16")) {
242         *defaultp = 0;
243         return AUD_FMT_U16;
244     }
245     else if (!strcasecmp (s, "s8")) {
246         *defaultp = 0;
247         return AUD_FMT_S8;
248     }
249     else if (!strcasecmp (s, "s16")) {
250         *defaultp = 0;
251         return AUD_FMT_S16;
252     }
253     else {
254         dolog ("Bogus audio format `%s' using %s\n",
255                s, audio_audfmt_to_string (defval));
256         *defaultp = 1;
257         return defval;
258     }
259 }
260
261 static audfmt_e audio_get_conf_fmt (const char *envname,
262                                     audfmt_e defval,
263                                     int *defaultp)
264 {
265     const char *var = getenv (envname);
266     if (!var) {
267         *defaultp = 1;
268         return defval;
269     }
270     return audio_string_to_audfmt (var, defval, defaultp);
271 }
272
273 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
274 {
275     int val;
276     char *strval;
277
278     strval = getenv (key);
279     if (strval) {
280         *defaultp = 0;
281         val = atoi (strval);
282         return val;
283     }
284     else {
285         *defaultp = 1;
286         return defval;
287     }
288 }
289
290 static const char *audio_get_conf_str (const char *key,
291                                        const char *defval,
292                                        int *defaultp)
293 {
294     const char *val = getenv (key);
295     if (!val) {
296         *defaultp = 1;
297         return defval;
298     }
299     else {
300         *defaultp = 0;
301         return val;
302     }
303 }
304
305 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
306 {
307     if (conf.log_to_monitor) {
308         if (cap) {
309             term_printf ("%s: ", cap);
310         }
311
312         term_vprintf (fmt, ap);
313     }
314     else {
315         if (cap) {
316             fprintf (stderr, "%s: ", cap);
317         }
318
319         vfprintf (stderr, fmt, ap);
320     }
321 }
322
323 void AUD_log (const char *cap, const char *fmt, ...)
324 {
325     va_list ap;
326
327     va_start (ap, fmt);
328     AUD_vlog (cap, fmt, ap);
329     va_end (ap);
330 }
331
332 static void audio_print_options (const char *prefix,
333                                  struct audio_option *opt)
334 {
335     char *uprefix;
336
337     if (!prefix) {
338         dolog ("No prefix specified\n");
339         return;
340     }
341
342     if (!opt) {
343         dolog ("No options\n");
344         return;
345     }
346
347     uprefix = audio_alloc_prefix (prefix);
348
349     for (; opt->name; opt++) {
350         const char *state = "default";
351         printf ("  %s_%s: ", uprefix, opt->name);
352
353         if (opt->overridenp && *opt->overridenp) {
354             state = "current";
355         }
356
357         switch (opt->tag) {
358         case AUD_OPT_BOOL:
359             {
360                 int *intp = opt->valp;
361                 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
362             }
363             break;
364
365         case AUD_OPT_INT:
366             {
367                 int *intp = opt->valp;
368                 printf ("integer, %s = %d\n", state, *intp);
369             }
370             break;
371
372         case AUD_OPT_FMT:
373             {
374                 audfmt_e *fmtp = opt->valp;
375                 printf (
376                     "format, %s = %s, (one of: U8 S8 U16 S16)\n",
377                     state,
378                     audio_audfmt_to_string (*fmtp)
379                     );
380             }
381             break;
382
383         case AUD_OPT_STR:
384             {
385                 const char **strp = opt->valp;
386                 printf ("string, %s = %s\n",
387                         state,
388                         *strp ? *strp : "(not set)");
389             }
390             break;
391
392         default:
393             printf ("???\n");
394             dolog ("Bad value tag for option %s_%s %d\n",
395                    uprefix, opt->name, opt->tag);
396             break;
397         }
398         printf ("    %s\n", opt->descr);
399     }
400
401     qemu_free (uprefix);
402 }
403
404 static void audio_process_options (const char *prefix,
405                                    struct audio_option *opt)
406 {
407     char *optname;
408     const char qemu_prefix[] = "QEMU_";
409     size_t preflen;
410
411     if (audio_bug (AUDIO_FUNC, !prefix)) {
412         dolog ("prefix = NULL\n");
413         return;
414     }
415
416     if (audio_bug (AUDIO_FUNC, !opt)) {
417         dolog ("opt = NULL\n");
418         return;
419     }
420
421     preflen = strlen (prefix);
422
423     for (; opt->name; opt++) {
424         size_t len, i;
425         int def;
426
427         if (!opt->valp) {
428             dolog ("Option value pointer for `%s' is not set\n",
429                    opt->name);
430             continue;
431         }
432
433         len = strlen (opt->name);
434         /* len of opt->name + len of prefix + size of qemu_prefix
435          * (includes trailing zero) + zero + underscore (on behalf of
436          * sizeof) */
437         optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1);
438         if (!optname) {
439             dolog ("Could not allocate memory for option name `%s'\n",
440                    opt->name);
441             continue;
442         }
443
444         strcpy (optname, qemu_prefix);
445
446         /* copy while upper-casing, including trailing zero */
447         for (i = 0; i <= preflen; ++i) {
448             optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]);
449         }
450         strcat (optname, "_");
451         strcat (optname, opt->name);
452
453         def = 1;
454         switch (opt->tag) {
455         case AUD_OPT_BOOL:
456         case AUD_OPT_INT:
457             {
458                 int *intp = opt->valp;
459                 *intp = audio_get_conf_int (optname, *intp, &def);
460             }
461             break;
462
463         case AUD_OPT_FMT:
464             {
465                 audfmt_e *fmtp = opt->valp;
466                 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
467             }
468             break;
469
470         case AUD_OPT_STR:
471             {
472                 const char **strp = opt->valp;
473                 *strp = audio_get_conf_str (optname, *strp, &def);
474             }
475             break;
476
477         default:
478             dolog ("Bad value tag for option `%s' - %d\n",
479                    optname, opt->tag);
480             break;
481         }
482
483         if (!opt->overridenp) {
484             opt->overridenp = &opt->overriden;
485         }
486         *opt->overridenp = !def;
487         qemu_free (optname);
488     }
489 }
490
491 static void audio_print_settings (audsettings_t *as)
492 {
493     dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
494
495     switch (as->fmt) {
496     case AUD_FMT_S8:
497         AUD_log (NULL, "S8");
498         break;
499     case AUD_FMT_U8:
500         AUD_log (NULL, "U8");
501         break;
502     case AUD_FMT_S16:
503         AUD_log (NULL, "S16");
504         break;
505     case AUD_FMT_U16:
506         AUD_log (NULL, "U16");
507         break;
508     default:
509         AUD_log (NULL, "invalid(%d)", as->fmt);
510         break;
511     }
512     AUD_log (NULL, "\n");
513 }
514
515 static int audio_validate_settigs (audsettings_t *as)
516 {
517     int invalid;
518
519     invalid = as->nchannels != 1 && as->nchannels != 2;
520
521     switch (as->fmt) {
522     case AUD_FMT_S8:
523     case AUD_FMT_U8:
524     case AUD_FMT_S16:
525     case AUD_FMT_U16:
526         break;
527     default:
528         invalid = 1;
529         break;
530     }
531
532     invalid |= as->freq <= 0;
533
534     if (invalid) {
535         return -1;
536     }
537     return 0;
538 }
539
540 static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
541 {
542     int bits = 8, sign = 0;
543
544     switch (as->fmt) {
545     case AUD_FMT_S8:
546         sign = 1;
547     case AUD_FMT_U8:
548         break;
549
550     case AUD_FMT_S16:
551         sign = 1;
552     case AUD_FMT_U16:
553         bits = 16;
554         break;
555     }
556     return info->freq == as->freq
557         && info->nchannels == as->nchannels
558         && info->sign == sign
559         && info->bits == bits;
560 }
561
562 void audio_pcm_init_info (
563     struct audio_pcm_info *info,
564     audsettings_t *as,
565     int swap_endian
566     )
567 {
568     int bits = 8, sign = 0;
569
570     switch (as->fmt) {
571     case AUD_FMT_S8:
572         sign = 1;
573     case AUD_FMT_U8:
574         break;
575
576     case AUD_FMT_S16:
577         sign = 1;
578     case AUD_FMT_U16:
579         bits = 16;
580         break;
581     }
582
583     info->freq = as->freq;
584     info->bits = bits;
585     info->sign = sign;
586     info->nchannels = as->nchannels;
587     info->shift = (as->nchannels == 2) + (bits == 16);
588     info->align = (1 << info->shift) - 1;
589     info->bytes_per_second = info->freq << info->shift;
590     info->swap_endian = swap_endian;
591 }
592
593 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
594 {
595     if (!len) {
596         return;
597     }
598
599     if (info->sign) {
600         memset (buf, len << info->shift, 0x00);
601     }
602     else {
603         if (info->bits == 8) {
604             memset (buf, len << info->shift, 0x80);
605         }
606         else {
607             int i;
608             uint16_t *p = buf;
609             int shift = info->nchannels - 1;
610             short s = INT16_MAX;
611
612             if (info->swap_endian) {
613                 s = bswap16 (s);
614             }
615
616             for (i = 0; i < len << shift; i++) {
617                 p[i] = s;
618             }
619         }
620     }
621 }
622
623 /*
624  * Hard voice (capture)
625  */
626 static void audio_pcm_hw_free_resources_in (HWVoiceIn *hw)
627 {
628     if (hw->conv_buf) {
629         qemu_free (hw->conv_buf);
630     }
631     hw->conv_buf = NULL;
632 }
633
634 static int audio_pcm_hw_alloc_resources_in (HWVoiceIn *hw)
635 {
636     hw->conv_buf = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (st_sample_t));
637     if (!hw->conv_buf) {
638         dolog ("Could not allocate ADC conversion buffer (%d samples)\n",
639                hw->samples);
640         return -1;
641     }
642     return 0;
643 }
644
645 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
646 {
647     SWVoiceIn *sw;
648     int m = hw->total_samples_captured;
649
650     for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
651         if (sw->active) {
652             m = audio_MIN (m, sw->total_hw_samples_acquired);
653         }
654     }
655     return m;
656 }
657
658 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
659 {
660     int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
661     if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
662         dolog ("live=%d hw->samples=%d\n", live, hw->samples);
663         return 0;
664     }
665     return live;
666 }
667
668 /*
669  * Soft voice (capture)
670  */
671 static void audio_pcm_sw_free_resources_in (SWVoiceIn *sw)
672 {
673     if (sw->conv_buf) {
674         qemu_free (sw->conv_buf);
675     }
676
677     if (sw->rate) {
678         st_rate_stop (sw->rate);
679     }
680
681     sw->conv_buf = NULL;
682     sw->rate = NULL;
683 }
684
685 static int audio_pcm_sw_alloc_resources_in (SWVoiceIn *sw)
686 {
687     int samples = ((int64_t) sw->hw->samples << 32) / sw->ratio;
688     sw->conv_buf = audio_calloc (AUDIO_FUNC, samples, sizeof (st_sample_t));
689     if (!sw->conv_buf) {
690         dolog ("Could not allocate buffer for `%s' (%d samples)\n",
691                SW_NAME (sw), samples);
692         return -1;
693     }
694
695     sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq);
696     if (!sw->rate) {
697         qemu_free (sw->conv_buf);
698         sw->conv_buf = NULL;
699         return -1;
700     }
701     return 0;
702 }
703
704 static int audio_pcm_sw_init_in (
705     SWVoiceIn *sw,
706     HWVoiceIn *hw,
707     const char *name,
708     audsettings_t *as
709     )
710 {
711     /* None of the cards emulated by QEMU are big-endian
712        hence following shortcut */
713     audio_pcm_init_info (&sw->info, as, audio_need_to_swap_endian (0));
714     sw->hw = hw;
715     sw->ratio = ((int64_t) sw->info.freq << 32) / sw->hw->info.freq;
716
717     sw->clip =
718         mixeng_clip
719         [sw->info.nchannels == 2]
720         [sw->info.sign]
721         [sw->info.swap_endian]
722         [sw->info.bits == 16];
723
724     sw->name = qemu_strdup (name);
725     audio_pcm_sw_free_resources_in (sw);
726     return audio_pcm_sw_alloc_resources_in (sw);
727 }
728
729 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
730 {
731     HWVoiceIn *hw = sw->hw;
732     int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
733     int rpos;
734
735     if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
736         dolog ("live=%d hw->samples=%d\n", live, hw->samples);
737         return 0;
738     }
739
740     rpos = hw->wpos - live;
741     if (rpos >= 0) {
742         return rpos;
743     }
744     else {
745         return hw->samples + rpos;
746     }
747 }
748
749 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
750 {
751     HWVoiceIn *hw = sw->hw;
752     int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
753     st_sample_t *src, *dst = sw->conv_buf;
754
755     rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
756
757     live = hw->total_samples_captured - sw->total_hw_samples_acquired;
758     if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
759         dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
760         return 0;
761     }
762
763     samples = size >> sw->info.shift;
764     if (!live) {
765         return 0;
766     }
767
768     swlim = (live * sw->ratio) >> 32;
769     swlim = audio_MIN (swlim, samples);
770
771     while (swlim) {
772         src = hw->conv_buf + rpos;
773         isamp = hw->wpos - rpos;
774         /* XXX: <= ? */
775         if (isamp <= 0) {
776             isamp = hw->samples - rpos;
777         }
778
779         if (!isamp) {
780             break;
781         }
782         osamp = swlim;
783
784         if (audio_bug (AUDIO_FUNC, osamp < 0)) {
785             dolog ("osamp=%d\n", osamp);
786             return 0;
787         }
788
789         st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
790         swlim -= osamp;
791         rpos = (rpos + isamp) % hw->samples;
792         dst += osamp;
793         ret += osamp;
794         total += isamp;
795     }
796
797     sw->clip (buf, sw->conv_buf, ret);
798     sw->total_hw_samples_acquired += total;
799     return ret << sw->info.shift;
800 }
801
802 /*
803  * Hard voice (playback)
804  */
805 static void audio_pcm_hw_free_resources_out (HWVoiceOut *hw)
806 {
807     if (hw->mix_buf) {
808         qemu_free (hw->mix_buf);
809     }
810
811     hw->mix_buf = NULL;
812 }
813
814 static int audio_pcm_hw_alloc_resources_out (HWVoiceOut *hw)
815 {
816     hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (st_sample_t));
817     if (!hw->mix_buf) {
818         dolog ("Could not allocate DAC mixing buffer (%d samples)\n",
819                hw->samples);
820         return -1;
821     }
822
823     return 0;
824 }
825
826 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
827 {
828     SWVoiceOut *sw;
829     int m = INT_MAX;
830     int nb_live = 0;
831
832     for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
833         if (sw->active || !sw->empty) {
834             m = audio_MIN (m, sw->total_hw_samples_mixed);
835             nb_live += 1;
836         }
837     }
838
839     *nb_livep = nb_live;
840     return m;
841 }
842
843 int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
844 {
845     int smin;
846
847     smin = audio_pcm_hw_find_min_out (hw, nb_live);
848
849     if (!*nb_live) {
850         return 0;
851     }
852     else {
853         int live = smin;
854
855         if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
856             dolog ("live=%d hw->samples=%d\n", live, hw->samples);
857             return 0;
858         }
859         return live;
860     }
861 }
862
863 int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
864 {
865     int nb_live;
866     int live;
867
868     live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
869     if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
870         dolog ("live=%d hw->samples=%d\n", live, hw->samples);
871         return 0;
872     }
873     return live;
874 }
875
876 /*
877  * Soft voice (playback)
878  */
879 static void audio_pcm_sw_free_resources_out (SWVoiceOut *sw)
880 {
881     if (sw->buf) {
882         qemu_free (sw->buf);
883     }
884
885     if (sw->rate) {
886         st_rate_stop (sw->rate);
887     }
888
889     sw->buf = NULL;
890     sw->rate = NULL;
891 }
892
893 static int audio_pcm_sw_alloc_resources_out (SWVoiceOut *sw)
894 {
895     sw->buf = audio_calloc (AUDIO_FUNC, sw->hw->samples, sizeof (st_sample_t));
896     if (!sw->buf) {
897         dolog ("Could not allocate buffer for `%s' (%d samples)\n",
898                SW_NAME (sw), sw->hw->samples);
899         return -1;
900     }
901
902     sw->rate = st_rate_start (sw->info.freq, sw->hw->info.freq);
903     if (!sw->rate) {
904         qemu_free (sw->buf);
905         sw->buf = NULL;
906         return -1;
907     }
908     return 0;
909 }
910
911 static int audio_pcm_sw_init_out (
912     SWVoiceOut *sw,
913     HWVoiceOut *hw,
914     const char *name,
915     audsettings_t *as
916     )
917 {
918     /* None of the cards emulated by QEMU are big-endian
919        hence following shortcut */
920     audio_pcm_init_info (&sw->info, as, audio_need_to_swap_endian (0));
921     sw->hw = hw;
922     sw->empty = 1;
923     sw->active = 0;
924     sw->ratio = ((int64_t) sw->hw->info.freq << 32) / sw->info.freq;
925     sw->total_hw_samples_mixed = 0;
926
927     sw->conv =
928         mixeng_conv
929         [sw->info.nchannels == 2]
930         [sw->info.sign]
931         [sw->info.swap_endian]
932         [sw->info.bits == 16];
933     sw->name = qemu_strdup (name);
934
935     audio_pcm_sw_free_resources_out (sw);
936     return audio_pcm_sw_alloc_resources_out (sw);
937 }
938
939 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
940 {
941     int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
942     int ret = 0, pos = 0, total = 0;
943
944     if (!sw) {
945         return size;
946     }
947
948     hwsamples = sw->hw->samples;
949
950     live = sw->total_hw_samples_mixed;
951     if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
952         dolog ("live=%d hw->samples=%d\n", live, hwsamples);
953         return 0;
954     }
955
956     if (live == hwsamples) {
957         return 0;
958     }
959
960     wpos = (sw->hw->rpos + live) % hwsamples;
961     samples = size >> sw->info.shift;
962
963     dead = hwsamples - live;
964     swlim = ((int64_t) dead << 32) / sw->ratio;
965     swlim = audio_MIN (swlim, samples);
966     if (swlim) {
967         sw->conv (sw->buf, buf, swlim, &sw->vol);
968     }
969
970     while (swlim) {
971         dead = hwsamples - live;
972         left = hwsamples - wpos;
973         blck = audio_MIN (dead, left);
974         if (!blck) {
975             break;
976         }
977         isamp = swlim;
978         osamp = blck;
979         st_rate_flow_mix (
980             sw->rate,
981             sw->buf + pos,
982             sw->hw->mix_buf + wpos,
983             &isamp,
984             &osamp
985             );
986         ret += isamp;
987         swlim -= isamp;
988         pos += isamp;
989         live += osamp;
990         wpos = (wpos + osamp) % hwsamples;
991         total += osamp;
992     }
993
994     sw->total_hw_samples_mixed += total;
995     sw->empty = sw->total_hw_samples_mixed == 0;
996
997 #ifdef DEBUG_OUT
998     dolog (
999         "%s: write size %d ret %d total sw %d\n",
1000         SW_NAME (sw),
1001         size >> sw->info.shift,
1002         ret,
1003         sw->total_hw_samples_mixed
1004         );
1005 #endif
1006
1007     return ret << sw->info.shift;
1008 }
1009
1010 #ifdef DEBUG_AUDIO
1011 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1012 {
1013     dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1014            cap, info->bits, info->sign, info->freq, info->nchannels);
1015 }
1016 #endif
1017
1018 #define DAC
1019 #include "audio_template.h"
1020 #undef DAC
1021 #include "audio_template.h"
1022
1023 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1024 {
1025     int bytes;
1026
1027     if (!sw) {
1028         /* XXX: Consider options */
1029         return size;
1030     }
1031
1032     if (!sw->hw->enabled) {
1033         dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1034         return 0;
1035     }
1036
1037     bytes = sw->hw->pcm_ops->write (sw, buf, size);
1038     return bytes;
1039 }
1040
1041 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1042 {
1043     int bytes;
1044
1045     if (!sw) {
1046         /* XXX: Consider options */
1047         return size;
1048     }
1049
1050     if (!sw->hw->enabled) {
1051         dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1052         return 0;
1053     }
1054
1055     bytes = sw->hw->pcm_ops->read (sw, buf, size);
1056     return bytes;
1057 }
1058
1059 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1060 {
1061     return sw->hw->samples << sw->hw->info.shift;
1062 }
1063
1064 void AUD_set_active_out (SWVoiceOut *sw, int on)
1065 {
1066     HWVoiceOut *hw;
1067
1068     if (!sw) {
1069         return;
1070     }
1071
1072     hw = sw->hw;
1073     if (sw->active != on) {
1074         SWVoiceOut *temp_sw;
1075
1076         if (on) {
1077             int total;
1078
1079             hw->pending_disable = 0;
1080             if (!hw->enabled) {
1081                 hw->enabled = 1;
1082                 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
1083             }
1084
1085             if (sw->empty) {
1086                 total = 0;
1087             }
1088         }
1089         else {
1090             if (hw->enabled) {
1091                 int nb_active = 0;
1092
1093                 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1094                      temp_sw = temp_sw->entries.le_next) {
1095                     nb_active += temp_sw->active != 0;
1096                 }
1097
1098                 hw->pending_disable = nb_active == 1;
1099             }
1100         }
1101         sw->active = on;
1102     }
1103 }
1104
1105 void AUD_set_active_in (SWVoiceIn *sw, int on)
1106 {
1107     HWVoiceIn *hw;
1108
1109     if (!sw) {
1110         return;
1111     }
1112
1113     hw = sw->hw;
1114     if (sw->active != on) {
1115         SWVoiceIn *temp_sw;
1116
1117         if (on) {
1118             if (!hw->enabled) {
1119                 hw->enabled = 1;
1120                 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
1121             }
1122             sw->total_hw_samples_acquired = hw->total_samples_captured;
1123         }
1124         else {
1125             if (hw->enabled) {
1126                 int nb_active = 0;
1127
1128                 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1129                      temp_sw = temp_sw->entries.le_next) {
1130                     nb_active += temp_sw->active != 0;
1131                 }
1132
1133                 if (nb_active == 1) {
1134                     hw->enabled = 0;
1135                     hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1136                 }
1137             }
1138         }
1139         sw->active = on;
1140     }
1141 }
1142
1143 static int audio_get_avail (SWVoiceIn *sw)
1144 {
1145     int live;
1146
1147     if (!sw) {
1148         return 0;
1149     }
1150
1151     live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1152     if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1153         dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1154         return 0;
1155     }
1156
1157     ldebug (
1158         "%s: get_avail live %d ret %lld\n",
1159         SW_NAME (sw),
1160         live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1161         );
1162
1163     return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1164 }
1165
1166 static int audio_get_free (SWVoiceOut *sw)
1167 {
1168     int live, dead;
1169
1170     if (!sw) {
1171         return 0;
1172     }
1173
1174     live = sw->total_hw_samples_mixed;
1175
1176     if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1177         dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1178         return 0;
1179     }
1180
1181     dead = sw->hw->samples - live;
1182
1183 #ifdef DEBUG_OUT
1184     dolog ("%s: get_free live %d dead %d ret %lld\n",
1185            SW_NAME (sw),
1186            live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1187 #endif
1188
1189     return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1190 }
1191
1192 static void audio_run_out (AudioState *s)
1193 {
1194     HWVoiceOut *hw = NULL;
1195     SWVoiceOut *sw;
1196
1197     while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
1198         int played;
1199         int live, free, nb_live, cleanup_required;
1200
1201         live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1202         if (!nb_live) {
1203             live = 0;
1204         }
1205
1206         if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1207             dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1208             continue;
1209         }
1210
1211         if (hw->pending_disable && !nb_live) {
1212 #ifdef DEBUG_OUT
1213             dolog ("Disabling voice\n");
1214 #endif
1215             hw->enabled = 0;
1216             hw->pending_disable = 0;
1217             hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1218             continue;
1219         }
1220
1221         if (!live) {
1222             for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1223                 if (sw->active) {
1224                     free = audio_get_free (sw);
1225                     if (free > 0) {
1226                         sw->callback.fn (sw->callback.opaque, free);
1227                     }
1228                 }
1229             }
1230             continue;
1231         }
1232
1233         played = hw->pcm_ops->run_out (hw);
1234         if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1235             dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1236                    hw->rpos, hw->samples, played);
1237             hw->rpos = 0;
1238         }
1239
1240 #ifdef DEBUG_OUT
1241         dolog ("played=%d\n", played);
1242 #endif
1243
1244         if (played) {
1245             hw->ts_helper += played;
1246         }
1247
1248         cleanup_required = 0;
1249         for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1250             if (!sw->active && sw->empty) {
1251                 continue;
1252             }
1253
1254             if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1255                 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1256                        played, sw->total_hw_samples_mixed);
1257                 played = sw->total_hw_samples_mixed;
1258             }
1259
1260             sw->total_hw_samples_mixed -= played;
1261
1262             if (!sw->total_hw_samples_mixed) {
1263                 sw->empty = 1;
1264                 cleanup_required |= !sw->active && !sw->callback.fn;
1265             }
1266
1267             if (sw->active) {
1268                 free = audio_get_free (sw);
1269                 if (free > 0) {
1270                     sw->callback.fn (sw->callback.opaque, free);
1271                 }
1272             }
1273         }
1274
1275         if (cleanup_required) {
1276         restart:
1277             for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1278                 if (!sw->active && !sw->callback.fn) {
1279 #ifdef DEBUG_PLIVE
1280                     dolog ("Finishing with old voice\n");
1281 #endif
1282                     audio_close_out (s, sw);
1283                     goto restart; /* play it safe */
1284                 }
1285             }
1286         }
1287     }
1288 }
1289
1290 static void audio_run_in (AudioState *s)
1291 {
1292     HWVoiceIn *hw = NULL;
1293
1294     while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
1295         SWVoiceIn *sw;
1296         int captured, min;
1297
1298         captured = hw->pcm_ops->run_in (hw);
1299
1300         min = audio_pcm_hw_find_min_in (hw);
1301         hw->total_samples_captured += captured - min;
1302         hw->ts_helper += captured;
1303
1304         for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1305             sw->total_hw_samples_acquired -= min;
1306
1307             if (sw->active) {
1308                 int avail;
1309
1310                 avail = audio_get_avail (sw);
1311                 if (avail > 0) {
1312                     sw->callback.fn (sw->callback.opaque, avail);
1313                 }
1314             }
1315         }
1316     }
1317 }
1318
1319 static struct audio_option audio_options[] = {
1320     /* DAC */
1321     {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1322      "Use fixed settings for host DAC", NULL, 0},
1323
1324     {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
1325      "Frequency for fixed host DAC", NULL, 0},
1326
1327     {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
1328      "Format for fixed host DAC", NULL, 0},
1329
1330     {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
1331      "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1332
1333     {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
1334      "Number of voices for DAC", NULL, 0},
1335
1336     /* ADC */
1337     {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1338      "Use fixed settings for host ADC", NULL, 0},
1339
1340     {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
1341      "Frequency for fixed host ADC", NULL, 0},
1342
1343     {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
1344      "Format for fixed host ADC", NULL, 0},
1345
1346     {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
1347      "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1348
1349     {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
1350      "Number of voices for ADC", NULL, 0},
1351
1352     /* Misc */
1353     {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hz,
1354      "Timer period in HZ (0 - use lowest possible)", NULL, 0},
1355
1356     {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1357      "(undocumented)", NULL, 0},
1358
1359
1360     {"LOG_TO_MONITOR", AUD_OPT_BOOL, &conf.log_to_monitor,
1361      "print logging messages to montior instead of stderr", NULL, 0},
1362
1363     {NULL, 0, NULL, NULL, NULL, 0}
1364 };
1365
1366 void AUD_help (void)
1367 {
1368     size_t i;
1369
1370     audio_process_options ("AUDIO", audio_options);
1371     for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1372         struct audio_driver *d = drvtab[i];
1373         if (d->options) {
1374             audio_process_options (d->name, d->options);
1375         }
1376     }
1377
1378     printf ("Audio options:\n");
1379     audio_print_options ("AUDIO", audio_options);
1380     printf ("\n");
1381
1382     printf ("Available drivers:\n");
1383
1384     for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1385         struct audio_driver *d = drvtab[i];
1386
1387         printf ("Name: %s\n", d->name);
1388         printf ("Description: %s\n", d->descr);
1389
1390         switch (d->max_voices_out) {
1391         case 0:
1392             printf ("Does not support DAC\n");
1393             break;
1394         case 1:
1395             printf ("One DAC voice\n");
1396             break;
1397         case INT_MAX:
1398             printf ("Theoretically supports many DAC voices\n");
1399             break;
1400         default:
1401             printf ("Theoretically supports upto %d DAC voices\n",
1402                      d->max_voices_out);
1403             break;
1404         }
1405
1406         switch (d->max_voices_in) {
1407         case 0:
1408             printf ("Does not support ADC\n");
1409             break;
1410         case 1:
1411             printf ("One ADC voice\n");
1412             break;
1413         case INT_MAX:
1414             printf ("Theoretically supports many ADC voices\n");
1415             break;
1416         default:
1417             printf ("Theoretically supports upto %d ADC voices\n",
1418                      d->max_voices_in);
1419             break;
1420         }
1421
1422         if (d->options) {
1423             printf ("Options:\n");
1424             audio_print_options (d->name, d->options);
1425         }
1426         else {
1427             printf ("No options\n");
1428         }
1429         printf ("\n");
1430     }
1431
1432     printf (
1433         "Options are settable through environment variables.\n"
1434         "Example:\n"
1435 #ifdef _WIN32
1436         "  set QEMU_AUDIO_DRV=wav\n"
1437         "  set QEMU_WAV_PATH=c:/tune.wav\n"
1438 #else
1439         "  export QEMU_AUDIO_DRV=wav\n"
1440         "  export QEMU_WAV_PATH=$HOME/tune.wav\n"
1441         "(for csh replace export with setenv in the above)\n"
1442 #endif
1443         "  qemu ...\n\n"
1444         );
1445 }
1446
1447 void audio_timer (void *opaque)
1448 {
1449     AudioState *s = opaque;
1450
1451     audio_run_out (s);
1452     audio_run_in (s);
1453
1454     qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1455 }
1456
1457 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1458 {
1459     if (drv->options) {
1460         audio_process_options (drv->name, drv->options);
1461     }
1462     s->drv_opaque = drv->init ();
1463
1464     if (s->drv_opaque) {
1465         if (s->nb_hw_voices_out > drv->max_voices_out) {
1466             if (!drv->max_voices_out) {
1467                 dolog ("`%s' does not support DAC\n", drv->name);
1468             }
1469             else {
1470                 dolog (
1471                     "`%s' does not support %d multiple DAC voicess\n"
1472                     "Resetting to %d\n",
1473                     drv->name,
1474                     s->nb_hw_voices_out,
1475                     drv->max_voices_out
1476                     );
1477             }
1478             s->nb_hw_voices_out = drv->max_voices_out;
1479         }
1480
1481
1482         if (!drv->voice_size_in && drv->max_voices_in) {
1483             ldebug ("warning: No ADC voice size defined for `%s'\n",
1484                     drv->name);
1485             drv->max_voices_in = 0;
1486         }
1487
1488         if (!drv->voice_size_out && drv->max_voices_out) {
1489             ldebug ("warning: No DAC voice size defined for `%s'\n",
1490                     drv->name);
1491         }
1492
1493         if (drv->voice_size_in && !drv->max_voices_in) {
1494             ldebug ("warning: `%s' ADC voice size %d, zero voices \n",
1495                     drv->name, drv->voice_size_out);
1496         }
1497
1498         if (drv->voice_size_out && !drv->max_voices_out) {
1499             ldebug ("warning: `%s' DAC voice size %d, zero voices \n",
1500                     drv->name, drv->voice_size_in);
1501         }
1502
1503         if (s->nb_hw_voices_in > drv->max_voices_in) {
1504             if (!drv->max_voices_in) {
1505                 ldebug ("`%s' does not support ADC\n", drv->name);
1506             }
1507             else {
1508                 dolog (
1509                     "`%s' does not support %d multiple ADC voices\n"
1510                     "Resetting to %d\n",
1511                     drv->name,
1512                     s->nb_hw_voices_in,
1513                     drv->max_voices_in
1514                     );
1515             }
1516             s->nb_hw_voices_in = drv->max_voices_in;
1517         }
1518
1519         LIST_INIT (&s->hw_head_out);
1520         LIST_INIT (&s->hw_head_in);
1521         s->drv = drv;
1522         return 0;
1523     }
1524     else {
1525         dolog ("Could not init `%s' audio driver\n", drv->name);
1526         return -1;
1527     }
1528 }
1529
1530 static void audio_vm_change_state_handler (void *opaque, int running)
1531 {
1532     AudioState *s = opaque;
1533     HWVoiceOut *hwo = NULL;
1534     HWVoiceIn *hwi = NULL;
1535     int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1536
1537     while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1538         hwo->pcm_ops->ctl_out (hwo, op);
1539     }
1540
1541     while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1542         hwi->pcm_ops->ctl_in (hwi, op);
1543     }
1544 }
1545
1546 static void audio_atexit (void)
1547 {
1548     AudioState *s = &glob_audio_state;
1549     HWVoiceOut *hwo = NULL;
1550     HWVoiceIn *hwi = NULL;
1551
1552     while ((hwo = audio_pcm_hw_find_any_out (s, hwo))) {
1553         if (!hwo->pcm_ops) {
1554             continue;
1555         }
1556
1557         if (hwo->enabled) {
1558             hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1559         }
1560         hwo->pcm_ops->fini_out (hwo);
1561     }
1562
1563     while ((hwi = audio_pcm_hw_find_any_in (s, hwi))) {
1564         if (!hwi->pcm_ops) {
1565             continue;
1566         }
1567
1568         if (hwi->enabled) {
1569             hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1570         }
1571         hwi->pcm_ops->fini_in (hwi);
1572     }
1573
1574     if (s->drv) {
1575         s->drv->fini (s->drv_opaque);
1576     }
1577 }
1578
1579 static void audio_save (QEMUFile *f, void *opaque)
1580 {
1581     (void) f;
1582     (void) opaque;
1583 }
1584
1585 static int audio_load (QEMUFile *f, void *opaque, int version_id)
1586 {
1587     (void) f;
1588     (void) opaque;
1589
1590     if (version_id != 1) {
1591         return -EINVAL;
1592     }
1593
1594     return 0;
1595 }
1596
1597 void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card)
1598 {
1599     card->audio = s;
1600     card->name = qemu_strdup (name);
1601     memset (&card->entries, 0, sizeof (card->entries));
1602     LIST_INSERT_HEAD (&s->card_head, card, entries);
1603 }
1604
1605 void AUD_remove_card (QEMUSoundCard *card)
1606 {
1607     LIST_REMOVE (card, entries);
1608     card->audio = NULL;
1609     qemu_free (card->name);
1610 }
1611
1612 AudioState *AUD_init (void)
1613 {
1614     size_t i;
1615     int done = 0;
1616     const char *drvname;
1617     AudioState *s = &glob_audio_state;
1618
1619     audio_process_options ("AUDIO", audio_options);
1620
1621     s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1622     s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1623
1624     if (s->nb_hw_voices_out <= 0) {
1625         dolog ("Bogus number of DAC voices %d\n",
1626                s->nb_hw_voices_out);
1627         s->nb_hw_voices_out = 1;
1628     }
1629
1630     if (s->nb_hw_voices_in <= 0) {
1631         dolog ("Bogus number of ADC voices %d\n",
1632                s->nb_hw_voices_in);
1633         s->nb_hw_voices_in = 1;
1634     }
1635
1636     {
1637         int def;
1638         drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1639     }
1640
1641     s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1642     if (!s->ts) {
1643         dolog ("Could not create audio timer\n");
1644         return NULL;
1645     }
1646
1647     if (drvname) {
1648         int found = 0;
1649
1650         for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1651             if (!strcmp (drvname, drvtab[i]->name)) {
1652                 done = !audio_driver_init (s, drvtab[i]);
1653                 found = 1;
1654                 break;
1655             }
1656         }
1657
1658         if (!found) {
1659             dolog ("Unknown audio driver `%s'\n", drvname);
1660             dolog ("Run with -audio-help to list available drivers\n");
1661         }
1662     }
1663
1664     if (!done) {
1665         for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1666             if (drvtab[i]->can_be_default) {
1667                 done = !audio_driver_init (s, drvtab[i]);
1668             }
1669         }
1670     }
1671
1672     if (!done) {
1673         done = !audio_driver_init (s, &no_audio_driver);
1674         if (!done) {
1675             dolog ("Could not initialize audio subsystem\n");
1676         }
1677         else {
1678             dolog ("warning: Using timer based audio emulation\n");
1679         }
1680     }
1681
1682     if (done) {
1683         if (conf.period.hz <= 0) {
1684             if (conf.period.hz < 0) {
1685                 dolog ("warning: Timer period is negative - %d "
1686                        "treating as zero\n",
1687                        conf.period.hz);
1688             }
1689             conf.period.ticks = 1;
1690         }
1691         else {
1692             conf.period.ticks = ticks_per_sec / conf.period.hz;
1693         }
1694
1695         qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1696     }
1697     else {
1698         qemu_del_timer (s->ts);
1699         return NULL;
1700     }
1701
1702     LIST_INIT (&s->card_head);
1703     register_savevm ("audio", 0, 1, audio_save, audio_load, s);
1704     atexit (audio_atexit);
1705     qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1706     return s;
1707 }
This page took 0.116421 seconds and 4 git commands to generate.