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"
|
2014-01-26 02:01:31 +00:00
|
|
|
#include "gba-serialize.h"
|
2013-10-05 09:52:57 +00:00
|
|
|
#include "gba-thread.h"
|
2014-07-21 06:24:02 +00:00
|
|
|
#include "gba-video.h"
|
2013-10-02 06:56:10 +00:00
|
|
|
|
2014-07-21 07:29:38 +00:00
|
|
|
const unsigned GBA_AUDIO_SAMPLES = 2048;
|
2013-10-02 06:56:10 +00:00
|
|
|
const unsigned GBA_AUDIO_FIFO_SIZE = 8 * sizeof(int32_t);
|
2013-10-19 06:12:14 +00:00
|
|
|
#define SWEEP_CYCLES (GBA_ARM7TDMI_FREQUENCY / 128)
|
2013-10-02 06:56:10 +00:00
|
|
|
|
2013-10-19 06:12:14 +00:00
|
|
|
static int32_t _updateSquareChannel(struct GBAAudioSquareControl* envelope, int duty);
|
2013-10-18 16:33:07 +00:00
|
|
|
static void _updateEnvelope(struct GBAAudioEnvelope* envelope);
|
2014-07-16 07:46:02 +00:00
|
|
|
static bool _updateSweep(struct GBAAudioChannel1* ch);
|
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);
|
2014-02-03 13:22:29 +00:00
|
|
|
static int _applyBias(struct GBAAudio* audio, int sample);
|
2013-10-02 06:56:10 +00:00
|
|
|
static void _sample(struct GBAAudio* audio);
|
2013-09-30 08:23:58 +00:00
|
|
|
|
2014-07-21 07:14:48 +00:00
|
|
|
void GBAAudioInit(struct GBAAudio* audio, size_t samples) {
|
|
|
|
CircleBufferInit(&audio->left, samples * sizeof(int32_t));
|
|
|
|
CircleBufferInit(&audio->right, samples * sizeof(int32_t));
|
2014-07-19 21:34:24 +00:00
|
|
|
CircleBufferInit(&audio->chA.fifo, GBA_AUDIO_FIFO_SIZE);
|
|
|
|
CircleBufferInit(&audio->chB.fifo, GBA_AUDIO_FIFO_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioReset(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;
|
2014-01-20 22:32:13 +00:00
|
|
|
audio->ch1.sweep.time = 0;
|
2013-10-18 16:33:07 +00:00
|
|
|
audio->ch1.envelope.nextStep = INT_MAX;
|
2013-10-19 06:12:14 +00:00
|
|
|
audio->ch1.control.nextStep = 0;
|
2014-01-20 22:32:13 +00:00
|
|
|
audio->ch1.control.endTime = 0;
|
2013-10-19 06:12:14 +00:00
|
|
|
audio->ch1.nextSweep = INT_MAX;
|
|
|
|
audio->ch1.sample = 0;
|
2013-10-18 16:33:07 +00:00
|
|
|
audio->ch2.envelope.nextStep = INT_MAX;
|
2013-10-19 06:12:14 +00:00
|
|
|
audio->ch2.control.nextStep = 0;
|
2014-01-20 22:32:13 +00:00
|
|
|
audio->ch2.control.endTime = 0;
|
2013-10-19 06:12:14 +00:00
|
|
|
audio->ch2.sample = 0;
|
2013-10-19 10:04:45 +00:00
|
|
|
audio->ch3.bank.packed = 0;
|
2014-01-20 22:32:13 +00:00
|
|
|
audio->ch3.control.endTime = 0;
|
2013-10-19 10:04:45 +00:00
|
|
|
audio->ch3.sample = 0;
|
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;
|
2014-02-03 13:22:29 +00:00
|
|
|
audio->soundbias = 0x200;
|
2013-10-18 15:10:10 +00:00
|
|
|
audio->soundcntLo = 0;
|
|
|
|
audio->soundcntHi = 0;
|
2013-10-20 09:18:09 +00:00
|
|
|
audio->soundcntX = 0;
|
2013-10-02 06:56:10 +00:00
|
|
|
audio->sampleInterval = GBA_ARM7TDMI_FREQUENCY / audio->sampleRate;
|
|
|
|
|
2014-07-19 21:34:24 +00:00
|
|
|
CircleBufferClear(&audio->left);
|
|
|
|
CircleBufferClear(&audio->right);
|
|
|
|
CircleBufferClear(&audio->chA.fifo);
|
|
|
|
CircleBufferClear(&audio->chB.fifo);
|
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);
|
|
|
|
}
|
|
|
|
|
2014-07-21 07:14:48 +00:00
|
|
|
void GBAAudioResizeBuffer(struct GBAAudio* audio, size_t samples) {
|
2014-07-22 09:33:45 +00:00
|
|
|
if (samples > GBA_AUDIO_SAMPLES) {
|
2014-07-21 07:14:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GBASyncLockAudio(audio->p->sync);
|
|
|
|
int32_t buffer[GBA_AUDIO_SAMPLES];
|
|
|
|
int32_t dummy;
|
|
|
|
size_t read;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
read = CircleBufferDump(&audio->left, buffer, sizeof(buffer));
|
|
|
|
CircleBufferDeinit(&audio->left);
|
|
|
|
CircleBufferInit(&audio->left, samples * sizeof(int32_t));
|
|
|
|
for (i = 0; i * sizeof(int32_t) < read; ++i) {
|
|
|
|
if (!CircleBufferWrite32(&audio->left, buffer[i])) {
|
|
|
|
CircleBufferRead32(&audio->left, &dummy);
|
|
|
|
CircleBufferWrite32(&audio->left, buffer[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
read = CircleBufferDump(&audio->right, buffer, sizeof(buffer));
|
|
|
|
CircleBufferDeinit(&audio->right);
|
|
|
|
CircleBufferInit(&audio->right, samples * sizeof(int32_t));
|
|
|
|
for (i = 0; i * sizeof(int32_t) < read; ++i) {
|
|
|
|
if (!CircleBufferWrite32(&audio->right, buffer[i])) {
|
|
|
|
CircleBufferRead32(&audio->right, &dummy);
|
|
|
|
CircleBufferWrite32(&audio->right, buffer[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GBASyncUnlockAudio(audio->p->sync);
|
|
|
|
}
|
|
|
|
|
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;
|
2014-07-09 05:00:53 +00:00
|
|
|
if (audio->nextEvent <= 0) {
|
2013-10-18 08:21:44 +00:00
|
|
|
audio->nextEvent = INT_MAX;
|
2013-10-20 09:18:09 +00:00
|
|
|
if (audio->enable) {
|
2013-10-26 11:08:18 +00:00
|
|
|
if (audio->playingCh1 && !audio->ch1.envelope.dead) {
|
|
|
|
audio->nextCh1 -= audio->eventDiff;
|
|
|
|
if (audio->ch1.envelope.nextStep != INT_MAX) {
|
|
|
|
audio->ch1.envelope.nextStep -= audio->eventDiff;
|
|
|
|
if (audio->ch1.envelope.nextStep <= 0) {
|
2013-10-29 09:19:58 +00:00
|
|
|
int8_t sample = audio->ch1.control.hi * 0x10 - 0x8;
|
2013-10-26 11:08:18 +00:00
|
|
|
_updateEnvelope(&audio->ch1.envelope);
|
|
|
|
if (audio->ch1.envelope.nextStep < audio->nextEvent) {
|
|
|
|
audio->nextEvent = audio->ch1.envelope.nextStep;
|
|
|
|
}
|
2013-10-29 09:19:58 +00:00
|
|
|
audio->ch1.sample = sample * audio->ch1.envelope.currentVolume;
|
2013-10-20 09:18:09 +00:00
|
|
|
}
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:33:37 +00:00
|
|
|
if (audio->ch1.nextSweep != INT_MAX) {
|
|
|
|
audio->ch1.nextSweep -= audio->eventDiff;
|
|
|
|
if (audio->ch1.nextSweep <= 0) {
|
|
|
|
audio->playingCh1 = _updateSweep(&audio->ch1);
|
|
|
|
if (audio->ch1.nextSweep < audio->nextEvent) {
|
|
|
|
audio->nextEvent = audio->ch1.nextSweep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->nextCh1 <= 0) {
|
|
|
|
audio->nextCh1 += _updateChannel1(&audio->ch1);
|
|
|
|
if (audio->nextCh1 < audio->nextEvent) {
|
|
|
|
audio->nextEvent = audio->nextCh1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->ch1.control.stop) {
|
|
|
|
audio->ch1.control.endTime -= audio->eventDiff;
|
|
|
|
if (audio->ch1.control.endTime <= 0) {
|
|
|
|
audio->playingCh1 = 0;
|
2013-10-20 09:18:09 +00:00
|
|
|
}
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
2013-10-18 16:33:07 +00:00
|
|
|
}
|
|
|
|
|
2013-10-26 11:08:18 +00:00
|
|
|
if (audio->playingCh2 && !audio->ch2.envelope.dead) {
|
|
|
|
audio->nextCh2 -= audio->eventDiff;
|
|
|
|
if (audio->ch2.envelope.nextStep != INT_MAX) {
|
|
|
|
audio->ch2.envelope.nextStep -= audio->eventDiff;
|
|
|
|
if (audio->ch2.envelope.nextStep <= 0) {
|
2013-10-29 09:19:58 +00:00
|
|
|
int8_t sample = audio->ch2.control.hi * 0x10 - 0x8;
|
2013-10-26 11:08:18 +00:00
|
|
|
_updateEnvelope(&audio->ch2.envelope);
|
|
|
|
if (audio->ch2.envelope.nextStep < audio->nextEvent) {
|
|
|
|
audio->nextEvent = audio->ch2.envelope.nextStep;
|
|
|
|
}
|
2013-10-29 09:19:58 +00:00
|
|
|
audio->ch2.sample = sample * audio->ch2.envelope.currentVolume;
|
2013-10-20 09:18:09 +00:00
|
|
|
}
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
2013-10-18 16:33:07 +00:00
|
|
|
|
2013-10-20 23:33:37 +00:00
|
|
|
if (audio->nextCh2 <= 0) {
|
|
|
|
audio->nextCh2 += _updateChannel2(&audio->ch2);
|
|
|
|
if (audio->nextCh2 < audio->nextEvent) {
|
|
|
|
audio->nextEvent = audio->nextCh2;
|
2013-10-20 09:18:09 +00:00
|
|
|
}
|
2013-10-18 16:33:07 +00:00
|
|
|
}
|
|
|
|
|
2013-10-20 23:33:37 +00:00
|
|
|
if (audio->ch2.control.stop) {
|
|
|
|
audio->ch2.control.endTime -= audio->eventDiff;
|
|
|
|
if (audio->ch2.control.endTime <= 0) {
|
|
|
|
audio->playingCh2 = 0;
|
|
|
|
}
|
2013-10-20 09:18:09 +00:00
|
|
|
}
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
2013-10-18 06:41:11 +00:00
|
|
|
|
2013-10-20 23:33:37 +00:00
|
|
|
if (audio->playingCh3) {
|
2013-10-26 11:08:18 +00:00
|
|
|
audio->nextCh3 -= audio->eventDiff;
|
2013-10-20 23:33:37 +00:00
|
|
|
if (audio->nextCh3 <= 0) {
|
|
|
|
audio->nextCh3 += _updateChannel3(&audio->ch3);
|
|
|
|
if (audio->nextCh3 < audio->nextEvent) {
|
|
|
|
audio->nextEvent = audio->nextCh3;
|
|
|
|
}
|
2013-10-20 09:18:09 +00:00
|
|
|
}
|
2013-10-18 06:41:11 +00:00
|
|
|
|
2013-10-20 23:33:37 +00:00
|
|
|
if (audio->ch3.control.stop) {
|
|
|
|
audio->ch3.control.endTime -= audio->eventDiff;
|
|
|
|
if (audio->ch3.control.endTime <= 0) {
|
|
|
|
audio->playingCh3 = 0;
|
|
|
|
}
|
2013-10-20 09:18:09 +00:00
|
|
|
}
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
2013-10-18 06:41:11 +00:00
|
|
|
|
2013-10-26 11:08:18 +00:00
|
|
|
if (audio->playingCh4 && !audio->ch4.envelope.dead) {
|
|
|
|
audio->nextCh4 -= audio->eventDiff;
|
|
|
|
if (audio->ch4.envelope.nextStep != INT_MAX) {
|
|
|
|
audio->ch4.envelope.nextStep -= audio->eventDiff;
|
|
|
|
if (audio->ch4.envelope.nextStep <= 0) {
|
2013-10-29 09:19:58 +00:00
|
|
|
int8_t sample = (audio->ch4.sample >> 31) * 0x8;
|
2013-10-26 11:08:18 +00:00
|
|
|
_updateEnvelope(&audio->ch4.envelope);
|
|
|
|
if (audio->ch4.envelope.nextStep < audio->nextEvent) {
|
|
|
|
audio->nextEvent = audio->ch4.envelope.nextStep;
|
|
|
|
}
|
2013-10-29 09:19:58 +00:00
|
|
|
audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
|
2013-10-20 23:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->nextCh4 <= 0) {
|
|
|
|
audio->nextCh4 += _updateChannel4(&audio->ch4);
|
|
|
|
if (audio->nextCh4 < audio->nextEvent) {
|
|
|
|
audio->nextEvent = audio->nextCh4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->ch4.control.stop) {
|
|
|
|
audio->ch4.control.endTime -= audio->eventDiff;
|
|
|
|
if (audio->ch4.control.endTime <= 0) {
|
|
|
|
audio->playingCh4 = 0;
|
|
|
|
}
|
2013-10-20 09:18:09 +00:00
|
|
|
}
|
2013-10-18 08:21:44 +00:00
|
|
|
}
|
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;
|
2013-10-19 07:48:57 +00:00
|
|
|
if (audio->ch1.sweep.time) {
|
|
|
|
audio->ch1.nextSweep = audio->ch1.sweep.time * SWEEP_CYCLES;
|
|
|
|
} else {
|
2013-10-19 06:12:14 +00:00
|
|
|
audio->ch1.nextSweep = INT_MAX;
|
|
|
|
}
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value) {
|
2013-10-18 15:10:10 +00:00
|
|
|
audio->ch1.envelope.packed = value;
|
2013-10-26 11:23:08 +00:00
|
|
|
audio->ch1.envelope.dead = 0;
|
2013-10-26 11:08:18 +00:00
|
|
|
if (audio->ch1.envelope.stepTime) {
|
|
|
|
audio->ch1.envelope.nextStep = 0;
|
|
|
|
} else {
|
|
|
|
audio->ch1.envelope.nextStep = INT_MAX;
|
2013-10-26 11:23:08 +00:00
|
|
|
if (audio->ch1.envelope.initialVolume == 0) {
|
|
|
|
audio->ch1.envelope.dead = 1;
|
2013-10-29 09:19:58 +00:00
|
|
|
audio->ch1.sample = 0;
|
2013-10-26 11:23:08 +00:00
|
|
|
}
|
2013-10-26 11:08:18 +00:00
|
|
|
}
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->ch1.control.packed = value;
|
2013-10-20 23:33:37 +00:00
|
|
|
audio->ch1.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch1.envelope.length)) >> 8;
|
2013-10-19 06:12:14 +00:00
|
|
|
if (audio->ch1.control.restart) {
|
|
|
|
if (audio->ch1.sweep.time) {
|
|
|
|
audio->ch1.nextSweep = audio->ch1.sweep.time * SWEEP_CYCLES;
|
2013-10-19 07:48:57 +00:00
|
|
|
} else {
|
|
|
|
audio->ch1.nextSweep = INT_MAX;
|
|
|
|
}
|
2013-10-20 23:33:37 +00:00
|
|
|
if (!audio->playingCh1) {
|
2013-10-19 07:48:57 +00:00
|
|
|
audio->nextCh1 = 0;
|
|
|
|
}
|
2013-10-20 23:33:37 +00:00
|
|
|
audio->playingCh1 = 1;
|
2013-10-19 07:48:57 +00:00
|
|
|
if (audio->ch1.envelope.stepTime) {
|
|
|
|
audio->ch1.envelope.nextStep = 0;
|
|
|
|
} else {
|
|
|
|
audio->ch1.envelope.nextStep = INT_MAX;
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
|
|
|
audio->ch1.envelope.currentVolume = audio->ch1.envelope.initialVolume;
|
|
|
|
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 GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value) {
|
2013-10-18 15:10:10 +00:00
|
|
|
audio->ch2.envelope.packed = value;
|
2013-10-26 11:23:08 +00:00
|
|
|
audio->ch2.envelope.dead = 0;
|
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-10-26 11:23:08 +00:00
|
|
|
if (audio->ch2.envelope.initialVolume == 0) {
|
|
|
|
audio->ch2.envelope.dead = 1;
|
2013-10-29 09:19:58 +00:00
|
|
|
audio->ch2.sample = 0;
|
2013-10-26 11:23:08 +00:00
|
|
|
}
|
2013-10-18 16:33:07 +00:00
|
|
|
}
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->ch2.control.packed = value;
|
2013-10-20 23:33:37 +00:00
|
|
|
audio->ch1.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch2.envelope.length)) >> 8;
|
2013-10-19 06:12:14 +00:00
|
|
|
if (audio->ch2.control.restart) {
|
2013-10-20 23:33:37 +00:00
|
|
|
audio->playingCh2 = 1;
|
2013-10-19 06:12:14 +00:00
|
|
|
audio->ch2.envelope.currentVolume = audio->ch2.envelope.initialVolume;
|
|
|
|
if (audio->ch2.envelope.stepTime) {
|
|
|
|
audio->ch2.envelope.nextStep = 0;
|
|
|
|
} else {
|
|
|
|
audio->ch2.envelope.nextStep = INT_MAX;
|
|
|
|
}
|
|
|
|
audio->nextCh2 = 0;
|
|
|
|
}
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->ch3.bank.packed = value;
|
2013-10-29 09:19:58 +00:00
|
|
|
if (audio->ch3.control.endTime >= 0) {
|
|
|
|
audio->playingCh3 = audio->ch3.bank.enable;
|
|
|
|
}
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-10-20 23:33:37 +00:00
|
|
|
audio->ch3.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (256 - audio->ch3.wave.length)) >> 8;
|
|
|
|
if (audio->ch3.control.restart) {
|
2013-10-29 09:19:58 +00:00
|
|
|
audio->playingCh3 = audio->ch3.bank.enable;
|
2013-10-20 23:33:37 +00:00
|
|
|
}
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value) {
|
2013-10-18 15:10:10 +00:00
|
|
|
audio->ch4.envelope.packed = value;
|
2013-10-26 11:23:08 +00:00
|
|
|
audio->ch4.envelope.dead = 0;
|
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-10-26 11:23:08 +00:00
|
|
|
if (audio->ch4.envelope.initialVolume == 0) {
|
|
|
|
audio->ch4.envelope.dead = 1;
|
2013-10-29 09:19:58 +00:00
|
|
|
audio->ch4.sample = 0;
|
2013-10-26 11:23:08 +00:00
|
|
|
}
|
2013-10-18 16:33:07 +00:00
|
|
|
}
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->ch4.control.packed = value;
|
2013-10-20 23:33:37 +00:00
|
|
|
audio->ch4.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch4.envelope.length)) >> 8;
|
2013-10-18 08:21:44 +00:00
|
|
|
if (audio->ch4.control.restart) {
|
2013-10-20 23:33:37 +00:00
|
|
|
audio->playingCh4 = 1;
|
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-20 23:33:37 +00:00
|
|
|
audio->soundcntX = (value & 0x80) | (audio->soundcntX & 0x0F);
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 13:22:29 +00:00
|
|
|
void GBAAudioWriteSOUNDBIAS(struct GBAAudio* audio, uint16_t value) {
|
|
|
|
audio->soundbias = value;
|
|
|
|
}
|
|
|
|
|
2013-09-30 08:23:58 +00:00
|
|
|
void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) {
|
2013-10-19 10:04:45 +00:00
|
|
|
audio->ch3.wavedata[address | (!audio->ch3.bank.bank * 4)] = value;
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-22 05:07:15 +00:00
|
|
|
void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles) {
|
2013-10-02 06:56:10 +00:00
|
|
|
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;
|
2014-01-22 05:07:15 +00:00
|
|
|
dma->nextEvent = 0;
|
2014-04-20 01:14:17 +00:00
|
|
|
GBAMemoryUpdateDMAs(audio->p, -cycles);
|
2013-10-02 06:56:10 +00:00
|
|
|
}
|
2013-10-02 09:40:16 +00:00
|
|
|
CircleBufferRead8(&channel->fifo, &channel->sample);
|
2013-10-02 06:56:10 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 11:43:56 +00:00
|
|
|
unsigned GBAAudioCopy(struct GBAAudio* audio, void* left, void* right, unsigned nSamples) {
|
|
|
|
GBASyncLockAudio(audio->p->sync);
|
|
|
|
unsigned read = 0;
|
|
|
|
if (left) {
|
|
|
|
unsigned readL = CircleBufferRead(&audio->left, left, nSamples * sizeof(int32_t)) >> 2;
|
|
|
|
if (readL < nSamples) {
|
|
|
|
memset((int32_t*) left + readL, 0, nSamples - readL);
|
|
|
|
}
|
|
|
|
read = readL;
|
|
|
|
}
|
|
|
|
if (right) {
|
|
|
|
unsigned readR = CircleBufferRead(&audio->right, right, nSamples * sizeof(int32_t)) >> 2;
|
|
|
|
if (readR < nSamples) {
|
|
|
|
memset((int32_t*) right + readR, 0, nSamples - readR);
|
|
|
|
}
|
|
|
|
read = read >= readR ? read : readR;
|
|
|
|
}
|
|
|
|
GBASyncConsumeAudio(audio->p->sync);
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
2014-01-29 08:43:57 +00:00
|
|
|
unsigned GBAAudioResampleNN(struct GBAAudio* audio, float ratio, float* drift, struct GBAStereoSample* output, unsigned nSamples) {
|
2014-01-29 07:52:28 +00:00
|
|
|
int32_t left[GBA_AUDIO_SAMPLES];
|
|
|
|
int32_t right[GBA_AUDIO_SAMPLES];
|
|
|
|
|
|
|
|
// toRead is in GBA samples
|
|
|
|
// TODO: Do this with fixed-point math
|
|
|
|
unsigned toRead = ceilf(nSamples / ratio);
|
2014-01-29 08:43:57 +00:00
|
|
|
unsigned totalRead = 0;
|
2014-01-29 07:52:28 +00:00
|
|
|
while (nSamples) {
|
|
|
|
unsigned currentRead = GBA_AUDIO_SAMPLES;
|
|
|
|
if (currentRead > toRead) {
|
|
|
|
currentRead = toRead;
|
|
|
|
}
|
|
|
|
unsigned read = GBAAudioCopy(audio, left, right, currentRead);
|
|
|
|
toRead -= read;
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < read; ++i) {
|
|
|
|
*drift += ratio;
|
|
|
|
while (*drift >= 1.f) {
|
|
|
|
output->left = left[i];
|
|
|
|
output->right = right[i];
|
|
|
|
++output;
|
2014-01-29 08:43:57 +00:00
|
|
|
++totalRead;
|
2014-01-29 07:52:28 +00:00
|
|
|
--nSamples;
|
|
|
|
*drift -= 1.f;
|
|
|
|
if (!nSamples) {
|
2014-01-29 08:43:57 +00:00
|
|
|
return totalRead;
|
2014-01-29 07:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (read < currentRead) {
|
|
|
|
memset(output, 0, nSamples * sizeof(struct GBAStereoSample));
|
2014-01-29 08:43:57 +00:00
|
|
|
break;
|
2014-01-29 07:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-29 08:43:57 +00:00
|
|
|
return totalRead;
|
2014-01-29 07:52:28 +00:00
|
|
|
}
|
|
|
|
|
2013-10-19 06:12:14 +00:00
|
|
|
static int32_t _updateSquareChannel(struct GBAAudioSquareControl* control, int duty) {
|
|
|
|
control->hi = !control->hi;
|
2013-10-19 07:48:57 +00:00
|
|
|
int period = 16 * (2048 - control->frequency);
|
2013-10-19 06:12:14 +00:00
|
|
|
switch (duty) {
|
|
|
|
case 0:
|
|
|
|
return control->hi ? period : period * 7;
|
|
|
|
case 1:
|
|
|
|
return control->hi ? period * 2 : period * 6;
|
|
|
|
case 2:
|
|
|
|
return period * 4;
|
|
|
|
case 3:
|
|
|
|
return control->hi ? period * 6 : period * 2;
|
|
|
|
default:
|
|
|
|
// This should never be hit
|
|
|
|
return period * 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2013-10-26 11:08:18 +00:00
|
|
|
envelope->dead = 1;
|
2013-10-18 16:33:07 +00:00
|
|
|
envelope->nextStep = INT_MAX;
|
|
|
|
} else {
|
|
|
|
envelope->nextStep += envelope->stepTime * (GBA_ARM7TDMI_FREQUENCY >> 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 07:46:02 +00:00
|
|
|
static bool _updateSweep(struct GBAAudioChannel1* ch) {
|
2013-10-19 06:12:14 +00:00
|
|
|
if (ch->sweep.direction) {
|
2013-10-19 07:48:57 +00:00
|
|
|
int frequency = ch->control.frequency;
|
|
|
|
frequency -= frequency >> ch->sweep.shift;
|
|
|
|
if (frequency >= 0) {
|
|
|
|
ch->control.frequency = frequency;
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-10-19 07:48:57 +00:00
|
|
|
int frequency = ch->control.frequency;
|
|
|
|
frequency += frequency >> ch->sweep.shift;
|
|
|
|
if (frequency < 2048) {
|
|
|
|
ch->control.frequency = frequency;
|
2013-10-19 06:12:14 +00:00
|
|
|
} else {
|
2014-07-16 07:46:02 +00:00
|
|
|
return false;
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ch->nextSweep += ch->sweep.time * SWEEP_CYCLES;
|
2014-07-16 07:46:02 +00:00
|
|
|
return true;
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 06:41:11 +00:00
|
|
|
static int32_t _updateChannel1(struct GBAAudioChannel1* ch) {
|
2013-10-19 06:12:14 +00:00
|
|
|
int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
|
|
|
|
ch->sample = ch->control.hi * 0x10 - 0x8;
|
|
|
|
ch->sample *= ch->envelope.currentVolume;
|
|
|
|
return timing;
|
2013-10-18 06:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t _updateChannel2(struct GBAAudioChannel2* ch) {
|
2013-10-19 06:12:14 +00:00
|
|
|
int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
|
|
|
|
ch->sample = ch->control.hi * 0x10 - 0x8;
|
|
|
|
ch->sample *= ch->envelope.currentVolume;
|
|
|
|
return timing;
|
2013-10-18 06:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t _updateChannel3(struct GBAAudioChannel3* ch) {
|
2013-10-19 10:04:45 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-07-09 10:52:58 +00:00
|
|
|
uint32_t bitsCarry = ch->wavedata[end] & 0x0F000000;
|
2013-10-19 10:04:45 +00:00
|
|
|
uint32_t bits;
|
|
|
|
for (i = start; i >= end; --i) {
|
2014-07-09 10:52:58 +00:00
|
|
|
bits = ch->wavedata[i] & 0x0F000000;
|
|
|
|
ch->wavedata[i] = ((ch->wavedata[i] & 0xF0F0F0F0) >> 4) | ((ch->wavedata[i] & 0x000F0F0F) << 12);
|
|
|
|
ch->wavedata[i] |= bitsCarry >> 20;
|
2013-10-19 10:04:45 +00:00
|
|
|
bitsCarry = bits;
|
|
|
|
}
|
2014-07-09 10:52:58 +00:00
|
|
|
ch->sample = (bitsCarry >> 20);
|
|
|
|
ch->sample >>= 2;
|
|
|
|
ch->sample *= volume;
|
2013-10-20 23:04:22 +00:00
|
|
|
return 8 * (2048 - ch->control.rate);
|
2013-10-18 06:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-03 13:22:29 +00:00
|
|
|
static int _applyBias(struct GBAAudio* audio, int sample) {
|
|
|
|
sample += audio->bias;
|
|
|
|
if (sample >= 0x400) {
|
|
|
|
sample = 0x3FF;
|
|
|
|
} else if (sample < 0) {
|
|
|
|
sample = 0;
|
|
|
|
}
|
|
|
|
return (sample - audio->bias) << 6;
|
|
|
|
}
|
|
|
|
|
2013-10-02 06:56:10 +00:00
|
|
|
static void _sample(struct GBAAudio* audio) {
|
|
|
|
int32_t sampleLeft = 0;
|
|
|
|
int32_t sampleRight = 0;
|
2013-10-30 04:12:26 +00:00
|
|
|
int psgShift = 6 - audio->volume;
|
2013-10-18 08:21:44 +00:00
|
|
|
|
2013-10-19 06:12:14 +00:00
|
|
|
if (audio->ch1Left) {
|
2013-10-20 08:58:52 +00:00
|
|
|
sampleLeft += audio->ch1.sample;
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->ch1Right) {
|
2013-10-20 08:58:52 +00:00
|
|
|
sampleRight += audio->ch1.sample;
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->ch2Left) {
|
2013-10-20 08:58:52 +00:00
|
|
|
sampleLeft += audio->ch2.sample;
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->ch2Right) {
|
2013-10-20 08:58:52 +00:00
|
|
|
sampleRight += audio->ch2.sample;
|
2013-10-19 06:12:14 +00:00
|
|
|
}
|
|
|
|
|
2013-10-19 10:04:45 +00:00
|
|
|
if (audio->ch3Left) {
|
2013-10-20 08:58:52 +00:00
|
|
|
sampleLeft += audio->ch3.sample;
|
2013-10-19 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->ch3Right) {
|
2013-10-20 08:58:52 +00:00
|
|
|
sampleRight += audio->ch3.sample;
|
2013-10-19 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 08:21:44 +00:00
|
|
|
if (audio->ch4Left) {
|
2013-10-20 08:58:52 +00:00
|
|
|
sampleLeft += audio->ch4.sample;
|
2013-10-18 08:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (audio->ch4Right) {
|
2013-10-20 08:58:52 +00:00
|
|
|
sampleRight += audio->ch4.sample;
|
2013-10-18 08:21:44 +00:00
|
|
|
}
|
2013-10-02 06:56:10 +00:00
|
|
|
|
2013-10-20 08:58:52 +00:00
|
|
|
sampleLeft = (sampleLeft * (1 + audio->volumeLeft)) >> psgShift;
|
|
|
|
sampleRight = (sampleRight * (1 + audio->volumeRight)) >> psgShift;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-03 13:22:29 +00:00
|
|
|
sampleLeft = _applyBias(audio, sampleLeft);
|
|
|
|
sampleRight = _applyBias(audio, sampleRight);
|
|
|
|
|
2014-01-15 11:43:56 +00:00
|
|
|
GBASyncLockAudio(audio->p->sync);
|
2014-02-03 13:22:29 +00:00
|
|
|
CircleBufferWrite32(&audio->left, sampleLeft);
|
|
|
|
CircleBufferWrite32(&audio->right, sampleRight);
|
2014-01-15 11:43:56 +00:00
|
|
|
unsigned produced = CircleBufferSize(&audio->left);
|
2014-08-09 10:59:31 +00:00
|
|
|
struct GBAThread* thread = GBAThreadGetContext();
|
|
|
|
if (thread && thread->stream) {
|
|
|
|
thread->stream->postAudioFrame(thread->stream, sampleLeft, sampleRight);
|
|
|
|
}
|
2014-07-21 07:14:48 +00:00
|
|
|
GBASyncProduceAudio(audio->p->sync, produced >= CircleBufferCapacity(&audio->left) / sizeof(int32_t) * 3);
|
2013-09-30 08:23:58 +00:00
|
|
|
}
|
2014-01-26 02:01:31 +00:00
|
|
|
|
|
|
|
void GBAAudioSerialize(const struct GBAAudio* audio, struct GBASerializedState* state) {
|
|
|
|
state->audio.ch1Volume = audio->ch1.envelope.currentVolume;
|
|
|
|
state->audio.ch1Dead = audio->ch1.envelope.dead;
|
|
|
|
state->audio.ch1Hi = audio->ch1.control.hi;
|
|
|
|
state->audio.ch1.envelopeNextStep = audio->ch1.envelope.nextStep;
|
|
|
|
state->audio.ch1.waveNextStep = audio->ch1.control.nextStep;
|
|
|
|
state->audio.ch1.sweepNextStep = audio->ch1.nextSweep;
|
|
|
|
state->audio.ch1.endTime = audio->ch1.control.endTime;
|
|
|
|
state->audio.ch1.nextEvent = audio->nextCh1;
|
|
|
|
|
|
|
|
state->audio.ch2Volume = audio->ch2.envelope.currentVolume;
|
|
|
|
state->audio.ch2Dead = audio->ch2.envelope.dead;
|
|
|
|
state->audio.ch2Hi = audio->ch2.control.hi;
|
|
|
|
state->audio.ch2.envelopeNextStep = audio->ch2.envelope.nextStep;
|
|
|
|
state->audio.ch2.waveNextStep = audio->ch2.control.nextStep;
|
|
|
|
state->audio.ch2.endTime = audio->ch2.control.endTime;
|
|
|
|
state->audio.ch2.nextEvent = audio->nextCh2;
|
|
|
|
|
|
|
|
memcpy(state->audio.ch3.wavebanks, audio->ch3.wavedata, sizeof(state->audio.ch3.wavebanks));
|
|
|
|
state->audio.ch3.endTime = audio->ch3.control.endTime;
|
|
|
|
state->audio.ch3.nextEvent = audio->nextCh3;
|
|
|
|
|
|
|
|
state->audio.ch4Volume = audio->ch4.envelope.currentVolume;
|
|
|
|
state->audio.ch4Dead = audio->ch4.envelope.dead;
|
|
|
|
state->audio.ch4.envelopeNextStep = audio->ch4.envelope.nextStep;
|
|
|
|
state->audio.ch4.lfsr = audio->ch4.lfsr;
|
|
|
|
state->audio.ch4.endTime = audio->ch4.control.endTime;
|
|
|
|
state->audio.ch4.nextEvent = audio->nextCh4;
|
|
|
|
|
2014-07-08 07:02:39 +00:00
|
|
|
CircleBufferDump(&audio->chA.fifo, state->audio.fifoA, sizeof(state->audio.fifoA));
|
|
|
|
CircleBufferDump(&audio->chB.fifo, state->audio.fifoB, sizeof(state->audio.fifoB));
|
|
|
|
state->audio.fifoSize = CircleBufferSize(&audio->chA.fifo);
|
|
|
|
|
2014-01-26 02:01:31 +00:00
|
|
|
state->audio.nextEvent = audio->nextEvent;
|
|
|
|
state->audio.eventDiff = audio->eventDiff;
|
|
|
|
state->audio.nextSample = audio->nextSample;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAAudioDeserialize(struct GBAAudio* audio, const struct GBASerializedState* state) {
|
|
|
|
audio->ch1.envelope.currentVolume = state->audio.ch1Volume;
|
|
|
|
audio->ch1.envelope.dead = state->audio.ch1Dead;
|
|
|
|
audio->ch1.control.hi = state->audio.ch1Hi;
|
|
|
|
audio->ch1.envelope.nextStep = state->audio.ch1.envelopeNextStep;
|
|
|
|
audio->ch1.control.nextStep = state->audio.ch1.waveNextStep;
|
|
|
|
audio->ch1.nextSweep = state->audio.ch1.sweepNextStep;
|
|
|
|
audio->ch1.control.endTime = state->audio.ch1.endTime;
|
|
|
|
audio->nextCh1 = state->audio.ch1.nextEvent;
|
|
|
|
|
|
|
|
audio->ch2.envelope.currentVolume = state->audio.ch2Volume;
|
|
|
|
audio->ch2.envelope.dead = state->audio.ch2Dead;
|
|
|
|
audio->ch2.control.hi = state->audio.ch2Hi;
|
|
|
|
audio->ch2.envelope.nextStep = state->audio.ch2.envelopeNextStep;
|
|
|
|
audio->ch2.control.nextStep = state->audio.ch2.waveNextStep;
|
|
|
|
audio->ch2.control.endTime = state->audio.ch2.endTime;
|
|
|
|
audio->nextCh2 = state->audio.ch2.nextEvent;
|
|
|
|
|
|
|
|
memcpy(audio->ch3.wavedata, state->audio.ch3.wavebanks, sizeof(audio->ch3.wavedata));
|
|
|
|
audio->ch3.control.endTime = state->audio.ch3.endTime;
|
|
|
|
audio->nextCh3 = state->audio.ch3.nextEvent;
|
|
|
|
|
|
|
|
audio->ch4.envelope.currentVolume = state->audio.ch4Volume;
|
|
|
|
audio->ch4.envelope.dead = state->audio.ch4Dead;
|
|
|
|
audio->ch4.envelope.nextStep = state->audio.ch4.envelopeNextStep;
|
|
|
|
audio->ch4.lfsr = state->audio.ch4.lfsr;
|
|
|
|
audio->ch4.control.endTime = state->audio.ch4.endTime;
|
|
|
|
audio->nextCh4 = state->audio.ch4.nextEvent;
|
|
|
|
|
2014-07-08 07:02:39 +00:00
|
|
|
CircleBufferClear(&audio->chA.fifo);
|
|
|
|
CircleBufferClear(&audio->chB.fifo);
|
2014-07-09 04:56:03 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < state->audio.fifoSize; ++i) {
|
|
|
|
CircleBufferWrite8(&audio->chA.fifo, state->audio.fifoA[i]);
|
|
|
|
CircleBufferWrite8(&audio->chB.fifo, state->audio.fifoB[i]);
|
2014-07-08 07:02:39 +00:00
|
|
|
}
|
|
|
|
|
2014-01-26 02:01:31 +00:00
|
|
|
audio->nextEvent = state->audio.nextEvent;
|
|
|
|
audio->eventDiff = state->audio.eventDiff;
|
|
|
|
audio->nextSample = state->audio.nextSample;
|
|
|
|
}
|
2014-07-21 06:24:02 +00:00
|
|
|
|
|
|
|
float GBAAudioCalculateRatio(struct GBAAudio* audio, float desiredFPS, float desiredSampleRate) {
|
|
|
|
return desiredSampleRate * GBA_ARM7TDMI_FREQUENCY / (VIDEO_TOTAL_LENGTH * desiredFPS * audio->sampleRate);
|
|
|
|
}
|