2 * QEMU FMOD audio driver
4 * Copyright (c) 2004-2005 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
25 #include <fmod_errors.h>
28 #define AUDIO_CAP "fmod"
29 #include "audio_int.h"
31 typedef struct FMODVoiceOut {
34 FSOUND_SAMPLE *fmod_sample;
38 typedef struct FMODVoiceIn {
40 FSOUND_SAMPLE *fmod_sample;
61 static void GCC_FMT_ATTR (1, 2) fmod_logerr (const char *fmt, ...)
66 AUD_vlog (AUDIO_CAP, fmt, ap);
69 AUD_log (AUDIO_CAP, "Reason: %s\n",
70 FMOD_ErrorString (FSOUND_GetError ()));
73 static void GCC_FMT_ATTR (2, 3) fmod_logerr2 (
81 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
84 AUD_vlog (AUDIO_CAP, fmt, ap);
87 AUD_log (AUDIO_CAP, "Reason: %s\n",
88 FMOD_ErrorString (FSOUND_GetError ()));
91 static int fmod_write (SWVoiceOut *sw, void *buf, int len)
93 return audio_pcm_sw_write (sw, buf, len);
96 static void fmod_clear_sample (FMODVoiceOut *fmd)
98 HWVoiceOut *hw = &fmd->hw;
100 void *p1 = 0, *p2 = 0;
101 unsigned int len1 = 0, len2 = 0;
103 status = FSOUND_Sample_Lock (
106 hw->samples << hw->info.shift,
114 fmod_logerr ("Failed to lock sample\n");
118 if ((len1 & hw->info.align) || (len2 & hw->info.align)) {
119 dolog ("Lock returned misaligned length %d, %d, alignment %d\n",
120 len1, len2, hw->info.align + 1);
124 if ((len1 + len2) - (hw->samples << hw->info.shift)) {
125 dolog ("Lock returned incomplete length %d, %d\n",
126 len1 + len2, hw->samples << hw->info.shift);
130 audio_pcm_info_clear_buf (&hw->info, p1, hw->samples);
133 status = FSOUND_Sample_Unlock (fmd->fmod_sample, p1, p2, len1, len2);
135 fmod_logerr ("Failed to unlock sample\n");
139 static void fmod_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
141 int src_len1 = dst_len;
143 int pos = hw->rpos + dst_len;
144 st_sample_t *src1 = hw->mix_buf + hw->rpos;
145 st_sample_t *src2 = NULL;
147 if (pos > hw->samples) {
148 src_len1 = hw->samples - hw->rpos;
150 src_len2 = dst_len - src_len1;
155 hw->clip (dst, src1, src_len1);
159 dst = advance (dst, src_len1 << hw->info.shift);
160 hw->clip (dst, src2, src_len2);
163 hw->rpos = pos % hw->samples;
166 static int fmod_unlock_sample (FSOUND_SAMPLE *sample, void *p1, void *p2,
167 unsigned int blen1, unsigned int blen2)
169 int status = FSOUND_Sample_Unlock (sample, p1, p2, blen1, blen2);
171 fmod_logerr ("Failed to unlock sample\n");
177 static int fmod_lock_sample (
178 FSOUND_SAMPLE *sample,
179 struct audio_pcm_info *info,
190 status = FSOUND_Sample_Lock (
201 fmod_logerr ("Failed to lock sample\n");
205 if ((*blen1 & info->align) || (*blen2 & info->align)) {
206 dolog ("Lock returned misaligned length %d, %d, alignment %d\n",
207 *blen1, *blen2, info->align + 1);
209 fmod_unlock_sample (sample, *p1, *p2, *blen1, *blen2);
218 if (!*p1 && *blen1) {
219 dolog ("warning: !p1 && blen1=%d\n", *blen1);
224 dolog ("warning: !p2 && blen2=%d\n", *blen2);
231 static int fmod_run_out (HWVoiceOut *hw)
233 FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
235 void *p1 = 0, *p2 = 0;
236 unsigned int blen1 = 0, blen2 = 0;
237 unsigned int len1 = 0, len2 = 0;
240 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
245 if (!hw->pending_disable
247 && (conf.threshold && live <= conf.threshold)) {
248 ldebug ("live=%d nb_live=%d\n", live, nb_live);
254 if (fmd->channel >= 0) {
256 int old_pos = fmd->old_pos;
257 int ppos = FSOUND_GetCurrentPosition (fmd->channel);
259 if (ppos == old_pos || !ppos) {
263 if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
264 len = ppos - old_pos;
267 if ((old_pos > ppos) && ((old_pos + len) > (ppos + hw->samples))) {
268 len = hw->samples - old_pos + ppos;
273 if (audio_bug (AUDIO_FUNC, decr < 0)) {
274 dolog ("decr=%d live=%d ppos=%d old_pos=%d len=%d\n",
275 decr, live, ppos, old_pos, len);
285 if (fmod_lock_sample (fmd->fmod_sample, &fmd->hw.info,
292 len1 = blen1 >> hw->info.shift;
293 len2 = blen2 >> hw->info.shift;
294 ldebug ("%p %p %d %d %d %d\n", p1, p2, len1, len2, blen1, blen2);
298 fmod_write_sample (hw, p1, len1);
302 fmod_write_sample (hw, p2, len2);
305 fmod_unlock_sample (fmd->fmod_sample, p1, p2, blen1, blen2);
307 fmd->old_pos = (fmd->old_pos + decr) % hw->samples;
311 static int aud_to_fmodfmt (audfmt_e fmt, int stereo)
313 int mode = FSOUND_LOOP_NORMAL;
317 mode |= FSOUND_SIGNED | FSOUND_8BITS;
321 mode |= FSOUND_UNSIGNED | FSOUND_8BITS;
325 mode |= FSOUND_SIGNED | FSOUND_16BITS;
329 mode |= FSOUND_UNSIGNED | FSOUND_16BITS;
333 dolog ("Internal logic error: Bad audio format %d\n", fmt);
337 mode |= FSOUND_8BITS;
339 mode |= stereo ? FSOUND_STEREO : FSOUND_MONO;
343 static void fmod_fini_out (HWVoiceOut *hw)
345 FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
347 if (fmd->fmod_sample) {
348 FSOUND_Sample_Free (fmd->fmod_sample);
349 fmd->fmod_sample = 0;
351 if (fmd->channel >= 0) {
352 FSOUND_StopSound (fmd->channel);
357 static int fmod_init_out (HWVoiceOut *hw, audsettings_t *as)
359 int bits16, mode, channel;
360 FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
361 audsettings_t obt_as = *as;
363 mode = aud_to_fmodfmt (as->fmt, as->nchannels == 2 ? 1 : 0);
364 fmd->fmod_sample = FSOUND_Sample_Alloc (
365 FSOUND_FREE, /* index */
366 conf.nb_samples, /* length */
374 if (!fmd->fmod_sample) {
375 fmod_logerr2 ("DAC", "Failed to allocate FMOD sample\n");
379 channel = FSOUND_PlaySoundEx (FSOUND_FREE, fmd->fmod_sample, 0, 1);
381 fmod_logerr2 ("DAC", "Failed to start playing sound\n");
382 FSOUND_Sample_Free (fmd->fmod_sample);
385 fmd->channel = channel;
387 /* FMOD always operates on little endian frames? */
388 obt_as.endianness = 0;
389 audio_pcm_init_info (&hw->info, &obt_as);
390 bits16 = (mode & FSOUND_16BITS) != 0;
391 hw->samples = conf.nb_samples;
395 static int fmod_ctl_out (HWVoiceOut *hw, int cmd, ...)
398 FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
402 fmod_clear_sample (fmd);
403 status = FSOUND_SetPaused (fmd->channel, 0);
405 fmod_logerr ("Failed to resume channel %d\n", fmd->channel);
410 status = FSOUND_SetPaused (fmd->channel, 1);
412 fmod_logerr ("Failed to pause channel %d\n", fmd->channel);
419 static int fmod_init_in (HWVoiceIn *hw, audsettings_t *as)
422 FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
423 audsettings_t obt_as = *as;
425 if (conf.broken_adc) {
429 mode = aud_to_fmodfmt (as->fmt, as->nchannels == 2 ? 1 : 0);
430 fmd->fmod_sample = FSOUND_Sample_Alloc (
431 FSOUND_FREE, /* index */
432 conf.nb_samples, /* length */
440 if (!fmd->fmod_sample) {
441 fmod_logerr2 ("ADC", "Failed to allocate FMOD sample\n");
445 /* FMOD always operates on little endian frames? */
446 obt_as.endianness = 0;
447 audio_pcm_init_info (&hw->info, &obt_as);
448 bits16 = (mode & FSOUND_16BITS) != 0;
449 hw->samples = conf.nb_samples;
453 static void fmod_fini_in (HWVoiceIn *hw)
455 FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
457 if (fmd->fmod_sample) {
458 FSOUND_Record_Stop ();
459 FSOUND_Sample_Free (fmd->fmod_sample);
460 fmd->fmod_sample = 0;
464 static int fmod_run_in (HWVoiceIn *hw)
466 FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
467 int hwshift = hw->info.shift;
468 int live, dead, new_pos, len;
469 unsigned int blen1 = 0, blen2 = 0;
470 unsigned int len1, len2;
474 live = audio_pcm_hw_get_live_in (hw);
475 dead = hw->samples - live;
480 new_pos = FSOUND_Record_GetPosition ();
482 fmod_logerr ("Could not get recording position\n");
486 len = audio_ring_dist (new_pos, hw->wpos, hw->samples);
490 len = audio_MIN (len, dead);
492 if (fmod_lock_sample (fmd->fmod_sample, &fmd->hw.info,
499 len1 = blen1 >> hwshift;
500 len2 = blen2 >> hwshift;
504 hw->conv (hw->conv_buf + hw->wpos, p1, len1, &nominal_volume);
507 hw->conv (hw->conv_buf, p2, len2, &nominal_volume);
510 fmod_unlock_sample (fmd->fmod_sample, p1, p2, blen1, blen2);
511 hw->wpos = (hw->wpos + decr) % hw->samples;
519 {"none", FSOUND_OUTPUT_NOSOUND},
521 {"winmm", FSOUND_OUTPUT_WINMM},
522 {"dsound", FSOUND_OUTPUT_DSOUND},
523 {"a3d", FSOUND_OUTPUT_A3D},
524 {"asio", FSOUND_OUTPUT_ASIO},
527 {"oss", FSOUND_OUTPUT_OSS},
528 {"alsa", FSOUND_OUTPUT_ALSA},
529 {"esd", FSOUND_OUTPUT_ESD},
532 {"mac", FSOUND_OUTPUT_MAC},
535 {"xbox", FSOUND_OUTPUT_XBOX},
536 {"ps2", FSOUND_OUTPUT_PS2},
537 {"gcube", FSOUND_OUTPUT_GC},
539 {"none-realtime", FSOUND_OUTPUT_NOSOUND_NONREALTIME}
542 static void *fmod_audio_init (void)
547 int output_type = -1;
548 const char *drv = conf.drvname;
550 ver = FSOUND_GetVersion ();
551 if (ver < FMOD_VERSION) {
552 dolog ("Wrong FMOD version %f, need at least %f\n", ver, FMOD_VERSION);
558 dolog ("FMOD before 3.75 has bug preventing ADC from working\n"
559 "ADC will be disabled.\n");
566 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
567 if (!strcmp (drv, drvtab[i].name)) {
568 output_type = drvtab[i].type;
574 dolog ("Unknown FMOD driver `%s'\n", drv);
575 dolog ("Valid drivers:\n");
576 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
577 dolog (" %s\n", drvtab[i].name);
582 if (output_type != -1) {
583 status = FSOUND_SetOutput (output_type);
585 fmod_logerr ("FSOUND_SetOutput(%d) failed\n", output_type);
591 status = FSOUND_SetBufferSize (conf.bufsize);
593 fmod_logerr ("FSOUND_SetBufferSize (%d) failed\n", conf.bufsize);
597 status = FSOUND_Init (conf.freq, conf.nb_channels, 0);
599 fmod_logerr ("FSOUND_Init failed\n");
606 static int fmod_read (SWVoiceIn *sw, void *buf, int size)
608 return audio_pcm_sw_read (sw, buf, size);
611 static int fmod_ctl_in (HWVoiceIn *hw, int cmd, ...)
614 FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
618 status = FSOUND_Record_StartSample (fmd->fmod_sample, 1);
620 fmod_logerr ("Failed to start recording\n");
625 status = FSOUND_Record_Stop ();
627 fmod_logerr ("Failed to stop recording\n");
634 static void fmod_audio_fini (void *opaque)
640 static struct audio_option fmod_options[] = {
641 {"DRV", AUD_OPT_STR, &conf.drvname,
642 "FMOD driver", NULL, 0},
643 {"FREQ", AUD_OPT_INT, &conf.freq,
644 "Default frequency", NULL, 0},
645 {"SAMPLES", AUD_OPT_INT, &conf.nb_samples,
646 "Buffer size in samples", NULL, 0},
647 {"CHANNELS", AUD_OPT_INT, &conf.nb_channels,
648 "Number of default channels (1 - mono, 2 - stereo)", NULL, 0},
649 {"BUFSIZE", AUD_OPT_INT, &conf.bufsize,
650 "(undocumented)", NULL, 0},
652 {"THRESHOLD", AUD_OPT_INT, &conf.threshold,
656 {NULL, 0, NULL, NULL, NULL, 0}
659 static struct audio_pcm_ops fmod_pcm_ops = {
673 struct audio_driver fmod_audio_driver = {
674 INIT_FIELD (name = ) "fmod",
675 INIT_FIELD (descr = ) "FMOD 3.xx http://www.fmod.org",
676 INIT_FIELD (options = ) fmod_options,
677 INIT_FIELD (init = ) fmod_audio_init,
678 INIT_FIELD (fini = ) fmod_audio_fini,
679 INIT_FIELD (pcm_ops = ) &fmod_pcm_ops,
680 INIT_FIELD (can_be_default = ) 1,
681 INIT_FIELD (max_voices_out = ) INT_MAX,
682 INIT_FIELD (max_voices_in = ) INT_MAX,
683 INIT_FIELD (voice_size_out = ) sizeof (FMODVoiceOut),
684 INIT_FIELD (voice_size_in = ) sizeof (FMODVoiceIn)