Start filling in audio framework
Jeffrey Pfau jeffrey@endrift.com
Thu, 17 Oct 2013 23:41:11 -0700
3 files changed,
71 insertions(+),
0 deletions(-)
M
src/gba/gba-audio.c
→
src/gba/gba-audio.c
@@ -4,13 +4,23 @@ #include "gba.h"
#include "gba-io.h" #include "gba-thread.h" +#include <limits.h> + const unsigned GBA_AUDIO_SAMPLES = 512; const unsigned GBA_AUDIO_FIFO_SIZE = 8 * sizeof(int32_t); +static int32_t _updateChannel1(struct GBAAudioChannel1* ch); +static int32_t _updateChannel2(struct GBAAudioChannel2* ch); +static int32_t _updateChannel3(struct GBAAudioChannel3* ch); +static int32_t _updateChannel4(struct GBAAudioChannel4* ch); static void _sample(struct GBAAudio* audio); void GBAAudioInit(struct GBAAudio* audio) { audio->nextEvent = 0; + audio->nextCh1 = 0; + audio->nextCh2 = 0; + audio->nextCh3 = 0; + audio->nextCh4 = 0; audio->eventDiff = 0; audio->nextSample = 0; audio->sampleRate = 0x8000;@@ -36,6 +46,27 @@
int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) { audio->nextEvent -= cycles; if (audio->nextEvent <= 0) { + audio->nextCh1 -= audio->eventDiff; + audio->nextCh2 -= audio->eventDiff; + audio->nextCh3 -= audio->eventDiff; + audio->nextCh4 -= audio->eventDiff; + + if ((audio->ch1Right || audio->ch1Left) && audio->nextCh1 <= 0) { + audio->nextCh1 += _updateChannel1(&audio->ch1); + } + + if ((audio->ch2Right || audio->ch2Left) && audio->nextCh2 <= 0) { + audio->nextCh2 += _updateChannel2(&audio->ch2); + } + + if ((audio->ch3Right || audio->ch3Left) && audio->nextCh3 <= 0) { + audio->nextCh3 += _updateChannel3(&audio->ch3); + } + + if ((audio->ch4Right || audio->ch4Left) && audio->nextCh4 <= 0) { + audio->nextCh4 += _updateChannel4(&audio->ch4); + } + audio->nextSample -= audio->eventDiff; if (audio->nextSample <= 0) { _sample(audio);@@ -154,6 +185,22 @@ dma->nextCount = 4;
GBAMemoryServiceDMA(&audio->p->memory, channel->dmaSource, dma); } CircleBufferRead8(&channel->fifo, &channel->sample); +} + +static int32_t _updateChannel1(struct GBAAudioChannel1* ch) { + return INT_MAX / 4; +} + +static int32_t _updateChannel2(struct GBAAudioChannel2* ch) { + return INT_MAX / 4; +} + +static int32_t _updateChannel3(struct GBAAudioChannel3* ch) { + return INT_MAX / 4; +} + +static int32_t _updateChannel4(struct GBAAudioChannel4* ch) { + return INT_MAX / 4; } static void _sample(struct GBAAudio* audio) {
M
src/gba/gba-audio.h
→
src/gba/gba-audio.h
@@ -173,6 +173,10 @@ unsigned sampleRate;
int32_t nextEvent; int32_t eventDiff; + int32_t nextCh1; + int32_t nextCh2; + int32_t nextCh3; + int32_t nextCh4; int32_t nextSample; int32_t sampleInterval;
M
src/gba/gba-io.c
→
src/gba/gba-io.c
@@ -22,33 +22,43 @@
// Audio case REG_SOUND1CNT_LO: GBAAudioWriteSOUND1CNT_LO(&gba->audio, value); + value &= 0x00FF; break; case REG_SOUND1CNT_HI: GBAAudioWriteSOUND1CNT_HI(&gba->audio, value); + value &= 0xFFC0; break; case REG_SOUND1CNT_X: GBAAudioWriteSOUND1CNT_X(&gba->audio, value); + value &= 0x4000; break; case REG_SOUND2CNT_LO: GBAAudioWriteSOUND2CNT_LO(&gba->audio, value); + value &= 0xFFC0; break; case REG_SOUND2CNT_HI: GBAAudioWriteSOUND2CNT_HI(&gba->audio, value); + value &= 0x4000; break; case REG_SOUND3CNT_LO: GBAAudioWriteSOUND3CNT_LO(&gba->audio, value); + value &= 0x00E0; break; case REG_SOUND3CNT_HI: GBAAudioWriteSOUND3CNT_HI(&gba->audio, value); + value &= 0xE000; break; case REG_SOUND3CNT_X: GBAAudioWriteSOUND3CNT_X(&gba->audio, value); + value &= 0x4000; break; case REG_SOUND4CNT_LO: GBAAudioWriteSOUND4CNT_LO(&gba->audio, value); + value &= 0xFF00; break; case REG_SOUND4CNT_HI: GBAAudioWriteSOUND4CNT_HI(&gba->audio, value); + value &= 0x40FF; break; case REG_SOUNDCNT_LO: GBAAudioWriteSOUNDCNT_LO(&gba->audio, value);@@ -284,6 +294,16 @@ case REG_BG0CNT:
case REG_BG1CNT: case REG_BG2CNT: case REG_BG3CNT: + case REG_SOUND1CNT_LO: + case REG_SOUND1CNT_HI: + case REG_SOUND1CNT_X: + case REG_SOUND2CNT_LO: + case REG_SOUND2CNT_HI: + case REG_SOUND3CNT_LO: + case REG_SOUND3CNT_HI: + case REG_SOUND3CNT_X: + case REG_SOUND4CNT_LO: + case REG_SOUND4CNT_HI: case REG_DMA0CNT_HI: case REG_DMA1CNT_HI: case REG_DMA2CNT_HI: