diff --git a/src/gb/audio.c b/src/gb/audio.c index 4d08fcacf..c996a6e94 100644 --- a/src/gb/audio.c +++ b/src/gb/audio.c @@ -14,6 +14,7 @@ const uint32_t DMG_LR35902_FREQUENCY = 0x400000; static const int CLOCKS_PER_BLIP_FRAME = 0x1000; static const unsigned BLIP_BUFFER_SIZE = 0x4000; +const int GB_AUDIO_VOLUME_MAX = 0x100; static void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value); static bool _writeSweep(struct GBAudioEnvelope* envelope, uint8_t value); @@ -38,6 +39,7 @@ void GBAudioInit(struct GBAudio* audio, size_t samples) { audio->forceDisableCh[1] = false; audio->forceDisableCh[2] = false; audio->forceDisableCh[3] = false; + audio->masterVolume = GB_AUDIO_VOLUME_MAX; } void GBAudioDeinit(struct GBAudio* audio) { @@ -641,8 +643,8 @@ void _sample(struct GBAudio* audio, int32_t cycles) { int16_t sampleLeft = 0; int16_t sampleRight = 0; GBAudioSamplePSG(audio, &sampleLeft, &sampleRight); - sampleLeft <<= 1; - sampleRight <<= 1; + sampleLeft = (sampleLeft * audio->masterVolume) >> 6; + sampleRight = (sampleRight * audio->masterVolume) >> 6; mCoreSyncLockAudio(audio->p->sync); unsigned produced; diff --git a/src/gb/audio.h b/src/gb/audio.h index e36249b05..7de8592ae 100644 --- a/src/gb/audio.h +++ b/src/gb/audio.h @@ -183,6 +183,7 @@ struct GBAudio { size_t samples; bool forceDisableCh[4]; + int masterVolume; }; void GBAudioInit(struct GBAudio* audio, size_t samples); diff --git a/src/gb/core.c b/src/gb/core.c index ca7b06109..e89eb0921 100644 --- a/src/gb/core.c +++ b/src/gb/core.c @@ -66,9 +66,10 @@ static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) { } static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) { - UNUSED(core); UNUSED(config); - // TODO + + struct GB* gb = core->board; + gb->audio.masterVolume = core->opts.volume; } static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) { diff --git a/src/gba/core.c b/src/gba/core.c index c210278b3..33b5778cb 100644 --- a/src/gba/core.c +++ b/src/gba/core.c @@ -76,6 +76,7 @@ static void _GBACoreSetSync(struct mCore* core, struct mCoreSync* sync) { static void _GBACoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) { struct GBA* gba = core->board; + gba->audio.masterVolume = core->opts.volume; #if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2 struct GBACore* gbacore = (struct GBACore*) core;