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
30 #include "audio/audio_int.h"
32 #define dolog(...) AUD_log ("audio", __VA_ARGS__)
34 #define ldebug(...) dolog (__VA_ARGS__)
39 #define QC_AUDIO_DRV "QEMU_AUDIO_DRV"
40 #define QC_VOICES "QEMU_VOICES"
41 #define QC_FIXED_FORMAT "QEMU_FIXED_FORMAT"
42 #define QC_FIXED_FREQ "QEMU_FIXED_FREQ"
44 static HWVoice *hw_voices;
46 AudioState audio_state = {
47 1, /* use fixed settings */
48 44100, /* fixed frequency */
49 2, /* fixed channels */
50 AUD_FMT_S16, /* fixed format */
51 1, /* number of hw voices */
55 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
56 /* http://www.multi-platforms.com/Tips/PopCount.htm */
57 uint32_t popcount (uint32_t u)
59 u = ((u&0x55555555) + ((u>>1)&0x55555555));
60 u = ((u&0x33333333) + ((u>>2)&0x33333333));
61 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
62 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
63 u = ( u&0x0000ffff) + (u>>16);
67 inline uint32_t lsbindex (uint32_t u)
69 return popcount ((u&-u)-1);
72 int audio_get_conf_int (const char *key, int defval)
77 strval = getenv (key);
85 const char *audio_get_conf_str (const char *key, const char *defval)
87 const char *val = getenv (key);
94 void AUD_log (const char *cap, const char *fmt, ...)
97 fprintf (stderr, "%s: ", cap);
99 vfprintf (stderr, fmt, ap);
106 void pcm_sw_free_resources (SWVoice *sw)
108 if (sw->buf) qemu_free (sw->buf);
109 if (sw->rate) st_rate_stop (sw->rate);
114 int pcm_sw_alloc_resources (SWVoice *sw)
116 sw->buf = qemu_mallocz (sw->hw->samples * sizeof (st_sample_t));
120 sw->rate = st_rate_start (sw->freq, sw->hw->freq);
129 void pcm_sw_fini (SWVoice *sw)
131 pcm_sw_free_resources (sw);
134 int pcm_sw_init (SWVoice *sw, HWVoice *hw, int freq,
135 int nchannels, audfmt_e fmt)
137 int bits = 8, sign = 0;
155 sw->nchannels = nchannels;
156 sw->shift = (nchannels == 2) + (bits == 16);
157 sw->align = (1 << sw->shift) - 1;
162 sw->ratio = (sw->hw->freq * ((int64_t) INT_MAX)) / sw->freq;
163 sw->bytes_per_second = sw->freq << sw->shift;
164 sw->conv = mixeng_conv[nchannels == 2][sign][bits == 16];
166 pcm_sw_free_resources (sw);
167 return pcm_sw_alloc_resources (sw);
171 void pcm_hw_free_resources (HWVoice *hw)
174 qemu_free (hw->mix_buf);
178 int pcm_hw_alloc_resources (HWVoice *hw)
180 hw->mix_buf = qemu_mallocz (hw->samples * sizeof (st_sample_t));
187 void pcm_hw_fini (HWVoice *hw)
190 ldebug ("pcm_hw_fini: %d %d %d\n", hw->freq, hw->nchannels, hw->fmt);
191 pcm_hw_free_resources (hw);
192 hw->pcm_ops->fini (hw);
193 memset (hw, 0, audio_state.drv->voice_size);
197 void pcm_hw_gc (HWVoice *hw)
205 int pcm_hw_get_live (HWVoice *hw)
207 int i, alive = 0, live = hw->samples;
209 for (i = 0; i < hw->nb_voices; i++) {
210 if (hw->pvoice[i]->live) {
211 live = audio_MIN (hw->pvoice[i]->live, live);
222 int pcm_hw_get_live2 (HWVoice *hw, int *nb_active)
224 int i, alive = 0, live = hw->samples;
227 for (i = 0; i < hw->nb_voices; i++) {
228 if (hw->pvoice[i]->live) {
229 if (hw->pvoice[i]->live < live) {
230 *nb_active = hw->pvoice[i]->active != 0;
231 live = hw->pvoice[i]->live;
243 void pcm_hw_dec_live (HWVoice *hw, int decr)
247 for (i = 0; i < hw->nb_voices; i++) {
248 if (hw->pvoice[i]->live) {
249 hw->pvoice[i]->live -= decr;
254 void pcm_hw_clear (HWVoice *hw, void *buf, int len)
262 memset (buf, len << hw->shift, 0x00);
266 memset (buf, len << hw->shift, 0x80);
273 int shift = hw->nchannels - 1;
275 for (i = 0; i < len << shift; i++) {
283 int pcm_hw_write (SWVoice *sw, void *buf, int size)
285 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
286 int ret = 0, pos = 0;
290 hwsamples = sw->hw->samples;
291 samples = size >> sw->shift;
294 sw->wpos = sw->hw->rpos;
298 dead = hwsamples - live;
299 swlim = (dead * ((int64_t) INT_MAX)) / sw->ratio;
300 swlim = audio_MIN (swlim, samples);
302 ldebug ("size=%d live=%d dead=%d swlim=%d wpos=%d\n",
303 size, live, dead, swlim, wpos);
305 sw->conv (sw->buf, buf, swlim);
308 dead = hwsamples - live;
309 left = hwsamples - wpos;
310 blck = audio_MIN (dead, left);
312 /* dolog ("swlim=%d\n", swlim); */
317 st_rate_flow (sw->rate, sw->buf + pos, sw->hw->mix_buf + wpos, &isamp, &osamp);
322 wpos = (wpos + osamp) % hwsamples;
327 return ret << sw->shift;
330 int pcm_hw_init (HWVoice *hw, int freq, int nchannels, audfmt_e fmt)
332 int sign = 0, bits = 8;
335 ldebug ("pcm_hw_init: %d %d %d\n", freq, nchannels, fmt);
336 if (hw->pcm_ops->init (hw, freq, nchannels, fmt)) {
337 memset (hw, 0, audio_state.drv->voice_size);
356 hw->shift = (hw->nchannels == 2) + (bits == 16);
357 hw->bytes_per_second = hw->freq << hw->shift;
358 hw->align = (1 << hw->shift) - 1;
359 hw->samples = hw->bufsize >> hw->shift;
360 hw->clip = mixeng_clip[hw->nchannels == 2][sign][bits == 16];
361 if (pcm_hw_alloc_resources (hw)) {
368 static int dist (void *hw)
371 return (((uint8_t *) hw - (uint8_t *) hw_voices)
372 / audio_state.voice_size) + 1;
379 #define ADVANCE(hw) hw ? advance (hw, audio_state.voice_size) : hw_voices
381 HWVoice *pcm_hw_find_any (HWVoice *hw)
384 for (; i < audio_state.nb_hw_voices; i++) {
391 HWVoice *pcm_hw_find_any_active (HWVoice *hw)
394 for (; i < audio_state.nb_hw_voices; i++) {
402 HWVoice *pcm_hw_find_any_active_enabled (HWVoice *hw)
405 for (; i < audio_state.nb_hw_voices; i++) {
407 if (hw->active && hw->enabled)
413 HWVoice *pcm_hw_find_any_passive (HWVoice *hw)
416 for (; i < audio_state.nb_hw_voices; i++) {
424 HWVoice *pcm_hw_find_specific (HWVoice *hw, int freq,
425 int nchannels, audfmt_e fmt)
427 while ((hw = pcm_hw_find_any_active (hw))) {
428 if (hw->freq == freq &&
429 hw->nchannels == nchannels &&
436 HWVoice *pcm_hw_add (int freq, int nchannels, audfmt_e fmt)
440 if (audio_state.fixed_format) {
441 freq = audio_state.fixed_freq;
442 nchannels = audio_state.fixed_channels;
443 fmt = audio_state.fixed_fmt;
446 hw = pcm_hw_find_specific (NULL, freq, nchannels, fmt);
451 hw = pcm_hw_find_any_passive (NULL);
453 hw->pcm_ops = audio_state.drv->pcm_ops;
457 if (pcm_hw_init (hw, freq, nchannels, fmt)) {
465 return pcm_hw_find_any (NULL);
468 int pcm_hw_add_sw (HWVoice *hw, SWVoice *sw)
470 SWVoice **pvoice = qemu_mallocz ((hw->nb_voices + 1) * sizeof (sw));
474 memcpy (pvoice, hw->pvoice, hw->nb_voices * sizeof (sw));
475 qemu_free (hw->pvoice);
477 hw->pvoice[hw->nb_voices++] = sw;
481 int pcm_hw_del_sw (HWVoice *hw, SWVoice *sw)
484 if (hw->nb_voices > 1) {
485 SWVoice **pvoice = qemu_mallocz ((hw->nb_voices - 1) * sizeof (sw));
488 dolog ("Can not maintain consistent state (not enough memory)\n");
492 for (i = 0, j = 0; i < hw->nb_voices; i++) {
493 if (j >= hw->nb_voices - 1) {
494 dolog ("Can not maintain consistent state "
495 "(invariant violated)\n");
498 if (hw->pvoice[i] != sw)
499 pvoice[j++] = hw->pvoice[i];
501 qemu_free (hw->pvoice);
506 qemu_free (hw->pvoice);
513 SWVoice *pcm_create_voice_pair (int freq, int nchannels, audfmt_e fmt)
518 sw = qemu_mallocz (sizeof (*sw));
522 hw = pcm_hw_add (freq, nchannels, fmt);
526 if (pcm_hw_add_sw (hw, sw))
529 if (pcm_sw_init (sw, hw, freq, nchannels, fmt))
535 pcm_hw_del_sw (hw, sw);
544 SWVoice *AUD_open (SWVoice *sw, const char *name,
545 int freq, int nchannels, audfmt_e fmt)
547 if (!audio_state.drv) {
551 if (sw && freq == sw->freq && sw->nchannels == nchannels && sw->fmt == fmt) {
556 ldebug ("Different format %s %d %d %d\n",
559 sw->nchannels == nchannels,
563 if (nchannels != 1 && nchannels != 2) {
564 dolog ("Bogus channel count %d for voice %s\n", nchannels, name);
568 if (!audio_state.fixed_format && sw) {
570 pcm_hw_del_sw (sw->hw, sw);
573 qemu_free (sw->name);
581 HWVoice *hw = sw->hw;
583 dolog ("Internal logic error voice %s has no hardware store\n",
588 if (pcm_sw_init (sw, hw, freq, nchannels, fmt)) {
590 pcm_hw_del_sw (hw, sw);
593 qemu_free (sw->name);
601 sw = pcm_create_voice_pair (freq, nchannels, fmt);
603 dolog ("Failed to create voice %s\n", name);
609 qemu_free (sw->name);
612 sw->name = qemu_strdup (name);
616 void AUD_close (SWVoice *sw)
622 pcm_hw_del_sw (sw->hw, sw);
625 qemu_free (sw->name);
631 int AUD_write (SWVoice *sw, void *buf, int size)
635 if (!sw->hw->enabled)
636 dolog ("Writing to disabled voice %s\n", sw->name);
637 bytes = sw->hw->pcm_ops->write (sw, buf, size);
645 while ((hw = pcm_hw_find_any_active_enabled (hw))) {
647 if (hw->pending_disable && pcm_hw_get_live (hw) <= 0) {
649 hw->pcm_ops->ctl (hw, VOICE_DISABLE);
650 for (i = 0; i < hw->nb_voices; i++) {
651 hw->pvoice[i]->live = 0;
652 /* hw->pvoice[i]->old_ticks = 0; */
657 hw->pcm_ops->run (hw);
658 assert (hw->rpos < hw->samples);
659 for (i = 0; i < hw->nb_voices; i++) {
660 SWVoice *sw = hw->pvoice[i];
661 if (!sw->active && !sw->live && sw->old_ticks) {
662 int64_t delta = qemu_get_clock (vm_clock) - sw->old_ticks;
663 if (delta > audio_state.ticks_threshold) {
664 ldebug ("resetting old_ticks for %s\n", sw->name);
672 int AUD_get_free (SWVoice *sw)
679 free = ((sw->hw->samples - sw->live) << sw->hw->shift) * sw->ratio
682 free &= ~sw->hw->align;
688 int AUD_get_buffer_size (SWVoice *sw)
690 return sw->hw->bufsize;
693 void AUD_adjust (SWVoice *sw, int bytes)
697 sw->old_ticks += (ticks_per_sec * (int64_t) bytes) / sw->bytes_per_second;
700 void AUD_reset (SWVoice *sw)
706 int AUD_calc_elapsed (SWVoice *sw)
708 int64_t now, delta, bytes;
714 now = qemu_get_clock (vm_clock);
715 delta = now - sw->old_ticks;
716 bytes = (delta * sw->bytes_per_second) / ticks_per_sec;
718 dolog ("whoops delta(<0)=%lld\n", delta);
722 dead = sw->hw->samples - sw->live;
723 swlim = ((dead * (int64_t) INT_MAX) / sw->ratio);
733 void AUD_enable (SWVoice *sw, int on)
744 sw->wpos = sw->hw->rpos;
745 if (!sw->old_ticks) {
746 sw->old_ticks = qemu_get_clock (vm_clock);
750 if (sw->active != on) {
752 hw->pending_disable = 0;
755 for (i = 0; i < hw->nb_voices; i++) {
756 ldebug ("resetting voice\n");
758 sw->old_ticks = qemu_get_clock (vm_clock);
760 hw->pcm_ops->ctl (hw, VOICE_ENABLE);
764 if (hw->enabled && !hw->pending_disable) {
766 for (i = 0; i < hw->nb_voices; i++) {
767 nb_active += hw->pvoice[i]->active != 0;
770 if (nb_active == 1) {
771 hw->pending_disable = 1;
779 static struct audio_output_driver *drvtab[] = {
783 #ifdef USE_FMOD_AUDIO
794 static int voice_init (struct audio_output_driver *drv)
796 audio_state.opaque = drv->init ();
797 if (audio_state.opaque) {
798 if (audio_state.nb_hw_voices > drv->max_voices) {
799 dolog ("`%s' does not support %d multiple hardware channels\n"
801 drv->name, audio_state.nb_hw_voices, drv->max_voices);
802 audio_state.nb_hw_voices = drv->max_voices;
804 hw_voices = qemu_mallocz (audio_state.nb_hw_voices * drv->voice_size);
806 audio_state.drv = drv;
810 dolog ("Not enough memory for %d `%s' voices (each %d bytes)\n",
811 audio_state.nb_hw_voices, drv->name, drv->voice_size);
812 drv->fini (audio_state.opaque);
817 dolog ("Could not init `%s' audio\n", drv->name);
822 static void audio_vm_stop_handler (void *opaque, int reason)
826 while ((hw = pcm_hw_find_any (hw))) {
830 hw->pcm_ops->ctl (hw, reason ? VOICE_ENABLE : VOICE_DISABLE);
834 static void audio_atexit (void)
838 while ((hw = pcm_hw_find_any (hw))) {
842 hw->pcm_ops->ctl (hw, VOICE_DISABLE);
843 hw->pcm_ops->fini (hw);
845 audio_state.drv->fini (audio_state.opaque);
848 static void audio_save (QEMUFile *f, void *opaque)
852 static int audio_load (QEMUFile *f, void *opaque, int version_id)
866 audio_state.fixed_format =
867 !!audio_get_conf_int (QC_FIXED_FORMAT, audio_state.fixed_format);
868 audio_state.fixed_freq =
869 audio_get_conf_int (QC_FIXED_FREQ, audio_state.fixed_freq);
870 audio_state.nb_hw_voices =
871 audio_get_conf_int (QC_VOICES, audio_state.nb_hw_voices);
873 if (audio_state.nb_hw_voices <= 0) {
874 dolog ("Bogus number of voices %d, resetting to 1\n",
875 audio_state.nb_hw_voices);
878 drvname = audio_get_conf_str (QC_AUDIO_DRV, NULL);
881 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
882 if (!strcmp (drvname, drvtab[i]->name)) {
883 done = voice_init (drvtab[i]);
889 dolog ("Unknown audio driver `%s'\n", drvname);
893 qemu_add_vm_stop_handler (audio_vm_stop_handler, NULL);
894 atexit (audio_atexit);
897 for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
898 if (drvtab[i]->can_be_default)
899 done = voice_init (drvtab[i]);
903 audio_state.ticks_threshold = ticks_per_sec / 50;
904 audio_state.freq_threshold = 100;
906 register_savevm ("audio", 0, 1, audio_save, audio_load, NULL);
908 dolog ("Can not initialize audio subsystem\n");