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
);
156 mixeng_clear (src1
, src_len1
);
160 dst
= advance (dst
, src_len1
<< hw
->info
.shift
);
161 hw
->clip (dst
, src2
, src_len2
);
162 mixeng_clear (src2
, src_len2
);
165 hw
->rpos
= pos
% hw
->samples
;
168 static int fmod_unlock_sample (FSOUND_SAMPLE
*sample
, void *p1
, void *p2
,
169 unsigned int blen1
, unsigned int blen2
)
171 int status
= FSOUND_Sample_Unlock (sample
, p1
, p2
, blen1
, blen2
);
173 fmod_logerr ("Failed to unlock sample\n");
179 static int fmod_lock_sample (
180 FSOUND_SAMPLE
*sample
,
181 struct audio_pcm_info
*info
,
192 status
= FSOUND_Sample_Lock (
203 fmod_logerr ("Failed to lock sample\n");
207 if ((*blen1
& info
->align
) || (*blen2
& info
->align
)) {
208 dolog ("Lock returned misaligned length %d, %d, alignment %d\n",
209 *blen1
, *blen2
, info
->align
+ 1);
211 fmod_unlock_sample (sample
, *p1
, *p2
, *blen1
, *blen2
);
220 if (!*p1
&& *blen1
) {
221 dolog ("warning: !p1 && blen1=%d\n", *blen1
);
226 dolog ("warning: !p2 && blen2=%d\n", *blen2
);
233 static int fmod_run_out (HWVoiceOut
*hw
)
235 FMODVoiceOut
*fmd
= (FMODVoiceOut
*) hw
;
237 void *p1
= 0, *p2
= 0;
238 unsigned int blen1
= 0, blen2
= 0;
239 unsigned int len1
= 0, len2
= 0;
242 live
= audio_pcm_hw_get_live_out2 (hw
, &nb_live
);
247 if (!hw
->pending_disable
249 && (conf
.threshold
&& live
<= conf
.threshold
)) {
250 ldebug ("live=%d nb_live=%d\n", live
, nb_live
);
256 if (fmd
->channel
>= 0) {
258 int old_pos
= fmd
->old_pos
;
259 int ppos
= FSOUND_GetCurrentPosition (fmd
->channel
);
261 if (ppos
== old_pos
|| !ppos
) {
265 if ((old_pos
< ppos
) && ((old_pos
+ len
) > ppos
)) {
266 len
= ppos
- old_pos
;
269 if ((old_pos
> ppos
) && ((old_pos
+ len
) > (ppos
+ hw
->samples
))) {
270 len
= hw
->samples
- old_pos
+ ppos
;
275 if (audio_bug (AUDIO_FUNC
, decr
< 0)) {
276 dolog ("decr=%d live=%d ppos=%d old_pos=%d len=%d\n",
277 decr
, live
, ppos
, old_pos
, len
);
287 if (fmod_lock_sample (fmd
->fmod_sample
, &fmd
->hw
.info
,
294 len1
= blen1
>> hw
->info
.shift
;
295 len2
= blen2
>> hw
->info
.shift
;
296 ldebug ("%p %p %d %d %d %d\n", p1
, p2
, len1
, len2
, blen1
, blen2
);
300 fmod_write_sample (hw
, p1
, len1
);
304 fmod_write_sample (hw
, p2
, len2
);
307 fmod_unlock_sample (fmd
->fmod_sample
, p1
, p2
, blen1
, blen2
);
309 fmd
->old_pos
= (fmd
->old_pos
+ decr
) % hw
->samples
;
313 static int aud_to_fmodfmt (audfmt_e fmt
, int stereo
)
315 int mode
= FSOUND_LOOP_NORMAL
;
319 mode
|= FSOUND_SIGNED
| FSOUND_8BITS
;
323 mode
|= FSOUND_UNSIGNED
| FSOUND_8BITS
;
327 mode
|= FSOUND_SIGNED
| FSOUND_16BITS
;
331 mode
|= FSOUND_UNSIGNED
| FSOUND_16BITS
;
335 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
339 mode
|= FSOUND_8BITS
;
341 mode
|= stereo
? FSOUND_STEREO
: FSOUND_MONO
;
345 static void fmod_fini_out (HWVoiceOut
*hw
)
347 FMODVoiceOut
*fmd
= (FMODVoiceOut
*) hw
;
349 if (fmd
->fmod_sample
) {
350 FSOUND_Sample_Free (fmd
->fmod_sample
);
351 fmd
->fmod_sample
= 0;
353 if (fmd
->channel
>= 0) {
354 FSOUND_StopSound (fmd
->channel
);
359 static int fmod_init_out (HWVoiceOut
*hw
, audsettings_t
*as
)
361 int bits16
, mode
, channel
;
362 FMODVoiceOut
*fmd
= (FMODVoiceOut
*) hw
;
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 audio_pcm_init_info (&hw
->info
, as
, audio_need_to_swap_endian (0));
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
;
424 if (conf
.broken_adc
) {
428 mode
= aud_to_fmodfmt (as
->fmt
, as
->nchannels
== 2 ? 1 : 0);
429 fmd
->fmod_sample
= FSOUND_Sample_Alloc (
430 FSOUND_FREE
, /* index */
431 conf
.nb_samples
, /* length */
439 if (!fmd
->fmod_sample
) {
440 fmod_logerr2 ("ADC", "Failed to allocate FMOD sample\n");
444 /* FMOD always operates on little endian frames? */
445 audio_pcm_init_info (&hw
->info
, as
, audio_need_to_swap_endian (0));
446 bits16
= (mode
& FSOUND_16BITS
) != 0;
447 hw
->samples
= conf
.nb_samples
;
451 static void fmod_fini_in (HWVoiceIn
*hw
)
453 FMODVoiceIn
*fmd
= (FMODVoiceIn
*) hw
;
455 if (fmd
->fmod_sample
) {
456 FSOUND_Record_Stop ();
457 FSOUND_Sample_Free (fmd
->fmod_sample
);
458 fmd
->fmod_sample
= 0;
462 static int fmod_run_in (HWVoiceIn
*hw
)
464 FMODVoiceIn
*fmd
= (FMODVoiceIn
*) hw
;
465 int hwshift
= hw
->info
.shift
;
466 int live
, dead
, new_pos
, len
;
467 unsigned int blen1
= 0, blen2
= 0;
468 unsigned int len1
, len2
;
472 live
= audio_pcm_hw_get_live_in (hw
);
473 dead
= hw
->samples
- live
;
478 new_pos
= FSOUND_Record_GetPosition ();
480 fmod_logerr ("Could not get recording position\n");
484 len
= audio_ring_dist (new_pos
, hw
->wpos
, hw
->samples
);
488 len
= audio_MIN (len
, dead
);
490 if (fmod_lock_sample (fmd
->fmod_sample
, &fmd
->hw
.info
,
497 len1
= blen1
>> hwshift
;
498 len2
= blen2
>> hwshift
;
502 hw
->conv (hw
->conv_buf
+ hw
->wpos
, p1
, len1
, &nominal_volume
);
505 hw
->conv (hw
->conv_buf
, p2
, len2
, &nominal_volume
);
508 fmod_unlock_sample (fmd
->fmod_sample
, p1
, p2
, blen1
, blen2
);
509 hw
->wpos
= (hw
->wpos
+ decr
) % hw
->samples
;
517 {"none", FSOUND_OUTPUT_NOSOUND
},
519 {"winmm", FSOUND_OUTPUT_WINMM
},
520 {"dsound", FSOUND_OUTPUT_DSOUND
},
521 {"a3d", FSOUND_OUTPUT_A3D
},
522 {"asio", FSOUND_OUTPUT_ASIO
},
525 {"oss", FSOUND_OUTPUT_OSS
},
526 {"alsa", FSOUND_OUTPUT_ALSA
},
527 {"esd", FSOUND_OUTPUT_ESD
},
530 {"mac", FSOUND_OUTPUT_MAC
},
533 {"xbox", FSOUND_OUTPUT_XBOX
},
534 {"ps2", FSOUND_OUTPUT_PS2
},
535 {"gcube", FSOUND_OUTPUT_GC
},
537 {"none-realtime", FSOUND_OUTPUT_NOSOUND_NONREALTIME
}
540 static void *fmod_audio_init (void)
545 int output_type
= -1;
546 const char *drv
= conf
.drvname
;
548 ver
= FSOUND_GetVersion ();
549 if (ver
< FMOD_VERSION
) {
550 dolog ("Wrong FMOD version %f, need at least %f\n", ver
, FMOD_VERSION
);
556 dolog ("FMOD before 3.75 has bug preventing ADC from working\n"
557 "ADC will be disabled.\n");
564 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
565 if (!strcmp (drv
, drvtab
[i
].name
)) {
566 output_type
= drvtab
[i
].type
;
572 dolog ("Unknown FMOD driver `%s'\n", drv
);
573 dolog ("Valid drivers:\n");
574 for (i
= 0; i
< sizeof (drvtab
) / sizeof (drvtab
[0]); i
++) {
575 dolog (" %s\n", drvtab
[i
].name
);
580 if (output_type
!= -1) {
581 status
= FSOUND_SetOutput (output_type
);
583 fmod_logerr ("FSOUND_SetOutput(%d) failed\n", output_type
);
589 status
= FSOUND_SetBufferSize (conf
.bufsize
);
591 fmod_logerr ("FSOUND_SetBufferSize (%d) failed\n", conf
.bufsize
);
595 status
= FSOUND_Init (conf
.freq
, conf
.nb_channels
, 0);
597 fmod_logerr ("FSOUND_Init failed\n");
604 static int fmod_read (SWVoiceIn
*sw
, void *buf
, int size
)
606 return audio_pcm_sw_read (sw
, buf
, size
);
609 static int fmod_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
612 FMODVoiceIn
*fmd
= (FMODVoiceIn
*) hw
;
616 status
= FSOUND_Record_StartSample (fmd
->fmod_sample
, 1);
618 fmod_logerr ("Failed to start recording\n");
623 status
= FSOUND_Record_Stop ();
625 fmod_logerr ("Failed to stop recording\n");
632 static void fmod_audio_fini (void *opaque
)
638 static struct audio_option fmod_options
[] = {
639 {"DRV", AUD_OPT_STR
, &conf
.drvname
,
640 "FMOD driver", NULL
, 0},
641 {"FREQ", AUD_OPT_INT
, &conf
.freq
,
642 "Default frequency", NULL
, 0},
643 {"SAMPLES", AUD_OPT_INT
, &conf
.nb_samples
,
644 "Buffer size in samples", NULL
, 0},
645 {"CHANNELS", AUD_OPT_INT
, &conf
.nb_channels
,
646 "Number of default channels (1 - mono, 2 - stereo)", NULL
, 0},
647 {"BUFSIZE", AUD_OPT_INT
, &conf
.bufsize
,
648 "(undocumented)", NULL
, 0},
650 {"THRESHOLD", AUD_OPT_INT
, &conf
.threshold
,
654 {NULL
, 0, NULL
, NULL
, NULL
, 0}
657 static struct audio_pcm_ops fmod_pcm_ops
= {
671 struct audio_driver fmod_audio_driver
= {
672 INIT_FIELD (name
= ) "fmod",
673 INIT_FIELD (descr
= ) "FMOD 3.xx http://www.fmod.org",
674 INIT_FIELD (options
= ) fmod_options
,
675 INIT_FIELD (init
= ) fmod_audio_init
,
676 INIT_FIELD (fini
= ) fmod_audio_fini
,
677 INIT_FIELD (pcm_ops
= ) &fmod_pcm_ops
,
678 INIT_FIELD (can_be_default
= ) 1,
679 INIT_FIELD (max_voices_out
= ) INT_MAX
,
680 INIT_FIELD (max_voices_in
= ) INT_MAX
,
681 INIT_FIELD (voice_size_out
= ) sizeof (FMODVoiceOut
),
682 INIT_FIELD (voice_size_in
= ) sizeof (FMODVoiceIn
)