Switch: Fix audio resampling after GBA fix

This commit is contained in:
Vicki Pfau 2024-04-27 18:17:50 -07:00
parent db1e02521e
commit 5a28564b12
1 changed files with 23 additions and 9 deletions

View File

@ -20,7 +20,7 @@
#include <GLES3/gl31.h> #include <GLES3/gl31.h>
#define AUTO_INPUT 0x4E585031 #define AUTO_INPUT 0x4E585031
#define SAMPLES 0x200 #define SAMPLES 0x400
#define N_BUFFERS 6 #define N_BUFFERS 6
#define ANALOG_DEADZONE 0x4000 #define ANALOG_DEADZONE 0x4000
@ -91,6 +91,7 @@ static struct mRotationSource rotation = {0};
static AudioDriver audrenDriver; static AudioDriver audrenDriver;
static AudioDriverWaveBuf audrvBuffer[N_BUFFERS]; static AudioDriverWaveBuf audrvBuffer[N_BUFFERS];
static struct mStereoSample* audioBuffer[N_BUFFERS]; static struct mStereoSample* audioBuffer[N_BUFFERS];
static double fpsRatio = 1;
static bool frameLimiter = true; static bool frameLimiter = true;
static unsigned framecount = 0; static unsigned framecount = 0;
static unsigned framecap = 10; static unsigned framecap = 10;
@ -266,6 +267,19 @@ static void _updateRenderer(struct mGUIRunner* runner, bool gl) {
} }
} }
static void _resetSampleRate(u32 samplerate) {
if (!samplerate) {
samplerate = 32768;
}
audrvVoiceInit(&audrenDriver, 0, 2, PcmFormat_Int16, samplerate / fpsRatio);
audrvVoiceSetDestinationMix(&audrenDriver, 0, AUDREN_FINAL_MIX_ID);
audrvVoiceSetMixFactor(&audrenDriver, 0, 1.0f, 0, 0);
audrvVoiceSetMixFactor(&audrenDriver, 0, 0.0f, 0, 1);
audrvVoiceSetMixFactor(&audrenDriver, 0, 0.0f, 1, 0);
audrvVoiceSetMixFactor(&audrenDriver, 0, 1.0f, 1, 1);
audrvUpdate(&audrenDriver);
}
static void _setup(struct mGUIRunner* runner) { static void _setup(struct mGUIRunner* runner) {
_mapKey(&runner->core->inputMap, AUTO_INPUT, HidNpadButton_A, GBA_KEY_A); _mapKey(&runner->core->inputMap, AUTO_INPUT, HidNpadButton_A, GBA_KEY_A);
_mapKey(&runner->core->inputMap, AUTO_INPUT, HidNpadButton_B, GBA_KEY_B); _mapKey(&runner->core->inputMap, AUTO_INPUT, HidNpadButton_B, GBA_KEY_B);
@ -303,14 +317,8 @@ static void _setup(struct mGUIRunner* runner) {
runner->core->setAudioBufferSize(runner->core, SAMPLES); runner->core->setAudioBufferSize(runner->core, SAMPLES);
u32 samplerate = runner->core->audioSampleRate(runner->core); u32 samplerate = runner->core->audioSampleRate(runner->core);
double ratio = mCoreCalculateFramerateRatio(runner->core, 60.0); fpsRatio = mCoreCalculateFramerateRatio(runner->core, 60.0);
audrvVoiceInit(&audrenDriver, 0, 2, PcmFormat_Int16, samplerate / ratio); _resetSampleRate(samplerate);
audrvVoiceSetDestinationMix(&audrenDriver, 0, AUDREN_FINAL_MIX_ID);
audrvVoiceSetMixFactor(&audrenDriver, 0, 1.0f, 0, 0);
audrvVoiceSetMixFactor(&audrenDriver, 0, 0.0f, 0, 1);
audrvVoiceSetMixFactor(&audrenDriver, 0, 0.0f, 1, 0);
audrvVoiceSetMixFactor(&audrenDriver, 0, 1.0f, 1, 1);
audrvUpdate(&audrenDriver);
} }
static void _gameLoaded(struct mGUIRunner* runner) { static void _gameLoaded(struct mGUIRunner* runner) {
@ -572,6 +580,11 @@ static bool _running(struct mGUIRunner* runner) {
return appletMainLoop(); return appletMainLoop();
} }
static void _audioRateChanged(struct mAVStream* stream, uint32_t samplerate) {
UNUSED(stream);
_resetSampleRate(samplerate);
}
static void _postAudioBuffer(struct mAVStream* stream, struct mAudioBuffer* buffer) { static void _postAudioBuffer(struct mAVStream* stream, struct mAudioBuffer* buffer) {
UNUSED(stream); UNUSED(stream);
int i; int i;
@ -891,6 +904,7 @@ int main(int argc, char* argv[]) {
stream.postVideoFrame = NULL; stream.postVideoFrame = NULL;
stream.postAudioFrame = NULL; stream.postAudioFrame = NULL;
stream.postAudioBuffer = _postAudioBuffer; stream.postAudioBuffer = _postAudioBuffer;
stream.audioRateChanged = _audioRateChanged;
size_t i; size_t i;
for (i = 0; i < N_BUFFERS; ++i) { for (i = 0; i < N_BUFFERS; ++i) {