2004-11-07 19:04:02 +01:00
|
|
|
/*
|
2005-10-30 19:58:22 +01:00
|
|
|
* QEMU FMOD audio driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004-2005 Vassili Karpov (malc)
|
|
|
|
*
|
2004-11-07 19:04:02 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <fmod.h>
|
|
|
|
#include <fmod_errors.h>
|
2007-11-17 18:35:54 +01:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "audio.h"
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
#define AUDIO_CAP "fmod"
|
|
|
|
#include "audio_int.h"
|
2004-11-10 00:09:44 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
typedef struct FMODVoiceOut {
|
|
|
|
HWVoiceOut hw;
|
2004-11-10 00:09:44 +01:00
|
|
|
unsigned int old_pos;
|
|
|
|
FSOUND_SAMPLE *fmod_sample;
|
|
|
|
int channel;
|
2005-10-30 19:58:22 +01:00
|
|
|
} FMODVoiceOut;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
typedef struct FMODVoiceIn {
|
|
|
|
HWVoiceIn hw;
|
|
|
|
FSOUND_SAMPLE *fmod_sample;
|
|
|
|
} FMODVoiceIn;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
static struct {
|
2005-10-30 19:58:22 +01:00
|
|
|
const char *drvname;
|
2004-11-07 19:04:02 +01:00
|
|
|
int nb_samples;
|
|
|
|
int freq;
|
|
|
|
int nb_channels;
|
|
|
|
int bufsize;
|
|
|
|
int threshold;
|
2005-10-30 19:58:22 +01:00
|
|
|
int broken_adc;
|
2004-11-07 19:04:02 +01:00
|
|
|
} conf = {
|
2005-10-30 19:58:22 +01:00
|
|
|
NULL,
|
|
|
|
2048 * 2,
|
2004-11-07 19:04:02 +01:00
|
|
|
44100,
|
2005-10-30 19:58:22 +01:00
|
|
|
2,
|
|
|
|
0,
|
2004-11-07 19:04:02 +01:00
|
|
|
0,
|
2005-10-30 19:58:22 +01:00
|
|
|
0
|
2004-11-07 19:04:02 +01:00
|
|
|
};
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static void GCC_FMT_ATTR (1, 2) fmod_logerr (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
AUD_vlog (AUDIO_CAP, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
AUD_log (AUDIO_CAP, "Reason: %s\n",
|
|
|
|
FMOD_ErrorString (FSOUND_GetError ()));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GCC_FMT_ATTR (2, 3) fmod_logerr2 (
|
|
|
|
const char *typ,
|
|
|
|
const char *fmt,
|
|
|
|
...
|
|
|
|
)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
AUD_vlog (AUDIO_CAP, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
AUD_log (AUDIO_CAP, "Reason: %s\n",
|
|
|
|
FMOD_ErrorString (FSOUND_GetError ()));
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static int fmod_write (SWVoiceOut *sw, void *buf, int len)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
return audio_pcm_sw_write (sw, buf, len);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static void fmod_clear_sample (FMODVoiceOut *fmd)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
HWVoiceOut *hw = &fmd->hw;
|
2004-11-07 19:04:02 +01:00
|
|
|
int status;
|
|
|
|
void *p1 = 0, *p2 = 0;
|
|
|
|
unsigned int len1 = 0, len2 = 0;
|
|
|
|
|
|
|
|
status = FSOUND_Sample_Lock (
|
|
|
|
fmd->fmod_sample,
|
|
|
|
0,
|
2005-10-30 19:58:22 +01:00
|
|
|
hw->samples << hw->info.shift,
|
2004-11-07 19:04:02 +01:00
|
|
|
&p1,
|
|
|
|
&p2,
|
|
|
|
&len1,
|
|
|
|
&len2
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!status) {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_logerr ("Failed to lock sample\n");
|
2004-11-07 19:04:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
if ((len1 & hw->info.align) || (len2 & hw->info.align)) {
|
|
|
|
dolog ("Lock returned misaligned length %d, %d, alignment %d\n",
|
|
|
|
len1, len2, hw->info.align + 1);
|
2004-11-07 19:04:02 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
if ((len1 + len2) - (hw->samples << hw->info.shift)) {
|
|
|
|
dolog ("Lock returned incomplete length %d, %d\n",
|
|
|
|
len1 + len2, hw->samples << hw->info.shift);
|
2004-11-07 19:04:02 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
audio_pcm_info_clear_buf (&hw->info, p1, hw->samples);
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
status = FSOUND_Sample_Unlock (fmd->fmod_sample, p1, p2, len1, len2);
|
|
|
|
if (!status) {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_logerr ("Failed to unlock sample\n");
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static void fmod_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
int src_len1 = dst_len;
|
|
|
|
int src_len2 = 0;
|
|
|
|
int pos = hw->rpos + dst_len;
|
2008-12-03 23:48:44 +01:00
|
|
|
struct st_sample *src1 = hw->mix_buf + hw->rpos;
|
|
|
|
struct st_sample *src2 = NULL;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
if (pos > hw->samples) {
|
|
|
|
src_len1 = hw->samples - hw->rpos;
|
|
|
|
src2 = hw->mix_buf;
|
2004-11-07 19:04:02 +01:00
|
|
|
src_len2 = dst_len - src_len1;
|
|
|
|
pos = src_len2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src_len1) {
|
|
|
|
hw->clip (dst, src1, src_len1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src_len2) {
|
2005-10-30 19:58:22 +01:00
|
|
|
dst = advance (dst, src_len1 << hw->info.shift);
|
2004-11-07 19:04:02 +01:00
|
|
|
hw->clip (dst, src2, src_len2);
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
hw->rpos = pos % hw->samples;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static int fmod_unlock_sample (FSOUND_SAMPLE *sample, void *p1, void *p2,
|
2004-11-07 19:04:02 +01:00
|
|
|
unsigned int blen1, unsigned int blen2)
|
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
int status = FSOUND_Sample_Unlock (sample, p1, p2, blen1, blen2);
|
2004-11-07 19:04:02 +01:00
|
|
|
if (!status) {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_logerr ("Failed to unlock sample\n");
|
2004-11-07 19:04:02 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static int fmod_lock_sample (
|
|
|
|
FSOUND_SAMPLE *sample,
|
|
|
|
struct audio_pcm_info *info,
|
|
|
|
int pos,
|
|
|
|
int len,
|
|
|
|
void **p1,
|
|
|
|
void **p2,
|
|
|
|
unsigned int *blen1,
|
|
|
|
unsigned int *blen2
|
|
|
|
)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status = FSOUND_Sample_Lock (
|
2005-10-30 19:58:22 +01:00
|
|
|
sample,
|
|
|
|
pos << info->shift,
|
|
|
|
len << info->shift,
|
2004-11-07 19:04:02 +01:00
|
|
|
p1,
|
|
|
|
p2,
|
|
|
|
blen1,
|
|
|
|
blen2
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!status) {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_logerr ("Failed to lock sample\n");
|
2004-11-07 19:04:02 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
if ((*blen1 & info->align) || (*blen2 & info->align)) {
|
|
|
|
dolog ("Lock returned misaligned length %d, %d, alignment %d\n",
|
|
|
|
*blen1, *blen2, info->align + 1);
|
|
|
|
|
|
|
|
fmod_unlock_sample (sample, *p1, *p2, *blen1, *blen2);
|
|
|
|
|
|
|
|
*p1 = NULL - 1;
|
|
|
|
*p2 = NULL - 1;
|
|
|
|
*blen1 = ~0U;
|
|
|
|
*blen2 = ~0U;
|
2004-11-07 19:04:02 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
if (!*p1 && *blen1) {
|
|
|
|
dolog ("warning: !p1 && blen1=%d\n", *blen1);
|
|
|
|
*blen1 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p2 && *blen2) {
|
|
|
|
dolog ("warning: !p2 && blen2=%d\n", *blen2);
|
|
|
|
*blen2 = 0;
|
|
|
|
}
|
|
|
|
|
2004-11-07 19:04:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static int fmod_run_out (HWVoiceOut *hw)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
|
|
|
|
int live, decr;
|
2004-11-07 19:04:02 +01:00
|
|
|
void *p1 = 0, *p2 = 0;
|
|
|
|
unsigned int blen1 = 0, blen2 = 0;
|
|
|
|
unsigned int len1 = 0, len2 = 0;
|
2005-10-30 19:58:22 +01:00
|
|
|
int nb_live;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
|
|
|
|
if (!live) {
|
|
|
|
return 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!hw->pending_disable
|
2005-10-30 19:58:22 +01:00
|
|
|
&& nb_live
|
|
|
|
&& (conf.threshold && live <= conf.threshold)) {
|
|
|
|
ldebug ("live=%d nb_live=%d\n", live, nb_live);
|
|
|
|
return 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
decr = live;
|
|
|
|
|
|
|
|
if (fmd->channel >= 0) {
|
2005-10-30 19:58:22 +01:00
|
|
|
int len = decr;
|
|
|
|
int old_pos = fmd->old_pos;
|
|
|
|
int ppos = FSOUND_GetCurrentPosition (fmd->channel);
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
if (ppos == old_pos || !ppos) {
|
|
|
|
return 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
|
|
|
|
len = ppos - old_pos;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((old_pos > ppos) && ((old_pos + len) > (ppos + hw->samples))) {
|
|
|
|
len = hw->samples - old_pos + ppos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
decr = len;
|
|
|
|
|
|
|
|
if (audio_bug (AUDIO_FUNC, decr < 0)) {
|
|
|
|
dolog ("decr=%d live=%d ppos=%d old_pos=%d len=%d\n",
|
|
|
|
decr, live, ppos, old_pos, len);
|
|
|
|
return 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
if (!decr) {
|
|
|
|
return 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
if (fmod_lock_sample (fmd->fmod_sample, &fmd->hw.info,
|
|
|
|
fmd->old_pos, decr,
|
|
|
|
&p1, &p2,
|
|
|
|
&blen1, &blen2)) {
|
|
|
|
return 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
len1 = blen1 >> hw->info.shift;
|
|
|
|
len2 = blen2 >> hw->info.shift;
|
2004-11-07 19:04:02 +01:00
|
|
|
ldebug ("%p %p %d %d %d %d\n", p1, p2, len1, len2, blen1, blen2);
|
|
|
|
decr = len1 + len2;
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
if (p1 && len1) {
|
|
|
|
fmod_write_sample (hw, p1, len1);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
if (p2 && len2) {
|
|
|
|
fmod_write_sample (hw, p2, len2);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_unlock_sample (fmd->fmod_sample, p1, p2, blen1, blen2);
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
fmd->old_pos = (fmd->old_pos + decr) % hw->samples;
|
2005-10-30 19:58:22 +01:00
|
|
|
return decr;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static int aud_to_fmodfmt (audfmt_e fmt, int stereo)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
|
|
|
int mode = FSOUND_LOOP_NORMAL;
|
|
|
|
|
|
|
|
switch (fmt) {
|
|
|
|
case AUD_FMT_S8:
|
|
|
|
mode |= FSOUND_SIGNED | FSOUND_8BITS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUD_FMT_U8:
|
|
|
|
mode |= FSOUND_UNSIGNED | FSOUND_8BITS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUD_FMT_S16:
|
|
|
|
mode |= FSOUND_SIGNED | FSOUND_16BITS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUD_FMT_U16:
|
|
|
|
mode |= FSOUND_UNSIGNED | FSOUND_16BITS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2005-10-30 19:58:22 +01:00
|
|
|
dolog ("Internal logic error: Bad audio format %d\n", fmt);
|
|
|
|
#ifdef DEBUG_FMOD
|
|
|
|
abort ();
|
|
|
|
#endif
|
|
|
|
mode |= FSOUND_8BITS;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
mode |= stereo ? FSOUND_STEREO : FSOUND_MONO;
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static void fmod_fini_out (HWVoiceOut *hw)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
if (fmd->fmod_sample) {
|
|
|
|
FSOUND_Sample_Free (fmd->fmod_sample);
|
|
|
|
fmd->fmod_sample = 0;
|
|
|
|
|
|
|
|
if (fmd->channel >= 0) {
|
|
|
|
FSOUND_StopSound (fmd->channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 23:48:44 +01:00
|
|
|
static int fmod_init_out (HWVoiceOut *hw, struct audsettings *as)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
|
|
|
int bits16, mode, channel;
|
2005-10-30 19:58:22 +01:00
|
|
|
FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
|
2008-12-03 23:48:44 +01:00
|
|
|
struct audsettings obt_as = *as;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
mode = aud_to_fmodfmt (as->fmt, as->nchannels == 2 ? 1 : 0);
|
2004-11-07 19:04:02 +01:00
|
|
|
fmd->fmod_sample = FSOUND_Sample_Alloc (
|
|
|
|
FSOUND_FREE, /* index */
|
|
|
|
conf.nb_samples, /* length */
|
|
|
|
mode, /* mode */
|
2005-11-05 19:55:28 +01:00
|
|
|
as->freq, /* freq */
|
2004-11-07 19:04:02 +01:00
|
|
|
255, /* volume */
|
|
|
|
128, /* pan */
|
|
|
|
255 /* priority */
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!fmd->fmod_sample) {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_logerr2 ("DAC", "Failed to allocate FMOD sample\n");
|
2004-11-07 19:04:02 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel = FSOUND_PlaySoundEx (FSOUND_FREE, fmd->fmod_sample, 0, 1);
|
|
|
|
if (channel < 0) {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_logerr2 ("DAC", "Failed to start playing sound\n");
|
2004-11-07 19:04:02 +01:00
|
|
|
FSOUND_Sample_Free (fmd->fmod_sample);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fmd->channel = channel;
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
/* FMOD always operates on little endian frames? */
|
2006-07-04 23:47:22 +02:00
|
|
|
obt_as.endianness = 0;
|
|
|
|
audio_pcm_init_info (&hw->info, &obt_as);
|
2005-10-30 19:58:22 +01:00
|
|
|
bits16 = (mode & FSOUND_16BITS) != 0;
|
2005-11-05 19:55:28 +01:00
|
|
|
hw->samples = conf.nb_samples;
|
2004-11-07 19:04:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static int fmod_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
|
|
|
int status;
|
2005-10-30 19:58:22 +01:00
|
|
|
FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case VOICE_ENABLE:
|
|
|
|
fmod_clear_sample (fmd);
|
|
|
|
status = FSOUND_SetPaused (fmd->channel, 0);
|
|
|
|
if (!status) {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_logerr ("Failed to resume channel %d\n", fmd->channel);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VOICE_DISABLE:
|
|
|
|
status = FSOUND_SetPaused (fmd->channel, 1);
|
|
|
|
if (!status) {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_logerr ("Failed to pause channel %d\n", fmd->channel);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-03 23:48:44 +01:00
|
|
|
static int fmod_init_in (HWVoiceIn *hw, struct audsettings *as)
|
2005-10-30 19:58:22 +01:00
|
|
|
{
|
|
|
|
int bits16, mode;
|
|
|
|
FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
|
2008-12-03 23:48:44 +01:00
|
|
|
struct audsettings obt_as = *as;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
if (conf.broken_adc) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
mode = aud_to_fmodfmt (as->fmt, as->nchannels == 2 ? 1 : 0);
|
2005-10-30 19:58:22 +01:00
|
|
|
fmd->fmod_sample = FSOUND_Sample_Alloc (
|
|
|
|
FSOUND_FREE, /* index */
|
|
|
|
conf.nb_samples, /* length */
|
|
|
|
mode, /* mode */
|
2005-11-05 19:55:28 +01:00
|
|
|
as->freq, /* freq */
|
2005-10-30 19:58:22 +01:00
|
|
|
255, /* volume */
|
|
|
|
128, /* pan */
|
|
|
|
255 /* priority */
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!fmd->fmod_sample) {
|
|
|
|
fmod_logerr2 ("ADC", "Failed to allocate FMOD sample\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FMOD always operates on little endian frames? */
|
2006-07-04 23:47:22 +02:00
|
|
|
obt_as.endianness = 0;
|
|
|
|
audio_pcm_init_info (&hw->info, &obt_as);
|
2005-10-30 19:58:22 +01:00
|
|
|
bits16 = (mode & FSOUND_16BITS) != 0;
|
2005-11-05 19:55:28 +01:00
|
|
|
hw->samples = conf.nb_samples;
|
2005-10-30 19:58:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fmod_fini_in (HWVoiceIn *hw)
|
|
|
|
{
|
|
|
|
FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
|
|
|
|
|
|
|
|
if (fmd->fmod_sample) {
|
|
|
|
FSOUND_Record_Stop ();
|
|
|
|
FSOUND_Sample_Free (fmd->fmod_sample);
|
|
|
|
fmd->fmod_sample = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fmod_run_in (HWVoiceIn *hw)
|
|
|
|
{
|
|
|
|
FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
|
|
|
|
int hwshift = hw->info.shift;
|
|
|
|
int live, dead, new_pos, len;
|
|
|
|
unsigned int blen1 = 0, blen2 = 0;
|
|
|
|
unsigned int len1, len2;
|
|
|
|
unsigned int decr;
|
|
|
|
void *p1, *p2;
|
|
|
|
|
|
|
|
live = audio_pcm_hw_get_live_in (hw);
|
|
|
|
dead = hw->samples - live;
|
|
|
|
if (!dead) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_pos = FSOUND_Record_GetPosition ();
|
|
|
|
if (new_pos < 0) {
|
2005-11-05 19:55:28 +01:00
|
|
|
fmod_logerr ("Could not get recording position\n");
|
2005-10-30 19:58:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = audio_ring_dist (new_pos, hw->wpos, hw->samples);
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
len = audio_MIN (len, dead);
|
|
|
|
|
|
|
|
if (fmod_lock_sample (fmd->fmod_sample, &fmd->hw.info,
|
|
|
|
hw->wpos, len,
|
|
|
|
&p1, &p2,
|
|
|
|
&blen1, &blen2)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
len1 = blen1 >> hwshift;
|
|
|
|
len2 = blen2 >> hwshift;
|
|
|
|
decr = len1 + len2;
|
|
|
|
|
|
|
|
if (p1 && blen1) {
|
|
|
|
hw->conv (hw->conv_buf + hw->wpos, p1, len1, &nominal_volume);
|
|
|
|
}
|
|
|
|
if (p2 && len2) {
|
|
|
|
hw->conv (hw->conv_buf, p2, len2, &nominal_volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
fmod_unlock_sample (fmd->fmod_sample, p1, p2, blen1, blen2);
|
|
|
|
hw->wpos = (hw->wpos + decr) % hw->samples;
|
|
|
|
return decr;
|
|
|
|
}
|
|
|
|
|
2004-11-07 19:04:02 +01:00
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
int type;
|
|
|
|
} drvtab[] = {
|
|
|
|
{"none", FSOUND_OUTPUT_NOSOUND},
|
|
|
|
#ifdef _WIN32
|
|
|
|
{"winmm", FSOUND_OUTPUT_WINMM},
|
|
|
|
{"dsound", FSOUND_OUTPUT_DSOUND},
|
|
|
|
{"a3d", FSOUND_OUTPUT_A3D},
|
|
|
|
{"asio", FSOUND_OUTPUT_ASIO},
|
|
|
|
#endif
|
|
|
|
#ifdef __linux__
|
|
|
|
{"oss", FSOUND_OUTPUT_OSS},
|
|
|
|
{"alsa", FSOUND_OUTPUT_ALSA},
|
|
|
|
{"esd", FSOUND_OUTPUT_ESD},
|
|
|
|
#endif
|
|
|
|
#ifdef __APPLE__
|
|
|
|
{"mac", FSOUND_OUTPUT_MAC},
|
|
|
|
#endif
|
|
|
|
#if 0
|
|
|
|
{"xbox", FSOUND_OUTPUT_XBOX},
|
|
|
|
{"ps2", FSOUND_OUTPUT_PS2},
|
|
|
|
{"gcube", FSOUND_OUTPUT_GC},
|
|
|
|
#endif
|
2005-10-30 19:58:22 +01:00
|
|
|
{"none-realtime", FSOUND_OUTPUT_NOSOUND_NONREALTIME}
|
2004-11-07 19:04:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void *fmod_audio_init (void)
|
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
size_t i;
|
2004-11-07 19:04:02 +01:00
|
|
|
double ver;
|
|
|
|
int status;
|
|
|
|
int output_type = -1;
|
2005-10-30 19:58:22 +01:00
|
|
|
const char *drv = conf.drvname;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
ver = FSOUND_GetVersion ();
|
|
|
|
if (ver < FMOD_VERSION) {
|
|
|
|
dolog ("Wrong FMOD version %f, need at least %f\n", ver, FMOD_VERSION);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
#ifdef __linux__
|
|
|
|
if (ver < 3.75) {
|
|
|
|
dolog ("FMOD before 3.75 has bug preventing ADC from working\n"
|
|
|
|
"ADC will be disabled.\n");
|
|
|
|
conf.broken_adc = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-11-07 19:04:02 +01:00
|
|
|
if (drv) {
|
|
|
|
int found = 0;
|
2008-12-22 21:33:55 +01:00
|
|
|
for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
|
2004-11-07 19:04:02 +01:00
|
|
|
if (!strcmp (drv, drvtab[i].name)) {
|
|
|
|
output_type = drvtab[i].type;
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2005-10-30 19:58:22 +01:00
|
|
|
dolog ("Unknown FMOD driver `%s'\n", drv);
|
|
|
|
dolog ("Valid drivers:\n");
|
2008-12-22 21:33:55 +01:00
|
|
|
for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
|
2005-10-30 19:58:22 +01:00
|
|
|
dolog (" %s\n", drvtab[i].name);
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output_type != -1) {
|
|
|
|
status = FSOUND_SetOutput (output_type);
|
|
|
|
if (!status) {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_logerr ("FSOUND_SetOutput(%d) failed\n", output_type);
|
2004-11-07 19:04:02 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conf.bufsize) {
|
|
|
|
status = FSOUND_SetBufferSize (conf.bufsize);
|
|
|
|
if (!status) {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_logerr ("FSOUND_SetBufferSize (%d) failed\n", conf.bufsize);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status = FSOUND_Init (conf.freq, conf.nb_channels, 0);
|
|
|
|
if (!status) {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_logerr ("FSOUND_Init failed\n");
|
2004-11-07 19:04:02 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &conf;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static int fmod_read (SWVoiceIn *sw, void *buf, int size)
|
|
|
|
{
|
|
|
|
return audio_pcm_sw_read (sw, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fmod_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case VOICE_ENABLE:
|
|
|
|
status = FSOUND_Record_StartSample (fmd->fmod_sample, 1);
|
|
|
|
if (!status) {
|
|
|
|
fmod_logerr ("Failed to start recording\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VOICE_DISABLE:
|
|
|
|
status = FSOUND_Record_Stop ();
|
|
|
|
if (!status) {
|
|
|
|
fmod_logerr ("Failed to stop recording\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-07 19:04:02 +01:00
|
|
|
static void fmod_audio_fini (void *opaque)
|
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
(void) opaque;
|
2004-11-07 19:04:02 +01:00
|
|
|
FSOUND_Close ();
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static struct audio_option fmod_options[] = {
|
|
|
|
{"DRV", AUD_OPT_STR, &conf.drvname,
|
|
|
|
"FMOD driver", NULL, 0},
|
|
|
|
{"FREQ", AUD_OPT_INT, &conf.freq,
|
|
|
|
"Default frequency", NULL, 0},
|
|
|
|
{"SAMPLES", AUD_OPT_INT, &conf.nb_samples,
|
|
|
|
"Buffer size in samples", NULL, 0},
|
|
|
|
{"CHANNELS", AUD_OPT_INT, &conf.nb_channels,
|
|
|
|
"Number of default channels (1 - mono, 2 - stereo)", NULL, 0},
|
|
|
|
{"BUFSIZE", AUD_OPT_INT, &conf.bufsize,
|
|
|
|
"(undocumented)", NULL, 0},
|
|
|
|
#if 0
|
|
|
|
{"THRESHOLD", AUD_OPT_INT, &conf.threshold,
|
|
|
|
"(undocumented)"},
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{NULL, 0, NULL, NULL, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2008-10-06 20:08:30 +02:00
|
|
|
static struct audio_pcm_ops fmod_pcm_ops = {
|
2005-10-30 19:58:22 +01:00
|
|
|
fmod_init_out,
|
|
|
|
fmod_fini_out,
|
|
|
|
fmod_run_out,
|
|
|
|
fmod_write,
|
|
|
|
fmod_ctl_out,
|
|
|
|
|
|
|
|
fmod_init_in,
|
|
|
|
fmod_fini_in,
|
|
|
|
fmod_run_in,
|
|
|
|
fmod_read,
|
|
|
|
fmod_ctl_in
|
2004-11-07 19:04:02 +01:00
|
|
|
};
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
struct audio_driver fmod_audio_driver = {
|
|
|
|
INIT_FIELD (name = ) "fmod",
|
|
|
|
INIT_FIELD (descr = ) "FMOD 3.xx http://www.fmod.org",
|
|
|
|
INIT_FIELD (options = ) fmod_options,
|
|
|
|
INIT_FIELD (init = ) fmod_audio_init,
|
|
|
|
INIT_FIELD (fini = ) fmod_audio_fini,
|
|
|
|
INIT_FIELD (pcm_ops = ) &fmod_pcm_ops,
|
|
|
|
INIT_FIELD (can_be_default = ) 1,
|
|
|
|
INIT_FIELD (max_voices_out = ) INT_MAX,
|
|
|
|
INIT_FIELD (max_voices_in = ) INT_MAX,
|
|
|
|
INIT_FIELD (voice_size_out = ) sizeof (FMODVoiceOut),
|
|
|
|
INIT_FIELD (voice_size_in = ) sizeof (FMODVoiceIn)
|
2004-11-07 19:04:02 +01:00
|
|
|
};
|