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
;
362 mode
= aud_to_fmodfmt (as
->fmt
, as
->nchannels
== 2 ? 1 : 0);
363 fmd
->fmod_sample
= FSOUND_Sample_Alloc (
364 FSOUND_FREE
, /* index */
365 conf
.nb_samples
, /* length */
373 if (!fmd
->fmod_sample
) {
374 fmod_logerr2 ("DAC", "Failed to allocate FMOD sample\n");
378 channel
= FSOUND_PlaySoundEx (FSOUND_FREE
, fmd
->fmod_sample
, 0, 1);
380 fmod_logerr2 ("DAC", "Failed to start playing sound\n");
381 FSOUND_Sample_Free (fmd
->fmod_sample
);
384 fmd
->channel
= channel
;
386 /* FMOD always operates on little endian frames? */
387 audio_pcm_init_info (&hw
->info
, as
, audio_need_to_swap_endian (0));
388 bits16
= (mode
& FSOUND_16BITS
) != 0;
389 hw
->samples
= conf
.nb_samples
;
393 static int fmod_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
396 FMODVoiceOut
*fmd
= (FMODVoiceOut
*) hw
;
400 fmod_clear_sample (fmd
);
401 status
= FSOUND_SetPaused (fmd
->channel
, 0);
403 fmod_logerr ("Failed to resume channel %d\n", fmd
->channel
);
408 status
= FSOUND_SetPaused (fmd
->channel
, 1);
410 fmod_logerr ("Failed to pause channel %d\n", fmd
->channel
);
417 static int fmod_init_in (HWVoiceIn
*hw
, audsettings_t
*as
)
420 FMODVoiceIn
*fmd
= (FMODVoiceIn
*) hw
;
422 if (conf
.broken_adc
) {
426 mode
= aud_to_fmodfmt (as
->fmt
, as
->nchannels
== 2 ? 1 : 0);
427 fmd
->fmod_sample
= FSOUND_Sample_Alloc (
428 FSOUND_FREE
, /* index */
429 conf
.nb_samples
, /* length */
437 if (!fmd
->fmod_sample
) {
438 fmod_logerr2 ("ADC", "Failed to allocate FMOD sample\n");
442 /* FMOD always operates on little endian frames? */
443 audio_pcm_init_info (&hw
->info
, as
, audio_need_to_swap_endian (0));
444 bits16
= (mode
& FSOUND_16BITS
) != 0;
445 hw
->samples
= conf
.nb_samples
;
449 static void fmod_fini_in (HWVoiceIn
*hw
)
451 FMODVoiceIn
*fmd
= (FMODVoiceIn
*) hw
;
453 if (fmd
->fmod_sample
) {
454 FSOUND_Record_Stop ();
455 FSOUND_Sample_Free (fmd
->fmod_sample
);
456 fmd
->fmod_sample
= 0;
460 static int fmod_run_in (HWVoiceIn
*hw
)
462 FMODVoiceIn
*fmd
= (FMODVoiceIn
*) hw
;
463 int hwshift
= hw
->info
.shift
;
464 int live
, dead
, new_pos
, len
;
465 unsigned int blen1
= 0, blen2
= 0;
466 unsigned int len1
, len2
;
470 live
= audio_pcm_hw_get_live_in (hw
);
471 dead
= hw
->samples
- live
;
476 new_pos
= FSOUND_Record_GetPosition ();
478 fmod_logerr ("Could not get recording position\n");
482 len
= audio_ring_dist (new_pos
, hw
->wpos
, hw
->samples
);
486 len
= audio_MIN (len
, dead
);
488 if (fmod_lock_sample (fmd
->fmod_sample
, &fmd
->hw
.info
,
495 len1
= blen1
>> hwshift
;
496 len2
= blen2
>> hwshift
;
500 hw
->conv (hw
->conv_buf
+ hw
->wpos
, p1
, len1
, &nominal_volume
);
503 hw
->conv (hw
->conv_buf
, p2
, len2
, &nominal_volume
);
506 fmod_unlock_sample (fmd
->fmod_sample
, p1
, p2
, blen1
, blen2
);
507 hw
->wpos
= (hw
->wpos
+ decr
) % hw
->samples
;
515 {"none", FSOUND_OUTPUT_NOSOUND
},
517 {"winmm", FSOUND_OUTPUT_WINMM
},
518 {"dsound", FSOUND_OUTPUT_DSOUND
},
519 {"a3d", FSOUND_OUTPUT_A3D
},
520 {"asio", FSOUND_OUTPUT_ASIO
},
523 {"oss", FSOUND_OUTPUT_OSS
},
524 {"alsa", FSOUND_OUTPUT_ALSA
},
525 {"esd", FSOUND_OUTPUT_ESD
},
528 {"mac", FSOUND_OUTPUT_MAC
},
531 {"xbox", FSOUND_OUTPUT_XBOX
},
532 {"ps2", FSOUND_OUTPUT_PS2
},
533 {"gcube", FSOUND_OUTPUT_GC
},
535 {"none-realtime", FSOUND_OUTPUT_NOSOUND_NONREALTIME
}
538 static void *fmod_audio_init (void)
543 int output_type
= -1;
544 const char *drv
= conf
.drvname
;
546 ver
= FSOUND_GetVersion ();
547 if (ver
< FMOD_VERSION
) {
548 dolog ("Wrong FMOD version %f, need at least %f\n", ver
, FMOD_VERSION
);
554 dolog ("FMOD before 3.75 has bug preventing ADC from working\n"
555 "ADC will be disabled.\n");
562 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
563 if (!strcmp (drv
, drvtab
[i
].name
)) {
564 output_type
= drvtab
[i
].type
;
570 dolog ("Unknown FMOD driver `%s'\n", drv
);
571 dolog ("Valid drivers:\n");
572 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
573 dolog (" %s\n", drvtab
[i
].name
);
578 if (output_type
!= -1) {
579 status
= FSOUND_SetOutput (output_type
);
581 fmod_logerr ("FSOUND_SetOutput(%d) failed\n", output_type
);
587 status
= FSOUND_SetBufferSize (conf
.bufsize
);
589 fmod_logerr ("FSOUND_SetBufferSize (%d) failed\n", conf
.bufsize
);
593 status
= FSOUND_Init (conf
.freq
, conf
.nb_channels
, 0);
595 fmod_logerr ("FSOUND_Init failed\n");
602 static int fmod_read (SWVoiceIn
*sw
, void *buf
, int size
)
604 return audio_pcm_sw_read (sw
, buf
, size
);
607 static int fmod_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
610 FMODVoiceIn
*fmd
= (FMODVoiceIn
*) hw
;
614 status
= FSOUND_Record_StartSample (fmd
->fmod_sample
, 1);
616 fmod_logerr ("Failed to start recording\n");
621 status
= FSOUND_Record_Stop ();
623 fmod_logerr ("Failed to stop recording\n");
630 static void fmod_audio_fini (void *opaque
)
636 static struct audio_option fmod_options
[] = {
637 {"DRV", AUD_OPT_STR
, &conf
.drvname
,
638 "FMOD driver", NULL
, 0},
639 {"FREQ", AUD_OPT_INT
, &conf
.freq
,
640 "Default frequency", NULL
, 0},
641 {"SAMPLES", AUD_OPT_INT
, &conf
.nb_samples
,
642 "Buffer size in samples", NULL
, 0},
643 {"CHANNELS", AUD_OPT_INT
, &conf
.nb_channels
,
644 "Number of default channels (1 - mono, 2 - stereo)", NULL
, 0},
645 {"BUFSIZE", AUD_OPT_INT
, &conf
.bufsize
,
646 "(undocumented)", NULL
, 0},
648 {"THRESHOLD", AUD_OPT_INT
, &conf
.threshold
,
652 {NULL
, 0, NULL
, NULL
, NULL
, 0}
655 static struct audio_pcm_ops fmod_pcm_ops
= {
669 struct audio_driver fmod_audio_driver
= {
670 INIT_FIELD (name
= ) "fmod",
671 INIT_FIELD (descr
= ) "FMOD 3.xx http://www.fmod.org",
672 INIT_FIELD (options
= ) fmod_options
,
673 INIT_FIELD (init
= ) fmod_audio_init
,
674 INIT_FIELD (fini
= ) fmod_audio_fini
,
675 INIT_FIELD (pcm_ops
= ) &fmod_pcm_ops
,
676 INIT_FIELD (can_be_default
= ) 1,
677 INIT_FIELD (max_voices_out
= ) INT_MAX
,
678 INIT_FIELD (max_voices_in
= ) INT_MAX
,
679 INIT_FIELD (voice_size_out
= ) sizeof (FMODVoiceOut
),
680 INIT_FIELD (voice_size_in
= ) sizeof (FMODVoiceIn
)