2013-09-30 08:23:58 +00:00
|
|
|
#include "gba-audio.h"
|
|
|
|
|
|
|
|
#include "gba.h"
|
2013-10-02 06:56:10 +00:00
|
|
|
#include "gba-io.h"
|
2013-10-05 09:52:57 +00:00
|
|
|
#include "gba-thread.h"
|
2013-10-02 06:56:10 +00:00
|
|
|
|
2013-10-18 06:41:11 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
2013-10-05 09:52:57 +00:00
|
|
|
const unsigned GBA_AUDIO_SAMPLES = 512;
|
2013-10-02 06:56:10 +00:00
|
|
|
const unsigned GBA_AUDIO_FIFO_SIZE = 8 * sizeof(int32_t);
|
|
|
|
|
2013-10-18 16:33:07 +00:00
|
|
|
static void _updateEnvelope(struct GBAAudioEnvelope* envelope);
|
2013-10-18 06:41:11 +00:00
|
|
|
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);
|
2013-10-02 06:56:10 +00:00
|
|
|
static void _sample(struct GBAAudio* audio);
|
2013-09-30 08:23:58 +00:00
|
|
|
|
|
|
|
void GBAAudioInit(struct GBAAudio* audio) {
|
2013-10-02 06:56:10 +00:00
|
|
|
audio->nextEvent = 0;
|
2013-10-18 06:41:11 +00:00
|
|
|
audio->nextCh1 = 0;
|
|
|
|
audio->nextCh2 = 0;
|
|
|
|
audio->nextCh3 = 0;
|
|
|
|
audio->nextCh4 = 0;
|
2013-10-18 16:33:07 +00:00
|
|
|
audio->ch1.envelope.nextStep = INT_MAX;
|
|
|
|
audio->ch2.envelope.nextStep = INT_MAX;
|
2013-10-18 08:21:44 +00:00
|
|
|
audio->ch4.sample = 0;
|
2013-10-18 16:33:07 +00:00
|
|
|
audio->ch4.envelope.nextStep = INT_MAX;
|
2013-10-02 06:56:10 +00:00
|
|
|
audio->eventDiff = 0;
|
|
|
|
audio->nextSample = 0;
|
|
|
|
audio->sampleRate = 0x8000;
|
2013-10-18 15:10:10 +00:00
|
|
|
audio->soundcntLo = 0;
|
|
|
|
audio->soundcntHi = 0;
|
2013-10-02 06:56:10 +00:00
|
|
|
audio->sampleInterval = GBA_ARM7TDMI_FREQUENCY / audio->sampleRate;
|
|
|
|
|
2013-10-02 09:40:16 +00:00
|
|
|
CircleBufferInit(&audio->left, GBA_AUDIO_SAMPLES * sizeof(int32_t));
|
|
|
|
CircleBufferInit(&audio->right, GBA_AUDIO_SAMPLES * sizeof(int32_t));
|
2013-10-02 06:56:10 +00:00
|
|
|
CircleBufferInit(&audio->chA.fifo, GBA_AUDIO_FIFO_SIZE);
|
|
|
|
CircleBufferInit(&audio->chB.fifo, GBA_AUDIO_FIFO_SIZE);
|
2013-10-03 19:08:52 +00:00
|
|
|
|
|
|
|
pthread_mutex_init(&audio->bufferMutex, 0);
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioDeinit(struct GBAAudio* audio) {
|
2013-10-02 06:56:10 +00:00
|
|
|
CircleBufferDeinit(&audio->left);
|
|
|
|
CircleBufferDeinit(&audio->right);
|
|
|
|
CircleBufferDeinit(&audio->chA.fifo);
|
|
|
|
CircleBufferDeinit(&audio->chB.fifo);
|
2013-10-03 19:08:52 +00:00
|
|
|
|
|
|
|
pthread_mutex_destroy(&audio->bufferMutex);
|
2013-10-02 06:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
|
|
|
|
audio->nextEvent -= cycles;
|
2013-10-18 16:33:07 +00:00
|
|
|
audio->eventDiff += cycles;
|
|
|
|
while (audio->nextEvent <= 0) {
|
2013-10-18 08:21:44 +00:00
|
|
|
audio->nextEvent = INT_MAX;
|
|
|
|
|
2013-10-18 06:41:11 +00:00
|
|
|
audio->nextCh1 -= audio->eventDiff;
|
|
|
|
audio->nextCh2 -= audio->eventDiff;
|
|
|
|
audio->nextCh3 -= audio->eventDiff;
|
|
|
|
audio->nextCh4 -= audio->eventDiff;
|
|
|
|
|
2013-10-18 16:33:07 +00:00
|
|
|
if (audio->ch1.envelope.nextStep != INT_MAX) {
|
|
|
|
audio->ch1.envelope.nextStep -= audio->eventDiff;
|
|
|
|
if (audio->ch1.envelope.nextStep <= 0) {
|
|
|
|
_updateEnvelope(&audio->ch1.envelope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->ch2.envelope.nextStep != INT_MAX) {
|
|
|
|
audio->ch2.envelope.nextStep -= audio->eventDiff;
|
|
|
|
if (audio->ch2.envelope.nextStep <= 0) {
|
|
|
|
_updateEnvelope(&audio->ch2.envelope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->ch4.envelope.nextStep != INT_MAX) {
|
|
|
|
audio->ch4.envelope.nextStep -= audio->eventDiff;
|
|
|
|
if (audio->ch4.envelope.nextStep <= 0) {
|
|
|
|
_updateEnvelope(&audio->ch4.envelope);
|
|
|
|
if (audio->ch4.envelope.nextStep < audio->nextEvent) {
|
|
|
|
audio->nextEvent = audio->nextCh4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-18 06:41:11 +00:00
|
|
|
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);
|
2013-10-18 08:21:44 +00:00
|
|
|
if (audio->nextCh4 < audio->nextEvent) {
|
|
|
|
audio->nextEvent = audio->nextCh4;
|
|
|
|
}
|
2013-10-18 06:41:11 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 06:56:10 +00:00
|
|
|
audio->nextSample -= audio->eventDiff;
|
|
|
|
if (audio->nextSample <= 0) {
|
|
|
|
_sample(audio);
|
|
|
|
audio->nextSample += audio->sampleInterval;
|
|
|
|
}
|
|
|
|
|
2013-10-18 08:21:44 +00:00
|
|
|
if (audio->nextSample < audio->nextEvent) {
|
|
|
|
audio->nextEvent = audio->nextSample;
|
|
|
|
}
|
2013-10-18 16:33:07 +00:00
|
|
|
audio->eventDiff = 0;
|
2013-10-02 06:56:10 +00:00
|
|
|
}
|
|
|
|
return audio->nextEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info) {
|
|
|
|
switch (info->dest) {
|
|
|
|
case BASE_IO | REG_FIFO_A_LO:
|
|
|
|
audio->chA.dmaSource = number;
|
|
|
|
break;
|
|
|
|
case BASE_IO | REG_FIFO_B_LO:
|
|
|
|
audio->chB.dmaSource = number;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GBALog(audio->p, GBA_LOG_GAME_ERROR, "Invalid FIFO destination: 0x%08X", info->dest);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
info->dstControl = DMA_FIXED;
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->ch1.sweep.packed = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value) {
|
2013-10-18 15:10:10 +00:00
|
|
|
audio->ch1.envelope.packed = value;
|
2013-10-18 16:33:07 +00:00
|
|
|
if (audio->ch1.envelope.stepTime) {
|
|
|
|
audio->ch1.envelope.nextStep = 0;
|
|
|
|
} else {
|
|
|
|
audio->ch1.envelope.nextStep = INT_MAX;
|
|
|
|
}
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->ch1.control.packed = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value) {
|
2013-10-18 15:10:10 +00:00
|
|
|
audio->ch2.envelope.packed = value;
|
2013-10-18 16:33:07 +00:00
|
|
|
if (audio->ch2.envelope.stepTime) {
|
|
|
|
audio->ch2.envelope.nextStep = 0;
|
|
|
|
} else {
|
|
|
|
audio->ch2.envelope.nextStep = INT_MAX;
|
|
|
|
}
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->ch2.control.packed = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->ch3.bank.packed = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->ch3.wave.packed = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->ch3.control.packed = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value) {
|
2013-10-18 15:10:10 +00:00
|
|
|
audio->ch4.envelope.packed = value;
|
2013-10-18 16:33:07 +00:00
|
|
|
if (audio->ch4.envelope.stepTime) {
|
|
|
|
audio->ch4.envelope.nextStep = 0;
|
|
|
|
} else {
|
|
|
|
audio->ch4.envelope.nextStep = INT_MAX;
|
|
|
|
}
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->ch4.control.packed = value;
|
2013-10-18 08:21:44 +00:00
|
|
|
if (audio->ch4.control.restart) {
|
2013-10-18 16:33:07 +00:00
|
|
|
audio->ch4.envelope.currentVolume = audio->ch4.envelope.initialVolume;
|
|
|
|
if (audio->ch4.envelope.stepTime) {
|
|
|
|
audio->ch4.envelope.nextStep = 0;
|
|
|
|
} else {
|
|
|
|
audio->ch4.envelope.nextStep = INT_MAX;
|
|
|
|
}
|
2013-10-18 08:21:44 +00:00
|
|
|
if (audio->ch4.control.power) {
|
|
|
|
audio->ch4.lfsr = 0x40;
|
|
|
|
} else {
|
|
|
|
audio->ch4.lfsr = 0x4000;
|
|
|
|
}
|
|
|
|
audio->nextCh4 = 0;
|
|
|
|
}
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value) {
|
2013-10-02 06:56:10 +00:00
|
|
|
audio->soundcntLo = value;
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value) {
|
2013-10-02 06:56:10 +00:00
|
|
|
audio->soundcntHi = value;
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value) {
|
2013-10-02 06:56:10 +00:00
|
|
|
audio->soundcntX = (value & 0xF0) | (audio->soundcntX & 0x0F);
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) {
|
|
|
|
GBALog(audio->p, GBA_LOG_STUB, "Audio unimplemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value) {
|
2013-10-02 06:56:10 +00:00
|
|
|
struct CircleBuffer* fifo;
|
|
|
|
switch (address) {
|
|
|
|
case REG_FIFO_A_LO:
|
|
|
|
fifo = &audio->chA.fifo;
|
|
|
|
break;
|
|
|
|
case REG_FIFO_B_LO:
|
|
|
|
fifo = &audio->chB.fifo;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", address);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (!CircleBufferWrite32(fifo, value)) {
|
|
|
|
int32_t dummy;
|
|
|
|
CircleBufferRead32(fifo, &dummy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId) {
|
|
|
|
struct GBAAudioFIFO* channel;
|
|
|
|
if (fifoId == 0) {
|
|
|
|
channel = &audio->chA;
|
|
|
|
} else if (fifoId == 1) {
|
|
|
|
channel = &audio->chB;
|
|
|
|
} else {
|
|
|
|
GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", fifoId);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-03 08:23:16 +00:00
|
|
|
if (CircleBufferSize(&channel->fifo) <= 4 * sizeof(int32_t)) {
|
2013-10-02 06:56:10 +00:00
|
|
|
struct GBADMA* dma = &audio->p->memory.dma[channel->dmaSource];
|
|
|
|
dma->nextCount = 4;
|
|
|
|
GBAMemoryServiceDMA(&audio->p->memory, channel->dmaSource, dma);
|
|
|
|
}
|
2013-10-02 09:40:16 +00:00
|
|
|
CircleBufferRead8(&channel->fifo, &channel->sample);
|
2013-10-02 06:56:10 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 16:33:07 +00:00
|
|
|
static void _updateEnvelope(struct GBAAudioEnvelope* envelope) {
|
|
|
|
if (envelope->direction) {
|
|
|
|
++envelope->currentVolume;
|
|
|
|
} else {
|
|
|
|
--envelope->currentVolume;
|
|
|
|
}
|
|
|
|
if (envelope->currentVolume >= 15) {
|
|
|
|
envelope->currentVolume = 15;
|
|
|
|
envelope->nextStep = INT_MAX;
|
|
|
|
} else if (envelope->currentVolume <= 0) {
|
|
|
|
envelope->currentVolume = 0;
|
|
|
|
envelope->nextStep = INT_MAX;
|
|
|
|
} else {
|
|
|
|
envelope->nextStep += envelope->stepTime * (GBA_ARM7TDMI_FREQUENCY >> 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-18 06:41:11 +00:00
|
|
|
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) {
|
2013-10-18 08:21:44 +00:00
|
|
|
int lsb = ch->lfsr & 1;
|
2013-10-18 16:33:07 +00:00
|
|
|
ch->sample = lsb * 0x10 - 0x8;
|
|
|
|
ch->sample *= ch->envelope.currentVolume;
|
2013-10-18 08:21:44 +00:00
|
|
|
ch->lfsr >>= 1;
|
|
|
|
ch->lfsr ^= (lsb * 0x60) << (ch->control.power ? 0 : 8);
|
|
|
|
int timing = ch->control.ratio ? 2 * ch->control.ratio : 1;
|
|
|
|
timing <<= ch->control.frequency;
|
|
|
|
timing *= 32;
|
|
|
|
return timing;
|
2013-10-18 06:41:11 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 06:56:10 +00:00
|
|
|
static void _sample(struct GBAAudio* audio) {
|
|
|
|
int32_t sampleLeft = 0;
|
|
|
|
int32_t sampleRight = 0;
|
2013-10-18 08:21:44 +00:00
|
|
|
int psgShift = 2 - audio->volume;
|
|
|
|
|
|
|
|
if (audio->ch4Left) {
|
|
|
|
sampleLeft += audio->ch4.sample >> psgShift;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->ch4Right) {
|
|
|
|
sampleRight += audio->ch4.sample >> psgShift;
|
|
|
|
}
|
2013-10-02 06:56:10 +00:00
|
|
|
|
2013-10-02 09:40:16 +00:00
|
|
|
if (audio->chALeft) {
|
2013-10-19 02:44:55 +00:00
|
|
|
sampleLeft += (audio->chA.sample << 2) >> !audio->volumeChA;
|
2013-10-02 09:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->chARight) {
|
2013-10-19 02:44:55 +00:00
|
|
|
sampleRight += (audio->chA.sample << 2) >> !audio->volumeChA;
|
2013-10-02 09:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->chBLeft) {
|
2013-10-19 02:44:55 +00:00
|
|
|
sampleLeft += (audio->chB.sample << 2) >> !audio->volumeChB;
|
2013-10-02 09:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->chBRight) {
|
2013-10-19 02:44:55 +00:00
|
|
|
sampleRight += (audio->chB.sample << 2) >> !audio->volumeChB;
|
2013-10-02 09:40:16 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 19:08:52 +00:00
|
|
|
pthread_mutex_lock(&audio->bufferMutex);
|
2013-10-05 09:52:57 +00:00
|
|
|
while (CircleBufferSize(&audio->left) + (GBA_AUDIO_SAMPLES * 2 / 5) >= audio->left.capacity) {
|
2013-10-09 05:36:55 +00:00
|
|
|
if (!audio->p->sync->audioWait) {
|
|
|
|
break;
|
|
|
|
}
|
2013-10-05 09:52:57 +00:00
|
|
|
GBASyncProduceAudio(audio->p->sync, &audio->bufferMutex);
|
|
|
|
}
|
2013-10-02 06:56:10 +00:00
|
|
|
CircleBufferWrite32(&audio->left, sampleLeft);
|
|
|
|
CircleBufferWrite32(&audio->right, sampleRight);
|
2013-10-03 19:08:52 +00:00
|
|
|
pthread_mutex_unlock(&audio->bufferMutex);
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|