From cc8eeb4353246cceb598e2de2829a1ce965d5281 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 19 Oct 2013 03:04:45 -0700 Subject: [PATCH] Start audio channel 3 --- src/gba/gba-audio.c | 54 +++++++++++++++++++++++++++++++++++++++++++-- src/gba/gba-audio.h | 7 +++--- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/gba/gba-audio.c b/src/gba/gba-audio.c index 42d247b43..7cad4d310 100644 --- a/src/gba/gba-audio.c +++ b/src/gba/gba-audio.c @@ -33,6 +33,8 @@ void GBAAudioInit(struct GBAAudio* audio) { audio->ch2.envelope.nextStep = INT_MAX; audio->ch2.control.nextStep = 0; audio->ch2.sample = 0; + audio->ch3.bank.packed = 0; + audio->ch3.sample = 0; audio->ch4.sample = 0; audio->ch4.envelope.nextStep = INT_MAX; audio->eventDiff = 0; @@ -281,7 +283,7 @@ void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value) { } void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) { - GBALog(audio->p, GBA_LOG_STUB, "Audio unimplemented"); + audio->ch3.wavedata[address | (!audio->ch3.bank.bank * 4)] = value; } void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value) { @@ -390,7 +392,47 @@ static int32_t _updateChannel2(struct GBAAudioChannel2* ch) { } static int32_t _updateChannel3(struct GBAAudioChannel3* ch) { - return INT_MAX / 4; + int i; + int start; + int end; + int volume; + switch (ch->wave.volume) { + case 0: + volume = 0; + break; + case 1: + volume = 4; + break; + case 2: + volume = 2; + break; + case 3: + volume = 1; + break; + default: + volume = 3; + break; + } + if (ch->bank.size) { + start = 7; + end = 0; + } else if (ch->bank.bank) { + start = 7; + end = 4; + } else { + start = 3; + end = 0; + } + uint32_t bitsCarry = ch->wavedata[end] & 0xF0000000; + uint32_t bits; + for (i = start; i >= end; --i) { + bits = ch->wavedata[i] & 0xF0000000; + ch->wavedata[i] <<= 4; + ch->wavedata[i] |= bitsCarry >> 28; + bitsCarry = bits; + } + ch->sample = ((bitsCarry >> 26) - 0x20) * volume; + return 16 * (2048 - ch->control.rate); } static int32_t _updateChannel4(struct GBAAudioChannel4* ch) { @@ -426,6 +468,14 @@ static void _sample(struct GBAAudio* audio) { sampleRight += audio->ch2.sample >> psgShift; } + if (audio->ch3Left) { + sampleLeft += audio->ch3.sample >> psgShift; + } + + if (audio->ch3Right) { + sampleRight += audio->ch3.sample >> psgShift; + } + if (audio->ch4Left) { sampleLeft += audio->ch4.sample >> psgShift; } diff --git a/src/gba/gba-audio.h b/src/gba/gba-audio.h index eee3a5989..7f3e4a549 100644 --- a/src/gba/gba-audio.h +++ b/src/gba/gba-audio.h @@ -69,7 +69,7 @@ struct GBAAudioChannel3 { unsigned : 5; unsigned size : 1; unsigned bank : 1; - unsigned disable : 1; + unsigned enable : 1; unsigned : 7; }; uint16_t packed; @@ -80,8 +80,6 @@ struct GBAAudioChannel3 { unsigned length : 8; unsigned : 5; unsigned volume : 3; - unsigned disable : 1; - unsigned : 7; }; uint16_t packed; } wave; @@ -95,6 +93,9 @@ struct GBAAudioChannel3 { }; uint16_t packed; } control; + + uint32_t wavedata[8]; + int8_t sample; }; struct GBAAudioChannel4 {