0.6.2-alt1
[qemu] / qemu-snapshot-2004-11-28_23 / audio / audio.c
1 /*
2  * QEMU Audio subsystem
3  * 
4  * Copyright (c) 2003-2004 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 <assert.h>
25 #include "vl.h"
26
27 #define USE_WAV_AUDIO
28
29 #include "audio/audio_int.h"
30
31 #define dolog(...) AUD_log ("audio", __VA_ARGS__)
32 #ifdef DEBUG
33 #define ldebug(...) dolog (__VA_ARGS__)
34 #else
35 #define ldebug(...)
36 #endif
37
38 #define QC_AUDIO_DRV    "QEMU_AUDIO_DRV"
39 #define QC_VOICES       "QEMU_VOICES"
40 #define QC_FIXED_FORMAT "QEMU_FIXED_FORMAT"
41 #define QC_FIXED_FREQ   "QEMU_FIXED_FREQ"
42
43 static HWVoice *hw_voices;
44
45 AudioState audio_state = {
46     1,                          /* use fixed settings */
47     44100,                      /* fixed frequency */
48     2,                          /* fixed channels */
49     AUD_FMT_S16,                /* fixed format */
50     1,                          /* number of hw voices */
51     -1                          /* voice size */
52 };
53
54 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
55 /* http://www.multi-platforms.com/Tips/PopCount.htm */
56 uint32_t popcount (uint32_t u)
57 {
58     u = ((u&0x55555555) + ((u>>1)&0x55555555));
59     u = ((u&0x33333333) + ((u>>2)&0x33333333));
60     u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
61     u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
62     u = ( u&0x0000ffff) + (u>>16);
63     return u;
64 }
65
66 inline uint32_t lsbindex (uint32_t u)
67 {
68     return popcount ((u&-u)-1);
69 }
70
71 int audio_get_conf_int (const char *key, int defval)
72 {
73     int val = defval;
74     char *strval;
75
76     strval = getenv (key);
77     if (strval) {
78         val = atoi (strval);
79     }
80
81     return val;
82 }
83
84 const char *audio_get_conf_str (const char *key, const char *defval)
85 {
86     const char *val = getenv (key);
87     if (!val)
88         return defval;
89     else
90         return val;
91 }
92
93 void AUD_log (const char *cap, const char *fmt, ...)
94 {
95     va_list ap;
96     fprintf (stderr, "%s: ", cap);
97     va_start (ap, fmt);
98     vfprintf (stderr, fmt, ap);
99     va_end (ap);
100 }
101
102 /*
103  * Soft Voice
104  */
105 void pcm_sw_free_resources (SWVoice *sw)
106 {
107     if (sw->buf) qemu_free (sw->buf);
108     if (sw->rate) st_rate_stop (sw->rate);
109     sw->buf = NULL;
110     sw->rate = NULL;
111 }
112
113 int pcm_sw_alloc_resources (SWVoice *sw)
114 {
115     sw->buf = qemu_mallocz (sw->hw->samples * sizeof (st_sample_t));
116     if (!sw->buf)
117         return -1;
118
119     sw->rate = st_rate_start (sw->freq, sw->hw->freq);
120     if (!sw->rate) {
121         qemu_free (sw->buf);
122         sw->buf = NULL;
123         return -1;
124     }
125     return 0;
126 }
127
128 void pcm_sw_fini (SWVoice *sw)
129 {
130     pcm_sw_free_resources (sw);
131 }
132
133 int pcm_sw_init (SWVoice *sw, HWVoice *hw, int freq,
134                  int nchannels, audfmt_e fmt)
135 {
136     int bits = 8, sign = 0;
137
138     switch (fmt) {
139     case AUD_FMT_S8:
140         sign = 1;
141     case AUD_FMT_U8:
142         break;
143
144     case AUD_FMT_S16:
145         sign = 1;
146     case AUD_FMT_U16:
147         bits = 16;
148         break;
149     }
150
151     sw->hw = hw;
152     sw->freq = freq;
153     sw->fmt = fmt;
154     sw->nchannels = nchannels;
155     sw->shift = (nchannels == 2) + (bits == 16);
156     sw->align = (1 << sw->shift) - 1;
157     sw->left = 0;
158     sw->pos = 0;
159     sw->wpos = 0;
160     sw->live = 0;
161     sw->ratio = (sw->hw->freq * ((int64_t) INT_MAX)) / sw->freq;
162     sw->bytes_per_second = sw->freq << sw->shift;
163     sw->conv = mixeng_conv[nchannels == 2][sign][bits == 16];
164
165     pcm_sw_free_resources (sw);
166     return pcm_sw_alloc_resources (sw);
167 }
168
169 /* Hard voice */
170 void pcm_hw_free_resources (HWVoice *hw)
171 {
172     if (hw->mix_buf)
173         qemu_free (hw->mix_buf);
174     hw->mix_buf = NULL;
175 }
176
177 int pcm_hw_alloc_resources (HWVoice *hw)
178 {
179     hw->mix_buf = qemu_mallocz (hw->samples * sizeof (st_sample_t));
180     if (!hw->mix_buf)
181         return -1;
182     return 0;
183 }
184
185 void pcm_hw_fini (HWVoice *hw)
186 {
187     if (hw->active) {
188         ldebug ("pcm_hw_fini: %d %d %d\n", hw->freq, hw->nchannels, hw->fmt);
189         pcm_hw_free_resources (hw);
190         hw->pcm_ops->fini (hw);
191         memset (hw, 0, audio_state.drv->voice_size);
192     }
193 }
194
195 void pcm_hw_gc (HWVoice *hw)
196 {
197     if (hw->nb_voices)
198         return;
199
200     pcm_hw_fini (hw);
201 }
202
203 int pcm_hw_get_live (HWVoice *hw)
204 {
205     int i, alive = 0, live = hw->samples;
206
207     for (i = 0; i < hw->nb_voices; i++) {
208         if (hw->pvoice[i]->live) {
209             live = audio_MIN (hw->pvoice[i]->live, live);
210             alive += 1;
211         }
212     }
213
214     if (alive)
215         return live;
216     else
217         return -1;
218 }
219
220 int pcm_hw_get_live2 (HWVoice *hw, int *nb_active)
221 {
222     int i, alive = 0, live = hw->samples;
223
224     *nb_active = 0;
225     for (i = 0; i < hw->nb_voices; i++) {
226         if (hw->pvoice[i]->live) {
227             if (hw->pvoice[i]->live < live) {
228                 *nb_active = hw->pvoice[i]->active != 0;
229                 live = hw->pvoice[i]->live;
230             }
231             alive += 1;
232         }
233     }
234
235     if (alive)
236         return live;
237     else
238         return -1;
239 }
240
241 void pcm_hw_dec_live (HWVoice *hw, int decr)
242 {
243     int i;
244
245     for (i = 0; i < hw->nb_voices; i++) {
246         if (hw->pvoice[i]->live) {
247             hw->pvoice[i]->live -= decr;
248         }
249     }
250 }
251
252 void pcm_hw_clear (HWVoice *hw, void *buf, int len)
253 {
254     if (!len)
255         return;
256
257     switch (hw->fmt) {
258     case AUD_FMT_S16:
259     case AUD_FMT_S8:
260         memset (buf, len << hw->shift, 0x00);
261         break;
262
263     case AUD_FMT_U8:
264         memset (buf, len << hw->shift, 0x80);
265         break;
266
267     case AUD_FMT_U16:
268         {
269             unsigned int i;
270             uint16_t *p = buf;
271             int shift = hw->nchannels - 1;
272
273             for (i = 0; i < len << shift; i++) {
274                 p[i] = INT16_MAX;
275             }
276         }
277         break;
278     }
279 }
280
281 int pcm_hw_write (SWVoice *sw, void *buf, int size)
282 {
283     int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
284     int ret = 0, pos = 0;
285     if (!sw)
286         return size;
287
288     hwsamples = sw->hw->samples;
289     samples = size >> sw->shift;
290
291     if (!sw->live) {
292         sw->wpos = sw->hw->rpos;
293     }
294     wpos = sw->wpos;
295     live = sw->live;
296     dead = hwsamples - live;
297     swlim = (dead * ((int64_t) INT_MAX)) / sw->ratio;
298     swlim = audio_MIN (swlim, samples);
299
300     ldebug ("size=%d live=%d dead=%d swlim=%d wpos=%d\n",
301            size, live, dead, swlim, wpos);
302     if (swlim)
303         sw->conv (sw->buf, buf, swlim);
304
305     while (swlim) {
306         dead = hwsamples - live;
307         left = hwsamples - wpos;
308         blck = audio_MIN (dead, left);
309         if (!blck) {
310             /* dolog ("swlim=%d\n", swlim); */
311             break;
312         }
313         isamp = swlim;
314         osamp = blck;
315         st_rate_flow (sw->rate, sw->buf + pos, sw->hw->mix_buf + wpos, &isamp, &osamp);
316         ret += isamp;
317         swlim -= isamp;
318         pos += isamp;
319         live += osamp;
320         wpos = (wpos + osamp) % hwsamples;
321     }
322
323     sw->wpos = wpos;
324     sw->live = live;
325     return ret << sw->shift;
326 }
327
328 int pcm_hw_init (HWVoice *hw, int freq, int nchannels, audfmt_e fmt)
329 {
330     int sign = 0, bits = 8;
331
332     pcm_hw_fini (hw);
333     ldebug ("pcm_hw_init: %d %d %d\n", freq, nchannels, fmt);
334     if (hw->pcm_ops->init (hw, freq, nchannels, fmt)) {
335         memset (hw, 0, audio_state.drv->voice_size);
336         return -1;
337     }
338
339     switch (hw->fmt) {
340     case AUD_FMT_S8:
341         sign = 1;
342     case AUD_FMT_U8:
343         break;
344
345     case AUD_FMT_S16:
346         sign = 1;
347     case AUD_FMT_U16:
348         bits = 16;
349         break;
350     }
351
352     hw->nb_voices = 0;
353     hw->active = 1;
354     hw->shift = (hw->nchannels == 2) + (bits == 16);
355     hw->bytes_per_second = hw->freq << hw->shift;
356     hw->align = (1 << hw->shift) - 1;
357     hw->samples = hw->bufsize >> hw->shift;
358     hw->clip = mixeng_clip[hw->nchannels == 2][sign][bits == 16];
359     if (pcm_hw_alloc_resources (hw)) {
360         pcm_hw_fini (hw);
361         return -1;
362     }
363     return 0;
364 }
365
366 static int dist (void *hw)
367 {
368     if (hw) {
369         return (((uint8_t *) hw - (uint8_t *) hw_voices)
370                 / audio_state.voice_size) + 1;
371     }
372     else {
373         return 0;
374     }
375 }
376
377 #define ADVANCE(hw) hw ? advance (hw, audio_state.voice_size) : hw_voices
378
379 HWVoice *pcm_hw_find_any (HWVoice *hw)
380 {
381     int i = dist (hw);
382     for (; i < audio_state.nb_hw_voices; i++) {
383         hw = ADVANCE (hw);
384         return hw;
385     }
386     return NULL;
387 }
388
389 HWVoice *pcm_hw_find_any_active (HWVoice *hw)
390 {
391     int i = dist (hw);
392     for (; i < audio_state.nb_hw_voices; i++) {
393         hw = ADVANCE (hw);
394         if (hw->active)
395             return hw;
396     }
397     return NULL;
398 }
399
400 HWVoice *pcm_hw_find_any_active_enabled (HWVoice *hw)
401 {
402     int i = dist (hw);
403     for (; i < audio_state.nb_hw_voices; i++) {
404         hw = ADVANCE (hw);
405         if (hw->active && hw->enabled)
406             return hw;
407     }
408     return NULL;
409 }
410
411 HWVoice *pcm_hw_find_any_passive (HWVoice *hw)
412 {
413     int i = dist (hw);
414     for (; i < audio_state.nb_hw_voices; i++) {
415         hw = ADVANCE (hw);
416         if (!hw->active)
417             return hw;
418     }
419     return NULL;
420 }
421
422 HWVoice *pcm_hw_find_specific (HWVoice *hw, int freq,
423                                int nchannels, audfmt_e fmt)
424 {
425     while ((hw = pcm_hw_find_any_active (hw))) {
426         if (hw->freq == freq &&
427             hw->nchannels == nchannels &&
428             hw->fmt == fmt)
429             return hw;
430     }
431     return NULL;
432 }
433
434 HWVoice *pcm_hw_add (int freq, int nchannels, audfmt_e fmt)
435 {
436     HWVoice *hw;
437
438     if (audio_state.fixed_format) {
439         freq = audio_state.fixed_freq;
440         nchannels = audio_state.fixed_channels;
441         fmt = audio_state.fixed_fmt;
442     }
443
444     hw = pcm_hw_find_specific (NULL, freq, nchannels, fmt);
445
446     if (hw)
447         return hw;
448
449     hw = pcm_hw_find_any_passive (NULL);
450     if (hw) {
451         hw->pcm_ops = audio_state.drv->pcm_ops;
452         if (!hw->pcm_ops)
453             return NULL;
454
455         if (pcm_hw_init (hw, freq, nchannels, fmt)) {
456             pcm_hw_gc (hw);
457             return NULL;
458         }
459         else
460             return hw;
461     }
462
463     return pcm_hw_find_any (NULL);
464 }
465
466 int pcm_hw_add_sw (HWVoice *hw, SWVoice *sw)
467 {
468     SWVoice **pvoice = qemu_mallocz ((hw->nb_voices + 1) * sizeof (sw));
469     if (!pvoice)
470         return -1;
471
472     memcpy (pvoice, hw->pvoice, hw->nb_voices * sizeof (sw));
473     qemu_free (hw->pvoice);
474     hw->pvoice = pvoice;
475     hw->pvoice[hw->nb_voices++] = sw;
476     return 0;
477 }
478
479 int pcm_hw_del_sw (HWVoice *hw, SWVoice *sw)
480 {
481     int i, j;
482     if (hw->nb_voices > 1) {
483         SWVoice **pvoice = qemu_mallocz ((hw->nb_voices - 1) * sizeof (sw));
484
485         if (!pvoice) {
486             dolog ("Can not maintain consistent state (not enough memory)\n");
487             return -1;
488         }
489
490         for (i = 0, j = 0; i < hw->nb_voices; i++) {
491             if (j >= hw->nb_voices - 1) {
492                 dolog ("Can not maintain consistent state "
493                        "(invariant violated)\n");
494                 return -1;
495             }
496             if (hw->pvoice[i] != sw)
497                 pvoice[j++] = hw->pvoice[i];
498         }
499         qemu_free (hw->pvoice);
500         hw->pvoice = pvoice;
501         hw->nb_voices -= 1;
502     }
503     else {
504         qemu_free (hw->pvoice);
505         hw->pvoice = NULL;
506         hw->nb_voices = 0;
507     }
508     return 0;
509 }
510
511 SWVoice *pcm_create_voice_pair (int freq, int nchannels, audfmt_e fmt)
512 {
513     SWVoice *sw;
514     HWVoice *hw;
515
516     sw = qemu_mallocz (sizeof (*sw));
517     if (!sw)
518         goto err1;
519
520     hw = pcm_hw_add (freq, nchannels, fmt);
521     if (!hw)
522         goto err2;
523
524     if (pcm_hw_add_sw (hw, sw))
525         goto err3;
526
527     if (pcm_sw_init (sw, hw, freq, nchannels, fmt))
528         goto err4;
529
530     return sw;
531
532 err4:
533     pcm_hw_del_sw (hw, sw);
534 err3:
535     pcm_hw_gc (hw);
536 err2:
537     qemu_free (sw);
538 err1:
539     return NULL;
540 }
541
542 SWVoice *AUD_open (SWVoice *sw, const char *name,
543                    int freq, int nchannels, audfmt_e fmt)
544 {
545     if (!audio_state.drv) {
546         return NULL;
547     }
548
549     if (sw && freq == sw->freq && sw->nchannels == nchannels && sw->fmt == fmt) {
550         return sw;
551     }
552
553     if (sw) {
554         ldebug ("Different format %s %d %d %d\n",
555                 name,
556                 sw->freq == freq,
557                 sw->nchannels == nchannels,
558                 sw->fmt == fmt);
559     }
560
561     if (nchannels != 1 && nchannels != 2) {
562         dolog ("Bogus channel count %d for voice %s\n", nchannels, name);
563         return NULL;
564     }
565
566     if (!audio_state.fixed_format && sw) {
567         pcm_sw_fini (sw);
568         pcm_hw_del_sw (sw->hw, sw);
569         pcm_hw_gc (sw->hw);
570         if (sw->name) {
571             qemu_free (sw->name);
572             sw->name = NULL;
573         }
574         qemu_free (sw);
575         sw = NULL;
576     }
577
578     if (sw) {
579         HWVoice *hw = sw->hw;
580         if (!hw) {
581             dolog ("Internal logic error voice %s has no hardware store\n",
582                    name);
583             return sw;
584         }
585
586         if (pcm_sw_init (sw, hw, freq, nchannels, fmt)) {
587             pcm_sw_fini (sw);
588             pcm_hw_del_sw (hw, sw);
589             pcm_hw_gc (hw);
590             if (sw->name) {
591                 qemu_free (sw->name);
592                 sw->name = NULL;
593             }
594             qemu_free (sw);
595             return NULL;
596         }
597     }
598     else {
599         sw = pcm_create_voice_pair (freq, nchannels, fmt);
600         if (!sw) {
601             dolog ("Failed to create voice %s\n", name);
602             return NULL;
603         }
604     }
605
606     if (sw->name) {
607         qemu_free (sw->name);
608         sw->name = NULL;
609     }
610     sw->name = qemu_strdup (name);
611     return sw;
612 }
613
614 void AUD_close (SWVoice *sw)
615 {
616     if (!sw)
617         return;
618
619     pcm_sw_fini (sw);
620     pcm_hw_del_sw (sw->hw, sw);
621     pcm_hw_gc (sw->hw);
622     if (sw->name) {
623         qemu_free (sw->name);
624         sw->name = NULL;
625     }
626     qemu_free (sw);
627 }
628
629 int AUD_write (SWVoice *sw, void *buf, int size)
630 {
631     int bytes;
632
633     if (!sw->hw->enabled)
634         dolog ("Writing to disabled voice %s\n", sw->name);
635     bytes = sw->hw->pcm_ops->write (sw, buf, size);
636     return bytes;
637 }
638
639 void AUD_run (void)
640 {
641     HWVoice *hw = NULL;
642
643     while ((hw = pcm_hw_find_any_active_enabled (hw))) {
644         int i;
645         if (hw->pending_disable && pcm_hw_get_live (hw) <= 0) {
646             hw->enabled = 0;
647             hw->pcm_ops->ctl (hw, VOICE_DISABLE);
648             for (i = 0; i < hw->nb_voices; i++) {
649                 hw->pvoice[i]->live = 0;
650                 /* hw->pvoice[i]->old_ticks = 0; */
651             }
652             continue;
653         }
654
655         hw->pcm_ops->run (hw);
656         assert (hw->rpos < hw->samples);
657         for (i = 0; i < hw->nb_voices; i++) {
658             SWVoice *sw = hw->pvoice[i];
659             if (!sw->active && !sw->live && sw->old_ticks) {
660                 int64_t delta = qemu_get_clock (vm_clock) - sw->old_ticks;
661                 if (delta > audio_state.ticks_threshold) {
662                     ldebug ("resetting old_ticks for %s\n", sw->name);
663                     sw->old_ticks = 0;
664                 }
665             }
666         }
667     }
668 }
669
670 int AUD_get_free (SWVoice *sw)
671 {
672     int free;
673
674     if (!sw)
675         return 4096;
676
677     free = ((sw->hw->samples - sw->live) << sw->hw->shift) * sw->ratio
678         / INT_MAX;
679
680     free &= ~sw->hw->align;
681     if (!free) return 0;
682
683     return free;
684 }
685
686 int AUD_get_buffer_size (SWVoice *sw)
687 {
688     return sw->hw->bufsize;
689 }
690
691 void AUD_adjust (SWVoice *sw, int bytes)
692 {
693     if (!sw)
694         return;
695     sw->old_ticks += (ticks_per_sec * (int64_t) bytes) / sw->bytes_per_second;
696 }
697
698 void AUD_reset (SWVoice *sw)
699 {
700     sw->active = 0;
701     sw->old_ticks = 0;
702 }
703
704 int AUD_calc_elapsed (SWVoice *sw)
705 {
706     int64_t now, delta, bytes;
707     int dead, swlim;
708
709     if (!sw)
710         return 0;
711
712     now = qemu_get_clock (vm_clock);
713     delta = now - sw->old_ticks;
714     bytes = (delta * sw->bytes_per_second) / ticks_per_sec;
715     if (delta < 0) {
716         dolog ("whoops delta(<0)=%lld\n", delta);
717         return 0;
718     }
719
720     dead = sw->hw->samples - sw->live;
721     swlim = ((dead * (int64_t) INT_MAX) / sw->ratio);
722
723     if (bytes > swlim) {
724         return swlim;
725     }
726     else {
727         return bytes;
728     }
729 }
730
731 void AUD_enable (SWVoice *sw, int on)
732 {
733     int i;
734     HWVoice *hw;
735
736     if (!sw)
737         return;
738
739     hw = sw->hw;
740     if (on) {
741         if (!sw->live)
742             sw->wpos = sw->hw->rpos;
743         if (!sw->old_ticks) {
744             sw->old_ticks = qemu_get_clock (vm_clock);
745         }
746     }
747
748     if (sw->active != on) {
749         if (on) {
750             hw->pending_disable = 0;
751             if (!hw->enabled) {
752                 hw->enabled = 1;
753                 for (i = 0; i < hw->nb_voices; i++) {
754                     ldebug ("resetting voice\n");
755                     sw = hw->pvoice[i];
756                     sw->old_ticks = qemu_get_clock (vm_clock);
757                 }
758                 hw->pcm_ops->ctl (hw, VOICE_ENABLE);
759             }
760         }
761         else {
762             if (hw->enabled && !hw->pending_disable) {
763                 int nb_active = 0;
764                 for (i = 0; i < hw->nb_voices; i++) {
765                     nb_active += hw->pvoice[i]->active != 0;
766                 }
767
768                 if (nb_active == 1) {
769                     hw->pending_disable = 1;
770                 }
771             }
772         }
773         sw->active = on;
774     }
775 }
776
777 static struct audio_output_driver *drvtab[] = {
778 #ifdef CONFIG_OSS
779     &oss_output_driver,
780 #endif
781 #ifdef CONFIG_FMOD
782     &fmod_output_driver,
783 #endif
784 #ifdef CONFIG_SDL
785     &sdl_output_driver,
786 #endif
787     &no_output_driver,
788 #ifdef USE_WAV_AUDIO
789     &wav_output_driver,
790 #endif
791 };
792
793 static int voice_init (struct audio_output_driver *drv)
794 {
795     audio_state.opaque = drv->init ();
796     if (audio_state.opaque) {
797         if (audio_state.nb_hw_voices > drv->max_voices) {
798             dolog ("`%s' does not support %d multiple hardware channels\n"
799                    "Resetting to %d\n",
800                    drv->name, audio_state.nb_hw_voices, drv->max_voices);
801             audio_state.nb_hw_voices = drv->max_voices;
802         }
803         hw_voices = qemu_mallocz (audio_state.nb_hw_voices * drv->voice_size);
804         if (hw_voices) {
805             audio_state.drv = drv;
806             return 1;
807         }
808         else {
809             dolog ("Not enough memory for %d `%s' voices (each %d bytes)\n",
810                    audio_state.nb_hw_voices, drv->name, drv->voice_size);
811             drv->fini (audio_state.opaque);
812             return 0;
813         }
814     }
815     else {
816         dolog ("Could not init `%s' audio\n", drv->name);
817         return 0;
818     }
819 }
820
821 static void audio_vm_stop_handler (void *opaque, int reason)
822 {
823     HWVoice *hw = NULL;
824
825     while ((hw = pcm_hw_find_any (hw))) {
826         if (!hw->pcm_ops)
827             continue;
828
829         hw->pcm_ops->ctl (hw, reason ? VOICE_ENABLE : VOICE_DISABLE);
830     }
831 }
832
833 static void audio_atexit (void)
834 {
835     HWVoice *hw = NULL;
836
837     while ((hw = pcm_hw_find_any (hw))) {
838         if (!hw->pcm_ops)
839             continue;
840
841         hw->pcm_ops->ctl (hw, VOICE_DISABLE);
842         hw->pcm_ops->fini (hw);
843     }
844     audio_state.drv->fini (audio_state.opaque);
845 }
846
847 static void audio_save (QEMUFile *f, void *opaque)
848 {
849 }
850
851 static int audio_load (QEMUFile *f, void *opaque, int version_id)
852 {
853     if (version_id != 1)
854         return -EINVAL;
855
856     return 0;
857 }
858
859 void AUD_init (void)
860 {
861     int i;
862     int done = 0;
863     const char *drvname;
864
865     audio_state.fixed_format =
866         !!audio_get_conf_int (QC_FIXED_FORMAT, audio_state.fixed_format);
867     audio_state.fixed_freq =
868         audio_get_conf_int (QC_FIXED_FREQ, audio_state.fixed_freq);
869     audio_state.nb_hw_voices =
870         audio_get_conf_int (QC_VOICES, audio_state.nb_hw_voices);
871
872     if (audio_state.nb_hw_voices <= 0) {
873         dolog ("Bogus number of voices %d, resetting to 1\n",
874                audio_state.nb_hw_voices);
875     }
876
877     drvname = audio_get_conf_str (QC_AUDIO_DRV, NULL);
878     if (drvname) {
879         int found = 0;
880         for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
881             if (!strcmp (drvname, drvtab[i]->name)) {
882                 done = voice_init (drvtab[i]);
883                 found = 1;
884                 break;
885             }
886         }
887         if (!found) {
888             dolog ("Unknown audio driver `%s'\n", drvname);
889         }
890     }
891
892     qemu_add_vm_stop_handler (audio_vm_stop_handler, NULL);
893     atexit (audio_atexit);
894
895     if (!done) {
896         for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
897             if (drvtab[i]->can_be_default)
898                 done = voice_init (drvtab[i]);
899         }
900     }
901
902     audio_state.ticks_threshold = ticks_per_sec / 50;
903     audio_state.freq_threshold = 100;
904
905     register_savevm ("audio", 0, 1, audio_save, audio_load, NULL);
906     if (!done) {
907         dolog ("Can not initialize audio subsystem\n");
908         voice_init (&no_output_driver);
909     }
910 }