From 2af1bf68e0a0833d368859d1c1bf4a04f8833552 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Mon, 8 Feb 2016 21:21:17 -0800 Subject: [PATCH] Core: Add core.setAudioBufferSize --- src/core/core.h | 1 + src/gb/audio.c | 9 +++++++++ src/gb/audio.h | 2 ++ src/gb/core.c | 6 ++++++ src/gba/core.c | 6 ++++++ src/platform/3ds/main.c | 2 +- src/platform/libretro/libretro.c | 4 ++-- src/platform/openemu/mGBAGameCore.m | 2 +- src/platform/qt/GameController.cpp | 2 +- src/platform/wii/main.c | 2 +- 10 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/core/core.h b/src/core/core.h index b12281d4c..9c5adaaa5 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -54,6 +54,7 @@ struct mCore { void (*getVideoBuffer)(struct mCore*, color_t** buffer, size_t* stride); struct blip_t* (*getAudioChannel)(struct mCore*, int ch); + void (*setAudioBufferSize)(struct mCore*, size_t samples); void (*setAVStream)(struct mCore*, struct mAVStream*); diff --git a/src/gb/audio.c b/src/gb/audio.c index e0324dcb7..0b9f5df3a 100644 --- a/src/gb/audio.c +++ b/src/gb/audio.c @@ -76,6 +76,15 @@ void GBAudioReset(struct GBAudio* audio) { audio->playingCh4 = false; } +void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples) { + mCoreSyncLockAudio(audio->p->sync); + audio->samples = samples; + blip_clear(audio->left); + blip_clear(audio->right); + audio->clock = 0; + mCoreSyncConsumeAudio(audio->p->sync); +} + void GBAudioWriteNR10(struct GBAudio* audio, uint8_t value) { audio->ch1.shift = GBAudioRegisterSquareSweepGetShift(value); audio->ch1.direction = GBAudioRegisterSquareSweepGetDirection(value); diff --git a/src/gb/audio.h b/src/gb/audio.h index e9e66c18b..08ab44697 100644 --- a/src/gb/audio.h +++ b/src/gb/audio.h @@ -187,6 +187,8 @@ void GBAudioInit(struct GBAudio* audio, size_t samples); void GBAudioDeinit(struct GBAudio* audio); void GBAudioReset(struct GBAudio* audio); +void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples); + void GBAudioWriteNR10(struct GBAudio* audio, uint8_t); void GBAudioWriteNR11(struct GBAudio* audio, uint8_t); void GBAudioWriteNR12(struct GBAudio* audio, uint8_t); diff --git a/src/gb/core.c b/src/gb/core.c index 33e1dd71c..1b063491b 100644 --- a/src/gb/core.c +++ b/src/gb/core.c @@ -96,6 +96,11 @@ static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) { } } +static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) { + struct GB* gb = core->board; + GBAudioResizeBuffer(&gb->audio, samples); +} + static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) { // TODO } @@ -225,6 +230,7 @@ struct mCore* GBCoreCreate(void) { core->setVideoBuffer = _GBCoreSetVideoBuffer; core->getVideoBuffer = _GBCoreGetVideoBuffer; core->getAudioChannel = _GBCoreGetAudioChannel; + core->setAudioBufferSize = _GBCoreSetAudioBufferSize; core->setAVStream = _GBCoreSetAVStream; core->isROM = GBIsROM; core->loadROM = _GBCoreLoadROM; diff --git a/src/gba/core.c b/src/gba/core.c index 99c0c181d..b7f11c898 100644 --- a/src/gba/core.c +++ b/src/gba/core.c @@ -127,6 +127,11 @@ static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) { } } +static void _GBACoreSetAudioBufferSize(struct mCore* core, size_t samples) { + struct GBA* gba = core->board; + GBAAudioResizeBuffer(&gba->audio, samples); +} + static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) { struct GBA* gba = core->board; gba->stream = stream; @@ -261,6 +266,7 @@ struct mCore* GBACoreCreate(void) { core->setVideoBuffer = _GBACoreSetVideoBuffer; core->getVideoBuffer = _GBACoreGetVideoBuffer; core->getAudioChannel = _GBACoreGetAudioChannel; + core->setAudioBufferSize = _GBACoreSetAudioBufferSize; core->setAVStream = _GBACoreSetAVStream; core->isROM = GBAIsROM; core->loadROM = _GBACoreLoadROM; diff --git a/src/platform/3ds/main.c b/src/platform/3ds/main.c index 557c91b82..c5f92f2ce 100644 --- a/src/platform/3ds/main.c +++ b/src/platform/3ds/main.c @@ -273,7 +273,7 @@ static void _setup(struct mGUIRunner* runner) { screenMode = mode; } - GBAAudioResizeBuffer(&((struct GBA*) runner->core->board)->audio, AUDIO_SAMPLES); + runner->core->setAudioBufferSize(runner->core, AUDIO_SAMPLES); } static void _gameLoaded(struct mGUIRunner* runner) { diff --git a/src/platform/libretro/libretro.c b/src/platform/libretro/libretro.c index 944dd4704..2066dd891 100644 --- a/src/platform/libretro/libretro.c +++ b/src/platform/libretro/libretro.c @@ -205,7 +205,7 @@ void retro_init(void) { mCoreInitConfig(core, NULL); core->init(core); struct GBA* gba = core->board; - gba->stream = &stream; + core->setAVStream(core, &stream); if (rumbleCallback) { gba->rumble = &rumble; } @@ -224,7 +224,7 @@ void retro_init(void) { outputBuffer = malloc(256 * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); core->setVideoBuffer(core, outputBuffer, 256); - GBAAudioResizeBuffer(&gba->audio, SAMPLES); + core->setAudioBufferSize(core, SAMPLES); blip_set_rates(core->getAudioChannel(core, 0), GBA_ARM7TDMI_FREQUENCY, 32768); blip_set_rates(core->getAudioChannel(core, 1), GBA_ARM7TDMI_FREQUENCY, 32768); diff --git a/src/platform/openemu/mGBAGameCore.m b/src/platform/openemu/mGBAGameCore.m index d476cf2e2..4eaa46890 100644 --- a/src/platform/openemu/mGBAGameCore.m +++ b/src/platform/openemu/mGBAGameCore.m @@ -73,9 +73,9 @@ core->desiredVideoDimensions(core, &width, &height); outputBuffer = malloc(width * height * BYTES_PER_PIXEL); core->setVideoBuffer(core, outputBuffer, width); + core->setAudioBufferSize(core, SAMPLES); gba = core->board; - GBAAudioResizeBuffer(&gba->audio, SAMPLES); GBACheatDeviceCreate(&cheats); GBACheatAttachDevice(gba, &cheats); cheatSets = [[NSMutableDictionary alloc] init]; diff --git a/src/platform/qt/GameController.cpp b/src/platform/qt/GameController.cpp index d46d5f76c..e28e3a029 100644 --- a/src/platform/qt/GameController.cpp +++ b/src/platform/qt/GameController.cpp @@ -932,7 +932,7 @@ void GameController::updateKeys() { void GameController::redoSamples(int samples) { // TODO: Put back audio buffers if (m_threadContext.core) { - GBAAudioResizeBuffer(&static_cast(m_threadContext.core->board)->audio, samples); + m_threadContext.core->setAudioBufferSize(m_threadContext.core, samples); } QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged"); } diff --git a/src/platform/wii/main.c b/src/platform/wii/main.c index 3e3154df3..466a317da 100644 --- a/src/platform/wii/main.c +++ b/src/platform/wii/main.c @@ -569,7 +569,7 @@ void _setup(struct mGUIRunner* runner) { outputBuffer = memalign(32, 256 * 256 * BYTES_PER_PIXEL); runner->core->setVideoBuffer(runner->core, outputBuffer, 256); - GBAAudioResizeBuffer(&((struct GBA*) runner->core->board)->audio, SAMPLES); + runner->core->setAudioBufferSize(runner->core, SAMPLES); double ratio = GBAAudioCalculateRatio(1, 60 / 1.001, 1); blip_set_rates(runner->core->getAudioChannel(runner->core, 0), GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);