2 * QEMU FMOD audio output driver
4 * Copyright (c) 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
25 #include <fmod_errors.h>
28 #include "audio/audio_int.h"
30 typedef struct FMODVoice {
33 FSOUND_SAMPLE *fmod_sample;
38 #define dolog(...) AUD_log ("fmod", __VA_ARGS__)
40 #define ldebug(...) dolog (__VA_ARGS__)
45 #define QC_FMOD_DRV "QEMU_FMOD_DRV"
46 #define QC_FMOD_FREQ "QEMU_FMOD_FREQ"
47 #define QC_FMOD_SAMPLES "QEMU_FMOD_SAMPLES"
48 #define QC_FMOD_CHANNELS "QEMU_FMOD_CHANNELS"
49 #define QC_FMOD_BUFSIZE "QEMU_FMOD_BUFSIZE"
50 #define QC_FMOD_THRESHOLD "QEMU_FMOD_THRESHOLD"
66 #define errstr() FMOD_ErrorString (FSOUND_GetError ())
68 static int fmod_hw_write (SWVoice *sw, void *buf, int len)
70 return pcm_hw_write (sw, buf, len);
73 static void fmod_clear_sample (FMODVoice *fmd)
75 HWVoice *hw = &fmd->hw;
77 void *p1 = 0, *p2 = 0;
78 unsigned int len1 = 0, len2 = 0;
80 status = FSOUND_Sample_Lock (
83 hw->samples << hw->shift,
91 dolog ("Failed to lock sample\nReason: %s\n", errstr ());
95 if ((len1 & hw->align) || (len2 & hw->align)) {
96 dolog ("Locking sample returned unaligned length %d, %d\n",
101 if (len1 + len2 != hw->samples << hw->shift) {
102 dolog ("Locking sample returned incomplete length %d, %d\n",
103 len1 + len2, hw->samples << hw->shift);
106 pcm_hw_clear (hw, p1, hw->samples);
109 status = FSOUND_Sample_Unlock (fmd->fmod_sample, p1, p2, len1, len2);
111 dolog ("Failed to unlock sample\nReason: %s\n", errstr ());
115 static int fmod_write_sample (HWVoice *hw, uint8_t *dst, st_sample_t *src,
116 int src_size, int src_pos, int dst_len)
118 int src_len1 = dst_len, src_len2 = 0, pos = src_pos + dst_len;
119 st_sample_t *src1 = src + src_pos, *src2 = 0;
121 if (src_pos + dst_len > src_size) {
122 src_len1 = src_size - src_pos;
124 src_len2 = dst_len - src_len1;
129 hw->clip (dst, src1, src_len1);
130 memset (src1, 0, src_len1 * sizeof (st_sample_t));
131 advance (dst, src_len1);
135 hw->clip (dst, src2, src_len2);
136 memset (src2, 0, src_len2 * sizeof (st_sample_t));
141 static int fmod_unlock_sample (FMODVoice *fmd, void *p1, void *p2,
142 unsigned int blen1, unsigned int blen2)
144 int status = FSOUND_Sample_Unlock (fmd->fmod_sample, p1, p2, blen1, blen2);
146 dolog ("Failed to unlock sample\nReason: %s\n", errstr ());
152 static int fmod_lock_sample (FMODVoice *fmd, int pos, int len,
153 void **p1, void **p2,
154 unsigned int *blen1, unsigned int *blen2)
156 HWVoice *hw = &fmd->hw;
159 status = FSOUND_Sample_Lock (
170 dolog ("Failed to lock sample\nReason: %s\n", errstr ());
174 if ((*blen1 & hw->align) || (*blen2 & hw->align)) {
175 dolog ("Locking sample returned unaligned length %d, %d\n",
177 fmod_unlock_sample (fmd, *p1, *p2, *blen1, *blen2);
183 static void fmod_hw_run (HWVoice *hw)
185 FMODVoice *fmd = (FMODVoice *) hw;
186 int rpos, live, decr;
187 void *p1 = 0, *p2 = 0;
188 unsigned int blen1 = 0, blen2 = 0;
189 unsigned int len1 = 0, len2 = 0;
192 live = pcm_hw_get_live2 (hw, &nb_active);
197 if (!hw->pending_disable
200 && live <= conf.threshold) {
201 ldebug ("live=%d nb_active=%d\n", live, nb_active);
208 if (fmd->channel >= 0) {
209 int pos2 = (fmd->old_pos + decr) % hw->samples;
210 int pos = FSOUND_GetCurrentPosition (fmd->channel);
212 if (fmd->old_pos < pos && pos2 >= pos) {
213 decr = pos - fmd->old_pos - (pos2 == pos) - 1;
215 else if (fmd->old_pos > pos && pos2 >= pos && pos2 < fmd->old_pos) {
216 decr = (hw->samples - fmd->old_pos) + pos - (pos2 == pos) - 1;
218 /* ldebug ("pos=%d pos2=%d old=%d live=%d decr=%d\n", */
219 /* pos, pos2, fmd->old_pos, live, decr); */
227 if (fmod_lock_sample (fmd, fmd->old_pos, decr, &p1, &p2, &blen1, &blen2)) {
231 len1 = blen1 >> hw->shift;
232 len2 = blen2 >> hw->shift;
233 ldebug ("%p %p %d %d %d %d\n", p1, p2, len1, len2, blen1, blen2);
238 rpos = fmod_write_sample (hw, p1, hw->mix_buf, hw->samples, rpos, len1);
242 rpos = fmod_write_sample (hw, p2, hw->mix_buf, hw->samples, rpos, len2);
245 fmod_unlock_sample (fmd, p1, p2, blen1, blen2);
247 pcm_hw_dec_live (hw, decr);
248 hw->rpos = rpos % hw->samples;
249 fmd->old_pos = (fmd->old_pos + decr) % hw->samples;
252 static int AUD_to_fmodfmt (audfmt_e fmt, int stereo)
254 int mode = FSOUND_LOOP_NORMAL;
258 mode |= FSOUND_SIGNED | FSOUND_8BITS;
262 mode |= FSOUND_UNSIGNED | FSOUND_8BITS;
266 mode |= FSOUND_SIGNED | FSOUND_16BITS;
270 mode |= FSOUND_UNSIGNED | FSOUND_16BITS;
274 dolog ("Internal logic error: Bad audio format %d\nAborting\n", fmt);
277 mode |= stereo ? FSOUND_STEREO : FSOUND_MONO;
281 static void fmod_hw_fini (HWVoice *hw)
283 FMODVoice *fmd = (FMODVoice *) hw;
285 if (fmd->fmod_sample) {
286 FSOUND_Sample_Free (fmd->fmod_sample);
287 fmd->fmod_sample = 0;
289 if (fmd->channel >= 0) {
290 FSOUND_StopSound (fmd->channel);
295 static int fmod_hw_init (HWVoice *hw, int freq, int nchannels, audfmt_e fmt)
297 int bits16, mode, channel;
298 FMODVoice *fmd = (FMODVoice *) hw;
300 mode = AUD_to_fmodfmt (fmt, nchannels == 2 ? 1 : 0);
301 fmd->fmod_sample = FSOUND_Sample_Alloc (
302 FSOUND_FREE, /* index */
303 conf.nb_samples, /* length */
311 if (!fmd->fmod_sample) {
312 dolog ("Failed to allocate FMOD sample\nReason: %s\n", errstr ());
316 channel = FSOUND_PlaySoundEx (FSOUND_FREE, fmd->fmod_sample, 0, 1);
318 dolog ("Failed to start playing sound\nReason: %s\n", errstr ());
319 FSOUND_Sample_Free (fmd->fmod_sample);
322 fmd->channel = channel;
326 hw->nchannels = nchannels;
327 bits16 = fmt == AUD_FMT_U16 || fmt == AUD_FMT_S16;
328 hw->bufsize = conf.nb_samples << (nchannels == 2) << bits16;
332 static int fmod_hw_ctl (HWVoice *hw, int cmd, ...)
335 FMODVoice *fmd = (FMODVoice *) hw;
339 fmod_clear_sample (fmd);
340 status = FSOUND_SetPaused (fmd->channel, 0);
342 dolog ("Failed to resume channel %d\nReason: %s\n",
343 fmd->channel, errstr ());
348 status = FSOUND_SetPaused (fmd->channel, 1);
350 dolog ("Failed to pause channel %d\nReason: %s\n",
351 fmd->channel, errstr ());
362 {"none", FSOUND_OUTPUT_NOSOUND},
364 {"winmm", FSOUND_OUTPUT_WINMM},
365 {"dsound", FSOUND_OUTPUT_DSOUND},
366 {"a3d", FSOUND_OUTPUT_A3D},
367 {"asio", FSOUND_OUTPUT_ASIO},
370 {"oss", FSOUND_OUTPUT_OSS},
371 {"alsa", FSOUND_OUTPUT_ALSA},
372 {"esd", FSOUND_OUTPUT_ESD},
375 {"mac", FSOUND_OUTPUT_MAC},
378 {"xbox", FSOUND_OUTPUT_XBOX},
379 {"ps2", FSOUND_OUTPUT_PS2},
380 {"gcube", FSOUND_OUTPUT_GC},
382 {"nort", FSOUND_OUTPUT_NOSOUND_NONREALTIME}
385 static void *fmod_audio_init (void)
390 int output_type = -1;
391 const char *drv = audio_get_conf_str (QC_FMOD_DRV, NULL);
393 ver = FSOUND_GetVersion ();
394 if (ver < FMOD_VERSION) {
395 dolog ("Wrong FMOD version %f, need at least %f\n", ver, FMOD_VERSION);
401 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
402 if (!strcmp (drv, drvtab[i].name)) {
403 output_type = drvtab[i].type;
409 dolog ("Unknown FMOD output driver `%s'\n", drv);
413 if (output_type != -1) {
414 status = FSOUND_SetOutput (output_type);
416 dolog ("FSOUND_SetOutput(%d) failed\nReason: %s\n",
417 output_type, errstr ());
422 conf.freq = audio_get_conf_int (QC_FMOD_FREQ, conf.freq);
423 conf.nb_samples = audio_get_conf_int (QC_FMOD_SAMPLES, conf.nb_samples);
425 audio_get_conf_int (QC_FMOD_CHANNELS,
426 (audio_state.nb_hw_voices > 1
427 ? audio_state.nb_hw_voices
428 : conf.nb_channels));
429 conf.bufsize = audio_get_conf_int (QC_FMOD_BUFSIZE, conf.bufsize);
430 conf.threshold = audio_get_conf_int (QC_FMOD_THRESHOLD, conf.threshold);
433 status = FSOUND_SetBufferSize (conf.bufsize);
435 dolog ("FSOUND_SetBufferSize (%d) failed\nReason: %s\n",
436 conf.bufsize, errstr ());
440 status = FSOUND_Init (conf.freq, conf.nb_channels, 0);
442 dolog ("FSOUND_Init failed\nReason: %s\n", errstr ());
449 static void fmod_audio_fini (void *opaque)
454 struct pcm_ops fmod_pcm_ops = {
462 struct audio_output_driver fmod_output_driver = {