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
28
#define AUDIO_CAP "audio"
29
#include "audio/audio.h"
34
#if defined __linux__ || (defined _BSD && !defined __APPLE__)
39
#include "audio/ossaudio.h"
43
#include "audio/sdlaudio.h"
47
#include "audio/wavaudio.h"
51
#include "audio/fmodaudio.h"
54
#define QC_AUDIO_DRV "QEMU_AUDIO_DRV"
55
#define QC_VOICES "QEMU_VOICES"
56
#define QC_FIXED_FORMAT "QEMU_FIXED_FORMAT"
57
#define QC_FIXED_FREQ "QEMU_FIXED_FREQ"
59
extern void SB16_init (void);
62
extern void Adlib_init (void);
66
extern void GUS_init (void);
69
static void (*hw_ctors[]) (void) = {
80
static HWVoice *hw_voice;
82
AudioState audio_state = {
83
1, /* use fixed settings */
84
44100, /* fixed frequency */
85
2, /* fixed channels */
86
AUD_FMT_S16, /* fixed format */
87
1, /* number of hw voices */
91
/* http://www.df.lth.se/~john_e/gems/gem002d.html */
92
/* http://www.multi-platforms.com/Tips/PopCount.htm */
93
uint32_t popcount (uint32_t u)
95
u = ((u&0x55555555) + ((u>>1)&0x55555555));
96
u = ((u&0x33333333) + ((u>>2)&0x33333333));
97
u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
98
u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
99
u = ( u&0x0000ffff) + (u>>16);
103
inline uint32_t lsbindex (uint32_t u)
105
return popcount ((u&-u)-1);
108
int audio_get_conf_int (const char *key, int defval)
113
strval = getenv (key);
121
const char *audio_get_conf_str (const char *key, const char *defval)
123
const char *val = getenv (key);
130
void audio_log (const char *fmt, ...)
134
vfprintf (stderr, fmt, ap);
141
void pcm_sw_free_resources (SWVoice *sw)
143
if (sw->buf) qemu_free (sw->buf);
144
if (sw->rate) st_rate_stop (sw->rate);
149
int pcm_sw_alloc_resources (SWVoice *sw)
151
sw->buf = qemu_mallocz (sw->hw->samples * sizeof (st_sample_t));
155
sw->rate = st_rate_start (sw->freq, sw->hw->freq);
164
void pcm_sw_fini (SWVoice *sw)
166
pcm_sw_free_resources (sw);
169
int pcm_sw_init (SWVoice *sw, HWVoice *hw, int freq,
170
int nchannels, audfmt_e fmt)
172
int bits = 8, sign = 0;
190
sw->nchannels = nchannels;
191
sw->shift = (nchannels == 2) + (bits == 16);
192
sw->align = (1 << sw->shift) - 1;
197
sw->ratio = (sw->hw->freq * ((int64_t) INT_MAX)) / sw->freq;
198
sw->bytes_per_second = sw->freq << sw->shift;
199
sw->conv = mixeng_conv[nchannels == 2][sign][bits == 16];
201
pcm_sw_free_resources (sw);
202
return pcm_sw_alloc_resources (sw);
206
void pcm_hw_free_resources (HWVoice *hw)
209
qemu_free (hw->mix_buf);
213
int pcm_hw_alloc_resources (HWVoice *hw)
215
hw->mix_buf = qemu_mallocz (hw->samples * sizeof (st_sample_t));
222
void pcm_hw_fini (HWVoice *hw)
225
ldebug ("pcm_hw_fini: %d %d %d\n", hw->freq, hw->nchannels, hw->fmt);
226
pcm_hw_free_resources (hw);
227
hw->pcm_ops->fini (hw);
228
memset (hw, 0, audio_state.drv->voice_size);
232
void pcm_hw_gc (HWVoice *hw)
240
int pcm_hw_get_live (HWVoice *hw)
242
int i, alive = 0, live = hw->samples;
244
for (i = 0; i < hw->nb_voices; i++) {
245
if (hw->pvoice[i]->live) {
246
live = audio_MIN (hw->pvoice[i]->live, live);
257
int pcm_hw_get_live2 (HWVoice *hw, int *nb_active)
259
int i, alive = 0, live = hw->samples;
262
for (i = 0; i < hw->nb_voices; i++) {
263
if (hw->pvoice[i]->live) {
264
if (hw->pvoice[i]->live < live) {
265
*nb_active = hw->pvoice[i]->active != 0;
266
live = hw->pvoice[i]->live;
278
void pcm_hw_dec_live (HWVoice *hw, int decr)
282
for (i = 0; i < hw->nb_voices; i++) {
283
if (hw->pvoice[i]->live) {
284
hw->pvoice[i]->live -= decr;
289
void pcm_hw_clear (HWVoice *hw, void *buf, int len)
297
memset (buf, len << hw->shift, 0x00);
301
memset (buf, len << hw->shift, 0x80);
308
int shift = hw->nchannels - 1;
310
for (i = 0; i < len << shift; i++) {
318
int pcm_hw_write (SWVoice *sw, void *buf, int size)
320
int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
321
int ret = 0, pos = 0;
325
hwsamples = sw->hw->samples;
326
samples = size >> sw->shift;
329
sw->wpos = sw->hw->rpos;
333
dead = hwsamples - live;
334
swlim = (dead * ((int64_t) INT_MAX)) / sw->ratio;
335
swlim = audio_MIN (swlim, samples);
337
ldebug ("size=%d live=%d dead=%d swlim=%d wpos=%d\n",
338
size, live, dead, swlim, wpos);
340
sw->conv (sw->buf, buf, swlim);
343
dead = hwsamples - live;
344
left = hwsamples - wpos;
345
blck = audio_MIN (dead, left);
347
/* dolog ("swlim=%d\n", swlim); */
352
st_rate_flow (sw->rate, sw->buf + pos, sw->hw->mix_buf + wpos, &isamp, &osamp);
357
wpos = (wpos + osamp) % hwsamples;
362
return ret << sw->shift;
365
int pcm_hw_init (HWVoice *hw, int freq, int nchannels, audfmt_e fmt)
367
int sign = 0, bits = 8;
370
ldebug ("pcm_hw_init: %d %d %d\n", freq, nchannels, fmt);
371
if (hw->pcm_ops->init (hw, freq, nchannels, fmt)) {
372
memset (hw, 0, audio_state.drv->voice_size);
391
hw->shift = (hw->nchannels == 2) + (bits == 16);
392
hw->bytes_per_second = hw->freq << hw->shift;
393
hw->align = (1 << hw->shift) - 1;
394
hw->samples = hw->bufsize >> hw->shift;
395
hw->clip = mixeng_clip[hw->nchannels == 2][sign][bits == 16];
396
if (pcm_hw_alloc_resources (hw)) {
403
static int dist (void *hw)
406
return (((uint8_t *) hw - (uint8_t *) hw_voice)
407
/ audio_state.voice_size) + 1;
414
#define ADVANCE(hw) hw ? advance (hw, audio_state.voice_size) : hw_voice
416
HWVoice *pcm_hw_find_any (HWVoice *hw)
419
for (; i < audio_state.nb_hw_voices; i++) {
426
HWVoice *pcm_hw_find_any_active (HWVoice *hw)
429
for (; i < audio_state.nb_hw_voices; i++) {
437
HWVoice *pcm_hw_find_any_active_enabled (HWVoice *hw)
440
for (; i < audio_state.nb_hw_voices; i++) {
442
if (hw->active && hw->enabled)
448
HWVoice *pcm_hw_find_any_passive (HWVoice *hw)
451
for (; i < audio_state.nb_hw_voices; i++) {
459
HWVoice *pcm_hw_find_specific (HWVoice *hw, int freq,
460
int nchannels, audfmt_e fmt)
462
while ((hw = pcm_hw_find_any_active (hw))) {
463
if (hw->freq == freq &&
464
hw->nchannels == nchannels &&
471
HWVoice *pcm_hw_add (int freq, int nchannels, audfmt_e fmt)
475
if (audio_state.fixed_format) {
476
freq = audio_state.fixed_freq;
477
nchannels = audio_state.fixed_channels;
478
fmt = audio_state.fixed_fmt;
481
hw = pcm_hw_find_specific (NULL, freq, nchannels, fmt);
486
hw = pcm_hw_find_any_passive (NULL);
488
hw->pcm_ops = audio_state.drv->pcm_ops;
492
if (pcm_hw_init (hw, freq, nchannels, fmt)) {
500
return pcm_hw_find_any (NULL);
503
int pcm_hw_add_sw (HWVoice *hw, SWVoice *sw)
505
SWVoice **pvoice = qemu_mallocz ((hw->nb_voices + 1) * sizeof (sw));
509
memcpy (pvoice, hw->pvoice, hw->nb_voices * sizeof (sw));
510
qemu_free (hw->pvoice);
512
hw->pvoice[hw->nb_voices++] = sw;
516
int pcm_hw_del_sw (HWVoice *hw, SWVoice *sw)
519
if (hw->nb_voices > 1) {
520
SWVoice **pvoice = qemu_mallocz ((hw->nb_voices - 1) * sizeof (sw));
523
dolog ("Can not maintain consistent state (not enough memory)\n");
527
for (i = 0, j = 0; i < hw->nb_voices; i++) {
528
if (j >= hw->nb_voices - 1) {
529
dolog ("Can not maintain consistent state "
530
"(invariant violated)\n");
533
if (hw->pvoice[i] != sw)
534
pvoice[j++] = hw->pvoice[i];
536
qemu_free (hw->pvoice);
541
qemu_free (hw->pvoice);
548
SWVoice *pcm_create_voice_pair (int freq, int nchannels, audfmt_e fmt)
553
sw = qemu_mallocz (sizeof (*sw));
557
hw = pcm_hw_add (freq, nchannels, fmt);
561
if (pcm_hw_add_sw (hw, sw))
564
if (pcm_sw_init (sw, hw, freq, nchannels, fmt))
570
pcm_hw_del_sw (hw, sw);
579
SWVoice *AUD_open (SWVoice *sw, const char *name,
580
int freq, int nchannels, audfmt_e fmt)
582
if (!audio_state.drv) {
586
if (sw && freq == sw->freq && sw->nchannels == nchannels && sw->fmt == fmt) {
591
ldebug ("Different format %s %d %d %d\n",
594
sw->nchannels == nchannels,
598
if (nchannels != 1 && nchannels != 2) {
599
dolog ("Bogus channel count %d for voice %s\n", nchannels, name);
603
if (!audio_state.fixed_format && sw) {
605
pcm_hw_del_sw (sw->hw, sw);
608
qemu_free (sw->name);
616
HWVoice *hw = sw->hw;
618
dolog ("Internal logic error voice %s has no hardware store\n",
623
if (pcm_sw_init (sw, hw, freq, nchannels, fmt)) {
625
pcm_hw_del_sw (hw, sw);
628
qemu_free (sw->name);
636
sw = pcm_create_voice_pair (freq, nchannels, fmt);
638
dolog ("Failed to create voice %s\n", name);
644
qemu_free (sw->name);
647
sw->name = qemu_strdup (name);
651
int AUD_write (SWVoice *sw, void *buf, int size)
655
if (!sw->hw->enabled)
656
dolog ("Writing to disabled voice %s\n", sw->name);
657
bytes = sw->hw->pcm_ops->write (sw, buf, size);
665
while ((hw = pcm_hw_find_any_active_enabled (hw))) {
667
if (hw->pending_disable && pcm_hw_get_live (hw) <= 0) {
669
hw->pcm_ops->ctl (hw, VOICE_DISABLE);
670
for (i = 0; i < hw->nb_voices; i++) {
671
hw->pvoice[i]->live = 0;
672
/* hw->pvoice[i]->old_ticks = 0; */
677
hw->pcm_ops->run (hw);
678
assert (hw->rpos < hw->samples);
679
for (i = 0; i < hw->nb_voices; i++) {
680
SWVoice *sw = hw->pvoice[i];
681
if (!sw->active && !sw->live && sw->old_ticks) {
682
int64_t delta = qemu_get_clock (vm_clock) - sw->old_ticks;
683
if (delta > audio_state.ticks_threshold) {
684
ldebug ("resetting old_ticks for %s\n", sw->name);
692
int AUD_get_free (SWVoice *sw)
699
free = ((sw->hw->samples - sw->live) << sw->hw->shift) * sw->ratio
702
free &= ~sw->hw->align;
708
int AUD_get_buffer_size (SWVoice *sw)
710
return sw->hw->bufsize;
713
void AUD_adjust (SWVoice *sw, int bytes)
717
sw->old_ticks += (ticks_per_sec * (int64_t) bytes) / sw->bytes_per_second;
720
void AUD_reset (SWVoice *sw)
726
int AUD_calc_elapsed (SWVoice *sw)
728
int64_t now, delta, bytes;
734
now = qemu_get_clock (vm_clock);
735
delta = now - sw->old_ticks;
736
bytes = (delta * sw->bytes_per_second) / ticks_per_sec;
738
dolog ("whoops delta(<0)=%lld\n", delta);
742
dead = sw->hw->samples - sw->live;
743
swlim = ((dead * (int64_t) INT_MAX) / sw->ratio);
753
void AUD_enable (SWVoice *sw, int on)
764
sw->wpos = sw->hw->rpos;
765
if (!sw->old_ticks) {
766
sw->old_ticks = qemu_get_clock (vm_clock);
770
if (sw->active != on) {
772
hw->pending_disable = 0;
775
for (i = 0; i < hw->nb_voices; i++) {
776
ldebug ("resetting voice\n");
778
sw->old_ticks = qemu_get_clock (vm_clock);
780
hw->pcm_ops->ctl (hw, VOICE_ENABLE);
784
if (hw->enabled && !hw->pending_disable) {
786
for (i = 0; i < hw->nb_voices; i++) {
787
nb_active += hw->pvoice[i]->active != 0;
790
if (nb_active == 1) {
791
hw->pending_disable = 1;
799
static struct audio_output_driver *drvtab[] = {
803
#ifdef USE_FMOD_AUDIO
814
static int voice_init (struct audio_output_driver *drv)
816
audio_state.opaque = drv->init ();
817
if (audio_state.opaque) {
818
if (audio_state.nb_hw_voices > drv->max_voices) {
819
dolog ("`%s' does not support %d multiple hardware channels\n"
821
drv->name, audio_state.nb_hw_voices, drv->max_voices);
822
audio_state.nb_hw_voices = drv->max_voices;
824
hw_voice = qemu_mallocz (audio_state.nb_hw_voices * drv->voice_size);
826
audio_state.drv = drv;
830
dolog ("Not enough memory for %d `%s' voices (each %d bytes)\n",
831
audio_state.nb_hw_voices, drv->name, drv->voice_size);
832
drv->fini (audio_state.opaque);
837
dolog ("Could not init `%s' audio\n", drv->name);
842
static void audio_vm_stop_handler (void *opaque, int reason)
846
while ((hw = pcm_hw_find_any (hw))) {
850
hw->pcm_ops->ctl (hw, reason ? VOICE_ENABLE : VOICE_DISABLE);
854
static void audio_atexit (void)
858
while ((hw = pcm_hw_find_any (hw))) {
862
hw->pcm_ops->ctl (hw, VOICE_DISABLE);
863
hw->pcm_ops->fini (hw);
865
audio_state.drv->fini (audio_state.opaque);
868
static void audio_save (QEMUFile *f, void *opaque)
872
static int audio_load (QEMUFile *f, void *opaque, int version_id)
886
audio_state.fixed_format =
887
!!audio_get_conf_int (QC_FIXED_FORMAT, audio_state.fixed_format);
888
audio_state.fixed_freq =
889
audio_get_conf_int (QC_FIXED_FREQ, audio_state.fixed_freq);
890
audio_state.nb_hw_voices =
891
audio_get_conf_int (QC_VOICES, audio_state.nb_hw_voices);
893
if (audio_state.nb_hw_voices <= 0) {
894
dolog ("Bogus number of voices %d, resetting to 1\n",
895
audio_state.nb_hw_voices);
898
drvname = audio_get_conf_str (QC_AUDIO_DRV, NULL);
901
for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
902
if (!strcmp (drvname, drvtab[i]->name)) {
903
done = voice_init (drvtab[i]);
909
dolog ("Unknown audio driver `%s'\n", drvname);
913
qemu_add_vm_stop_handler (audio_vm_stop_handler, NULL);
914
atexit (audio_atexit);
917
for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
918
if (drvtab[i]->can_be_default)
919
done = voice_init (drvtab[i]);
923
audio_state.ticks_threshold = ticks_per_sec / 50;
924
audio_state.freq_threshold = 100;
926
register_savevm ("audio", 0, 1, audio_save, audio_load, NULL);
928
dolog ("Can not initialize audio subsystem\n");
932
for (i = 0; hw_ctors[i]; i++) {