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>
26 #include "qemu-common.h"
29 #define AUDIO_CAP "fmod"
30 #include "audio_int.h"
32 typedef struct FMODVoiceOut
{
35 FSOUND_SAMPLE
*fmod_sample
;
39 typedef struct FMODVoiceIn
{
41 FSOUND_SAMPLE
*fmod_sample
;
62 static void GCC_FMT_ATTR (1, 2) fmod_logerr (const char *fmt
, ...)
67 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
70 AUD_log (AUDIO_CAP
, "Reason: %s\n",
71 FMOD_ErrorString (FSOUND_GetError ()));
74 static void GCC_FMT_ATTR (2, 3) fmod_logerr2 (
82 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
85 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
88 AUD_log (AUDIO_CAP
, "Reason: %s\n",
89 FMOD_ErrorString (FSOUND_GetError ()));
92 static int fmod_write (SWVoiceOut
*sw
, void *buf
, int len
)
94 return audio_pcm_sw_write (sw
, buf
, len
);
97 static void fmod_clear_sample (FMODVoiceOut
*fmd
)
99 HWVoiceOut
*hw
= &fmd
->hw
;
101 void *p1
= 0, *p2
= 0;
102 unsigned int len1
= 0, len2
= 0;
104 status
= FSOUND_Sample_Lock (
107 hw
->samples
<< hw
->info
.shift
,
115 fmod_logerr ("Failed to lock sample\n");
119 if ((len1
& hw
->info
.align
) || (len2
& hw
->info
.align
)) {
120 dolog ("Lock returned misaligned length %d, %d, alignment %d\n",
121 len1
, len2
, hw
->info
.align
+ 1);
125 if ((len1
+ len2
) - (hw
->samples
<< hw
->info
.shift
)) {
126 dolog ("Lock returned incomplete length %d, %d\n",
127 len1
+ len2
, hw
->samples
<< hw
->info
.shift
);
131 audio_pcm_info_clear_buf (&hw
->info
, p1
, hw
->samples
);
134 status
= FSOUND_Sample_Unlock (fmd
->fmod_sample
, p1
, p2
, len1
, len2
);
136 fmod_logerr ("Failed to unlock sample\n");
140 static void fmod_write_sample (HWVoiceOut
*hw
, uint8_t *dst
, int dst_len
)
142 int src_len1
= dst_len
;
144 int pos
= hw
->rpos
+ dst_len
;
145 struct st_sample
*src1
= hw
->mix_buf
+ hw
->rpos
;
146 struct st_sample
*src2
= NULL
;
148 if (pos
> hw
->samples
) {
149 src_len1
= hw
->samples
- hw
->rpos
;
151 src_len2
= dst_len
- src_len1
;
156 hw
->clip (dst
, src1
, src_len1
);
160 dst
= advance (dst
, src_len1
<< hw
->info
.shift
);
161 hw
->clip (dst
, src2
, src_len2
);
164 hw
->rpos
= pos
% hw
->samples
;
167 static int fmod_unlock_sample (FSOUND_SAMPLE
*sample
, void *p1
, void *p2
,
168 unsigned int blen1
, unsigned int blen2
)
170 int status
= FSOUND_Sample_Unlock (sample
, p1
, p2
, blen1
, blen2
);
172 fmod_logerr ("Failed to unlock sample\n");
178 static int fmod_lock_sample (
179 FSOUND_SAMPLE
*sample
,
180 struct audio_pcm_info
*info
,
191 status
= FSOUND_Sample_Lock (
202 fmod_logerr ("Failed to lock sample\n");
206 if ((*blen1
& info
->align
) || (*blen2
& info
->align
)) {
207 dolog ("Lock returned misaligned length %d, %d, alignment %d\n",
208 *blen1
, *blen2
, info
->align
+ 1);
210 fmod_unlock_sample (sample
, *p1
, *p2
, *blen1
, *blen2
);
219 if (!*p1
&& *blen1
) {
220 dolog ("warning: !p1 && blen1=%d\n", *blen1
);
225 dolog ("warning: !p2 && blen2=%d\n", *blen2
);
232 static int fmod_run_out (HWVoiceOut
*hw
)
234 FMODVoiceOut
*fmd
= (FMODVoiceOut
*) hw
;
236 void *p1
= 0, *p2
= 0;
237 unsigned int blen1
= 0, blen2
= 0;
238 unsigned int len1
= 0, len2
= 0;
241 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
246 if (!hw
->pending_disable
248 && (conf
.threshold
&& live
<= conf
.threshold
)) {
249 ldebug ("live=%d nb_live=%d\n", live
, nb_live
);
255 if (fmd
->channel
>= 0) {
257 int old_pos
= fmd
->old_pos
;
258 int ppos
= FSOUND_GetCurrentPosition (fmd
->channel
);
260 if (ppos
== old_pos
|| !ppos
) {
264 if ((old_pos
< ppos
) && ((old_pos
+ len
) > ppos
)) {
265 len
= ppos
- old_pos
;
268 if ((old_pos
> ppos
) && ((old_pos
+ len
) > (ppos
+ hw
->samples
))) {
269 len
= hw
->samples
- old_pos
+ ppos
;
274 if (audio_bug (AUDIO_FUNC
, decr
< 0)) {
275 dolog ("decr=%d live=%d ppos=%d old_pos=%d len=%d\n",
276 decr
, live
, ppos
, old_pos
, len
);
286 if (fmod_lock_sample (fmd
->fmod_sample
, &fmd
->hw
.info
,
293 len1
= blen1
>> hw
->info
.shift
;
294 len2
= blen2
>> hw
->info
.shift
;
295 ldebug ("%p %p %d %d %d %d\n", p1
, p2
, len1
, len2
, blen1
, blen2
);
299 fmod_write_sample (hw
, p1
, len1
);
303 fmod_write_sample (hw
, p2
, len2
);
306 fmod_unlock_sample (fmd
->fmod_sample
, p1
, p2
, blen1
, blen2
);
308 fmd
->old_pos
= (fmd
->old_pos
+ decr
) % hw
->samples
;
312 static int aud_to_fmodfmt (audfmt_e fmt
, int stereo
)
314 int mode
= FSOUND_LOOP_NORMAL
;
318 mode
|= FSOUND_SIGNED
| FSOUND_8BITS
;
322 mode
|= FSOUND_UNSIGNED
| FSOUND_8BITS
;
326 mode
|= FSOUND_SIGNED
| FSOUND_16BITS
;
330 mode
|= FSOUND_UNSIGNED
| FSOUND_16BITS
;
334 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
338 mode
|= FSOUND_8BITS
;
340 mode
|= stereo
? FSOUND_STEREO
: FSOUND_MONO
;
344 static void fmod_fini_out (HWVoiceOut
*hw
)
346 FMODVoiceOut
*fmd
= (FMODVoiceOut
*) hw
;
348 if (fmd
->fmod_sample
) {
349 FSOUND_Sample_Free (fmd
->fmod_sample
);
350 fmd
->fmod_sample
= 0;
352 if (fmd
->channel
>= 0) {
353 FSOUND_StopSound (fmd
->channel
);
358 static int fmod_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
360 int bits16
, mode
, channel
;
361 FMODVoiceOut
*fmd
= (FMODVoiceOut
*) hw
;
362 struct audsettings obt_as
= *as
;
364 mode
= aud_to_fmodfmt (as
->fmt
, as
->nchannels
== 2 ? 1 : 0);
365 fmd
->fmod_sample
= FSOUND_Sample_Alloc (
366 FSOUND_FREE
, /* index */
367 conf
.nb_samples
, /* length */
375 if (!fmd
->fmod_sample
) {
376 fmod_logerr2 ("DAC", "Failed to allocate FMOD sample\n");
380 channel
= FSOUND_PlaySoundEx (FSOUND_FREE
, fmd
->fmod_sample
, 0, 1);
382 fmod_logerr2 ("DAC", "Failed to start playing sound\n");
383 FSOUND_Sample_Free (fmd
->fmod_sample
);
386 fmd
->channel
= channel
;
388 /* FMOD always operates on little endian frames? */
389 obt_as
.endianness
= 0;
390 audio_pcm_init_info (&hw
->info
, &obt_as
);
391 bits16
= (mode
& FSOUND_16BITS
) != 0;
392 hw
->samples
= conf
.nb_samples
;
396 static int fmod_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
399 FMODVoiceOut
*fmd
= (FMODVoiceOut
*) hw
;
403 fmod_clear_sample (fmd
);
404 status
= FSOUND_SetPaused (fmd
->channel
, 0);
406 fmod_logerr ("Failed to resume channel %d\n", fmd
->channel
);
411 status
= FSOUND_SetPaused (fmd
->channel
, 1);
413 fmod_logerr ("Failed to pause channel %d\n", fmd
->channel
);
420 static int fmod_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
423 FMODVoiceIn
*fmd
= (FMODVoiceIn
*) hw
;
424 struct audsettings obt_as
= *as
;
426 if (conf
.broken_adc
) {
430 mode
= aud_to_fmodfmt (as
->fmt
, as
->nchannels
== 2 ? 1 : 0);
431 fmd
->fmod_sample
= FSOUND_Sample_Alloc (
432 FSOUND_FREE
, /* index */
433 conf
.nb_samples
, /* length */
441 if (!fmd
->fmod_sample
) {
442 fmod_logerr2 ("ADC", "Failed to allocate FMOD sample\n");
446 /* FMOD always operates on little endian frames? */
447 obt_as
.endianness
= 0;
448 audio_pcm_init_info (&hw
->info
, &obt_as
);
449 bits16
= (mode
& FSOUND_16BITS
) != 0;
450 hw
->samples
= conf
.nb_samples
;
454 static void fmod_fini_in (HWVoiceIn
*hw
)
456 FMODVoiceIn
*fmd
= (FMODVoiceIn
*) hw
;
458 if (fmd
->fmod_sample
) {
459 FSOUND_Record_Stop ();
460 FSOUND_Sample_Free (fmd
->fmod_sample
);
461 fmd
->fmod_sample
= 0;
465 static int fmod_run_in (HWVoiceIn
*hw
)
467 FMODVoiceIn
*fmd
= (FMODVoiceIn
*) hw
;
468 int hwshift
= hw
->info
.shift
;
469 int live
, dead
, new_pos
, len
;
470 unsigned int blen1
= 0, blen2
= 0;
471 unsigned int len1
, len2
;
475 live
= audio_pcm_hw_get_live_in (hw
);
476 dead
= hw
->samples
- live
;
481 new_pos
= FSOUND_Record_GetPosition ();
483 fmod_logerr ("Could not get recording position\n");
487 len
= audio_ring_dist (new_pos
, hw
->wpos
, hw
->samples
);
491 len
= audio_MIN (len
, dead
);
493 if (fmod_lock_sample (fmd
->fmod_sample
, &fmd
->hw
.info
,
500 len1
= blen1
>> hwshift
;
501 len2
= blen2
>> hwshift
;
505 hw
->conv (hw
->conv_buf
+ hw
->wpos
, p1
, len1
, &nominal_volume
);
508 hw
->conv (hw
->conv_buf
, p2
, len2
, &nominal_volume
);
511 fmod_unlock_sample (fmd
->fmod_sample
, p1
, p2
, blen1
, blen2
);
512 hw
->wpos
= (hw
->wpos
+ decr
) % hw
->samples
;
520 {"none", FSOUND_OUTPUT_NOSOUND
},
522 {"winmm", FSOUND_OUTPUT_WINMM
},
523 {"dsound", FSOUND_OUTPUT_DSOUND
},
524 {"a3d", FSOUND_OUTPUT_A3D
},
525 {"asio", FSOUND_OUTPUT_ASIO
},
528 {"oss", FSOUND_OUTPUT_OSS
},
529 {"alsa", FSOUND_OUTPUT_ALSA
},
530 {"esd", FSOUND_OUTPUT_ESD
},
533 {"mac", FSOUND_OUTPUT_MAC
},
536 {"xbox", FSOUND_OUTPUT_XBOX
},
537 {"ps2", FSOUND_OUTPUT_PS2
},
538 {"gcube", FSOUND_OUTPUT_GC
},
540 {"none-realtime", FSOUND_OUTPUT_NOSOUND_NONREALTIME
}
543 static void *fmod_audio_init (void)
548 int output_type
= -1;
549 const char *drv
= conf
.drvname
;
551 ver
= FSOUND_GetVersion ();
552 if (ver
< FMOD_VERSION
) {
553 dolog ("Wrong FMOD version %f, need at least %f\n", ver
, FMOD_VERSION
);
559 dolog ("FMOD before 3.75 has bug preventing ADC from working\n"
560 "ADC will be disabled.\n");
567 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
568 if (!strcmp (drv
, drvtab
[i
].name
)) {
569 output_type
= drvtab
[i
].type
;
575 dolog ("Unknown FMOD driver `%s'\n", drv
);
576 dolog ("Valid drivers:\n");
577 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
578 dolog (" %s\n", drvtab
[i
].name
);
583 if (output_type
!= -1) {
584 status
= FSOUND_SetOutput (output_type
);
586 fmod_logerr ("FSOUND_SetOutput(%d) failed\n", output_type
);
592 status
= FSOUND_SetBufferSize (conf
.bufsize
);
594 fmod_logerr ("FSOUND_SetBufferSize (%d) failed\n", conf
.bufsize
);
598 status
= FSOUND_Init (conf
.freq
, conf
.nb_channels
, 0);
600 fmod_logerr ("FSOUND_Init failed\n");
607 static int fmod_read (SWVoiceIn
*sw
, void *buf
, int size
)
609 return audio_pcm_sw_read (sw
, buf
, size
);
612 static int fmod_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
615 FMODVoiceIn
*fmd
= (FMODVoiceIn
*) hw
;
619 status
= FSOUND_Record_StartSample (fmd
->fmod_sample
, 1);
621 fmod_logerr ("Failed to start recording\n");
626 status
= FSOUND_Record_Stop ();
628 fmod_logerr ("Failed to stop recording\n");
635 static void fmod_audio_fini (void *opaque
)
641 static struct audio_option fmod_options
[] = {
642 {"DRV", AUD_OPT_STR
, &conf
.drvname
,
643 "FMOD driver", NULL
, 0},
644 {"FREQ", AUD_OPT_INT
, &conf
.freq
,
645 "Default frequency", NULL
, 0},
646 {"SAMPLES", AUD_OPT_INT
, &conf
.nb_samples
,
647 "Buffer size in samples", NULL
, 0},
648 {"CHANNELS", AUD_OPT_INT
, &conf
.nb_channels
,
649 "Number of default channels (1 - mono, 2 - stereo)", NULL
, 0},
650 {"BUFSIZE", AUD_OPT_INT
, &conf
.bufsize
,
651 "(undocumented)", NULL
, 0},
653 {"THRESHOLD", AUD_OPT_INT
, &conf
.threshold
,
657 {NULL
, 0, NULL
, NULL
, NULL
, 0}
660 static struct audio_pcm_ops fmod_pcm_ops
= {
674 struct audio_driver fmod_audio_driver
= {
675 INIT_FIELD (name
= ) "fmod",
676 INIT_FIELD (descr
= ) "FMOD 3.xx http://www.fmod.org",
677 INIT_FIELD (options
= ) fmod_options
,
678 INIT_FIELD (init
= ) fmod_audio_init
,
679 INIT_FIELD (fini
= ) fmod_audio_fini
,
680 INIT_FIELD (pcm_ops
= ) &fmod_pcm_ops
,
681 INIT_FIELD (can_be_default
= ) 1,
682 INIT_FIELD (max_voices_out
= ) INT_MAX
,
683 INIT_FIELD (max_voices_in
= ) INT_MAX
,
684 INIT_FIELD (voice_size_out
= ) sizeof (FMODVoiceOut
),
685 INIT_FIELD (voice_size_in
= ) sizeof (FMODVoiceIn
)