4 * Copyright (c) 2003-2004 Vassili Karpov (malc)
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
29 #include "audio/audio_int.h"
31 #define dolog(...) AUD_log ("audio", __VA_ARGS__)
33 #define ldebug(...) dolog (__VA_ARGS__)
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"
43 static HWVoice *hw_voices;
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 */
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)
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);
66 inline uint32_t lsbindex (uint32_t u)
68 return popcount ((u&-u)-1);
71 int audio_get_conf_int (const char *key, int defval)
76 strval = getenv (key);
84 const char *audio_get_conf_str (const char *key, const char *defval)
86 const char *val = getenv (key);
93 void AUD_log (const char *cap, const char *fmt, ...)
96 fprintf (stderr, "%s: ", cap);
98 vfprintf (stderr, fmt, ap);
105 void pcm_sw_free_resources (SWVoice *sw)
107 if (sw->buf) qemu_free (sw->buf);
108 if (sw->rate) st_rate_stop (sw->rate);
113 int pcm_sw_alloc_resources (SWVoice *sw)
115 sw->buf = qemu_mallocz (sw->hw->samples * sizeof (st_sample_t));
119 sw->rate = st_rate_start (sw->freq, sw->hw->freq);
128 void pcm_sw_fini (SWVoice *sw)
130 pcm_sw_free_resources (sw);
133 int pcm_sw_init (SWVoice *sw, HWVoice *hw, int freq,
134 int nchannels, audfmt_e fmt)
136 int bits = 8, sign = 0;
154 sw->nchannels = nchannels;
155 sw->shift = (nchannels == 2) + (bits == 16);
156 sw->align = (1 << sw->shift) - 1;
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];
165 pcm_sw_free_resources (sw);
166 return pcm_sw_alloc_resources (sw);
170 void pcm_hw_free_resources (HWVoice *hw)
173 qemu_free (hw->mix_buf);
177 int pcm_hw_alloc_resources (HWVoice *hw)
179 hw->mix_buf = qemu_mallocz (hw->samples * sizeof (st_sample_t));
185 void pcm_hw_fini (HWVoice *hw)
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);
195 void pcm_hw_gc (HWVoice *hw)
203 int pcm_hw_get_live (HWVoice *hw)
205 int i, alive = 0, live = hw->samples;
207 for (i = 0; i < hw->nb_voices; i++) {
208 if (hw->pvoice[i]->live) {
209 live = audio_MIN (hw->pvoice[i]->live, live);
220 int pcm_hw_get_live2 (HWVoice *hw, int *nb_active)
222 int i, alive = 0, live = hw->samples;
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;
241 void pcm_hw_dec_live (HWVoice *hw, int decr)
245 for (i = 0; i < hw->nb_voices; i++) {
246 if (hw->pvoice[i]->live) {
247 hw->pvoice[i]->live -= decr;
252 void pcm_hw_clear (HWVoice *hw, void *buf, int len)
260 memset (buf, len << hw->shift, 0x00);
264 memset (buf, len << hw->shift, 0x80);
271 int shift = hw->nchannels - 1;
273 for (i = 0; i < len << shift; i++) {
281 int pcm_hw_write (SWVoice *sw, void *buf, int size)
283 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
284 int ret = 0, pos = 0;
288 hwsamples = sw->hw->samples;
289 samples = size >> sw->shift;
292 sw->wpos = sw->hw->rpos;
296 dead = hwsamples - live;
297 swlim = (dead * ((int64_t) INT_MAX)) / sw->ratio;
298 swlim = audio_MIN (swlim, samples);
300 ldebug ("size=%d live=%d dead=%d swlim=%d wpos=%d\n",
301 size, live, dead, swlim, wpos);
303 sw->conv (sw->buf, buf, swlim);
306 dead = hwsamples - live;
307 left = hwsamples - wpos;
308 blck = audio_MIN (dead, left);
310 /* dolog ("swlim=%d\n", swlim); */
315 st_rate_flow (sw->rate, sw->buf + pos, sw->hw->mix_buf + wpos, &isamp, &osamp);
320 wpos = (wpos + osamp) % hwsamples;
325 return ret << sw->shift;
328 int pcm_hw_init (HWVoice *hw, int freq, int nchannels, audfmt_e fmt)
330 int sign = 0, bits = 8;
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);
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)) {
366 static int dist (void *hw)
369 return (((uint8_t *) hw - (uint8_t *) hw_voices)
370 / audio_state.voice_size) + 1;
377 #define ADVANCE(hw) hw ? advance (hw, audio_state.voice_size) : hw_voices
379 HWVoice *pcm_hw_find_any (HWVoice *hw)
382 for (; i < audio_state.nb_hw_voices; i++) {
389 HWVoice *pcm_hw_find_any_active (HWVoice *hw)
392 for (; i < audio_state.nb_hw_voices; i++) {
400 HWVoice *pcm_hw_find_any_active_enabled (HWVoice *hw)
403 for (; i < audio_state.nb_hw_voices; i++) {
405 if (hw->active && hw->enabled)
411 HWVoice *pcm_hw_find_any_passive (HWVoice *hw)
414 for (; i < audio_state.nb_hw_voices; i++) {
422 HWVoice *pcm_hw_find_specific (HWVoice *hw, int freq,
423 int nchannels, audfmt_e fmt)
425 while ((hw = pcm_hw_find_any_active (hw))) {
426 if (hw->freq == freq &&
427 hw->nchannels == nchannels &&
434 HWVoice *pcm_hw_add (int freq, int nchannels, audfmt_e fmt)
438 if (audio_state.fixed_format) {
439 freq = audio_state.fixed_freq;
440 nchannels = audio_state.fixed_channels;
441 fmt = audio_state.fixed_fmt;
444 hw = pcm_hw_find_specific (NULL, freq, nchannels, fmt);
449 hw = pcm_hw_find_any_passive (NULL);
451 hw->pcm_ops = audio_state.drv->pcm_ops;
455 if (pcm_hw_init (hw, freq, nchannels, fmt)) {
463 return pcm_hw_find_any (NULL);
466 int pcm_hw_add_sw (HWVoice *hw, SWVoice *sw)
468 SWVoice **pvoice = qemu_mallocz ((hw->nb_voices + 1) * sizeof (sw));
472 memcpy (pvoice, hw->pvoice, hw->nb_voices * sizeof (sw));
473 qemu_free (hw->pvoice);
475 hw->pvoice[hw->nb_voices++] = sw;
479 int pcm_hw_del_sw (HWVoice *hw, SWVoice *sw)
482 if (hw->nb_voices > 1) {
483 SWVoice **pvoice = qemu_mallocz ((hw->nb_voices - 1) * sizeof (sw));
486 dolog ("Can not maintain consistent state (not enough memory)\n");
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");
496 if (hw->pvoice[i] != sw)
497 pvoice[j++] = hw->pvoice[i];
499 qemu_free (hw->pvoice);
504 qemu_free (hw->pvoice);
511 SWVoice *pcm_create_voice_pair (int freq, int nchannels, audfmt_e fmt)
516 sw = qemu_mallocz (sizeof (*sw));
520 hw = pcm_hw_add (freq, nchannels, fmt);
524 if (pcm_hw_add_sw (hw, sw))
527 if (pcm_sw_init (sw, hw, freq, nchannels, fmt))
533 pcm_hw_del_sw (hw, sw);
542 SWVoice *AUD_open (SWVoice *sw, const char *name,
543 int freq, int nchannels, audfmt_e fmt)
545 if (!audio_state.drv) {
549 if (sw && freq == sw->freq && sw->nchannels == nchannels && sw->fmt == fmt) {
554 ldebug ("Different format %s %d %d %d\n",
557 sw->nchannels == nchannels,
561 if (nchannels != 1 && nchannels != 2) {
562 dolog ("Bogus channel count %d for voice %s\n", nchannels, name);
566 if (!audio_state.fixed_format && sw) {
568 pcm_hw_del_sw (sw->hw, sw);
571 qemu_free (sw->name);
579 HWVoice *hw = sw->hw;
581 dolog ("Internal logic error voice %s has no hardware store\n",
586 if (pcm_sw_init (sw, hw, freq, nchannels, fmt)) {
588 pcm_hw_del_sw (hw, sw);
591 qemu_free (sw->name);
599 sw = pcm_create_voice_pair (freq, nchannels, fmt);
601 dolog ("Failed to create voice %s\n", name);
607 qemu_free (sw->name);
610 sw->name = qemu_strdup (name);
614 void AUD_close (SWVoice *sw)
620 pcm_hw_del_sw (sw->hw, sw);
623 qemu_free (sw->name);
629 int AUD_write (SWVoice *sw, void *buf, int size)
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);
643 while ((hw = pcm_hw_find_any_active_enabled (hw))) {
645 if (hw->pending_disable && pcm_hw_get_live (hw) <= 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; */
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);
670 int AUD_get_free (SWVoice *sw)
677 free = ((sw->hw->samples - sw->live) << sw->hw->shift) * sw->ratio
680 free &= ~sw->hw->align;
686 int AUD_get_buffer_size (SWVoice *sw)
688 return sw->hw->bufsize;
691 void AUD_adjust (SWVoice *sw, int bytes)
695 sw->old_ticks += (ticks_per_sec * (int64_t) bytes) / sw->bytes_per_second;
698 void AUD_reset (SWVoice *sw)
704 int AUD_calc_elapsed (SWVoice *sw)
706 int64_t now, delta, bytes;
712 now = qemu_get_clock (vm_clock);
713 delta = now - sw->old_ticks;
714 bytes = (delta * sw->bytes_per_second) / ticks_per_sec;
716 dolog ("whoops delta(<0)=%lld\n", delta);
720 dead = sw->hw->samples - sw->live;
721 swlim = ((dead * (int64_t) INT_MAX) / sw->ratio);
731 void AUD_enable (SWVoice *sw, int on)
742 sw->wpos = sw->hw->rpos;
743 if (!sw->old_ticks) {
744 sw->old_ticks = qemu_get_clock (vm_clock);
748 if (sw->active != on) {
750 hw->pending_disable = 0;
753 for (i = 0; i < hw->nb_voices; i++) {
754 ldebug ("resetting voice\n");
756 sw->old_ticks = qemu_get_clock (vm_clock);
758 hw->pcm_ops->ctl (hw, VOICE_ENABLE);
762 if (hw->enabled && !hw->pending_disable) {
764 for (i = 0; i < hw->nb_voices; i++) {
765 nb_active += hw->pvoice[i]->active != 0;
768 if (nb_active == 1) {
769 hw->pending_disable = 1;
777 static struct audio_output_driver *drvtab[] = {
793 static int voice_init (struct audio_output_driver *drv)
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"
800 drv->name, audio_state.nb_hw_voices, drv->max_voices);
801 audio_state.nb_hw_voices = drv->max_voices;
803 hw_voices = qemu_mallocz (audio_state.nb_hw_voices * drv->voice_size);
805 audio_state.drv = drv;
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);
816 dolog ("Could not init `%s' audio\n", drv->name);
821 static void audio_vm_stop_handler (void *opaque, int reason)
825 while ((hw = pcm_hw_find_any (hw))) {
829 hw->pcm_ops->ctl (hw, reason ? VOICE_ENABLE : VOICE_DISABLE);
833 static void audio_atexit (void)
837 while ((hw = pcm_hw_find_any (hw))) {
841 hw->pcm_ops->ctl (hw, VOICE_DISABLE);
842 hw->pcm_ops->fini (hw);
844 audio_state.drv->fini (audio_state.opaque);
847 static void audio_save (QEMUFile *f, void *opaque)
851 static int audio_load (QEMUFile *f, void *opaque, int version_id)
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);
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);
877 drvname = audio_get_conf_str (QC_AUDIO_DRV, NULL);
880 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
881 if (!strcmp (drvname, drvtab[i]->name)) {
882 done = voice_init (drvtab[i]);
888 dolog ("Unknown audio driver `%s'\n", drvname);
892 qemu_add_vm_stop_handler (audio_vm_stop_handler, NULL);
893 atexit (audio_atexit);
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]);
902 audio_state.ticks_threshold = ticks_per_sec / 50;
903 audio_state.freq_threshold = 100;
905 register_savevm ("audio", 0, 1, audio_save, audio_load, NULL);
907 dolog ("Can not initialize audio subsystem\n");
908 voice_init (&no_output_driver);