📄 fmodaudio.c
字号:
/* * QEMU FMOD audio driver * * Copyright (c) 2004-2005 Vassili Karpov (malc) * * 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>#include "vl.h"#define AUDIO_CAP "fmod"#include "audio_int.h"typedef struct FMODVoiceOut { HWVoiceOut hw; unsigned int old_pos; FSOUND_SAMPLE *fmod_sample; int channel;} FMODVoiceOut;typedef struct FMODVoiceIn { HWVoiceIn hw; FSOUND_SAMPLE *fmod_sample;} FMODVoiceIn;static struct { const char *drvname; int nb_samples; int freq; int nb_channels; int bufsize; int threshold; int broken_adc;} conf = { NULL, 2048 * 2, 44100, 2, 0, 0, 0};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; AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); 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 int fmod_write (SWVoiceOut *sw, void *buf, int len){ return audio_pcm_sw_write (sw, buf, len);}static void fmod_clear_sample (FMODVoiceOut *fmd){ HWVoiceOut *hw = &fmd->hw; int status; void *p1 = 0, *p2 = 0; unsigned int len1 = 0, len2 = 0; status = FSOUND_Sample_Lock ( fmd->fmod_sample, 0, hw->samples << hw->info.shift, &p1, &p2, &len1, &len2 ); if (!status) { fmod_logerr ("Failed to lock sample\n"); return; } 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); goto fail; } if ((len1 + len2) - (hw->samples << hw->info.shift)) { dolog ("Lock returned incomplete length %d, %d\n", len1 + len2, hw->samples << hw->info.shift); goto fail; } audio_pcm_info_clear_buf (&hw->info, p1, hw->samples); fail: status = FSOUND_Sample_Unlock (fmd->fmod_sample, p1, p2, len1, len2); if (!status) { fmod_logerr ("Failed to unlock sample\n"); }}static void fmod_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len){ int src_len1 = dst_len; int src_len2 = 0; int pos = hw->rpos + dst_len; st_sample_t *src1 = hw->mix_buf + hw->rpos; st_sample_t *src2 = NULL; if (pos > hw->samples) { src_len1 = hw->samples - hw->rpos; src2 = hw->mix_buf; src_len2 = dst_len - src_len1; pos = src_len2; } if (src_len1) { hw->clip (dst, src1, src_len1); mixeng_clear (src1, src_len1); } if (src_len2) { dst = advance (dst, src_len1 << hw->info.shift); hw->clip (dst, src2, src_len2); mixeng_clear (src2, src_len2); } hw->rpos = pos % hw->samples;}static int fmod_unlock_sample (FSOUND_SAMPLE *sample, void *p1, void *p2, unsigned int blen1, unsigned int blen2){ int status = FSOUND_Sample_Unlock (sample, p1, p2, blen1, blen2); if (!status) { fmod_logerr ("Failed to unlock sample\n"); return -1; } return 0;}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 ){ int status; status = FSOUND_Sample_Lock ( sample, pos << info->shift, len << info->shift, p1, p2, blen1, blen2 ); if (!status) { fmod_logerr ("Failed to lock sample\n"); return -1; } 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; return -1; } if (!*p1 && *blen1) { dolog ("warning: !p1 && blen1=%d\n", *blen1); *blen1 = 0; } if (!p2 && *blen2) { dolog ("warning: !p2 && blen2=%d\n", *blen2); *blen2 = 0; } return 0;}static int fmod_run_out (HWVoiceOut *hw){ FMODVoiceOut *fmd = (FMODVoiceOut *) hw; int live, decr; void *p1 = 0, *p2 = 0; unsigned int blen1 = 0, blen2 = 0; unsigned int len1 = 0, len2 = 0; int nb_live; live = audio_pcm_hw_get_live_out2 (hw, &nb_live); if (!live) { return 0; } if (!hw->pending_disable && nb_live && (conf.threshold && live <= conf.threshold)) { ldebug ("live=%d nb_live=%d\n", live, nb_live); return 0; } decr = live; if (fmd->channel >= 0) { int len = decr; int old_pos = fmd->old_pos; int ppos = FSOUND_GetCurrentPosition (fmd->channel); if (ppos == old_pos || !ppos) { return 0; } 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; } } if (!decr) { return 0; } if (fmod_lock_sample (fmd->fmod_sample, &fmd->hw.info, fmd->old_pos, decr, &p1, &p2, &blen1, &blen2)) { return 0; } len1 = blen1 >> hw->info.shift; len2 = blen2 >> hw->info.shift; ldebug ("%p %p %d %d %d %d\n", p1, p2, len1, len2, blen1, blen2); decr = len1 + len2; if (p1 && len1) { fmod_write_sample (hw, p1, len1); } if (p2 && len2) { fmod_write_sample (hw, p2, len2); } fmod_unlock_sample (fmd->fmod_sample, p1, p2, blen1, blen2); fmd->old_pos = (fmd->old_pos + decr) % hw->samples; return decr;}static int aud_to_fmodfmt (audfmt_e fmt, int stereo){ 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: dolog ("Internal logic error: Bad audio format %d\n", fmt);#ifdef DEBUG_FMOD abort ();#endif mode |= FSOUND_8BITS; } mode |= stereo ? FSOUND_STEREO : FSOUND_MONO; return mode;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -