Reorganize audio code so as to only update channels when audible

This commit is contained in:
Jeffrey Pfau 2013-10-26 04:08:18 -07:00
parent cd448324c5
commit 70365782de
2 changed files with 38 additions and 23 deletions

View File

@ -68,17 +68,15 @@ int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
while (audio->nextEvent <= 0) { while (audio->nextEvent <= 0) {
audio->nextEvent = INT_MAX; audio->nextEvent = INT_MAX;
if (audio->enable) { if (audio->enable) {
audio->nextCh1 -= audio->eventDiff; if (audio->playingCh1 && !audio->ch1.envelope.dead) {
audio->nextCh2 -= audio->eventDiff; audio->nextCh1 -= audio->eventDiff;
audio->nextCh3 -= audio->eventDiff; if (audio->ch1.envelope.nextStep != INT_MAX) {
audio->nextCh4 -= audio->eventDiff; audio->ch1.envelope.nextStep -= audio->eventDiff;
if (audio->ch1.envelope.nextStep <= 0) {
if (audio->playingCh1) { _updateEnvelope(&audio->ch1.envelope);
audio->ch1.envelope.nextStep -= audio->eventDiff; if (audio->ch1.envelope.nextStep < audio->nextEvent) {
if (audio->ch1.envelope.nextStep <= 0) { audio->nextEvent = audio->ch1.envelope.nextStep;
_updateEnvelope(&audio->ch1.envelope); }
if (audio->ch1.envelope.nextStep < audio->nextEvent) {
audio->nextEvent = audio->ch1.envelope.nextStep;
} }
} }
@ -107,12 +105,15 @@ int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
} }
} }
if (audio->playingCh2) { if (audio->playingCh2 && !audio->ch2.envelope.dead) {
audio->ch2.envelope.nextStep -= audio->eventDiff; audio->nextCh2 -= audio->eventDiff;
if (audio->ch2.envelope.nextStep <= 0) { if (audio->ch2.envelope.nextStep != INT_MAX) {
_updateEnvelope(&audio->ch2.envelope); audio->ch2.envelope.nextStep -= audio->eventDiff;
if (audio->ch2.envelope.nextStep < audio->nextEvent) { if (audio->ch2.envelope.nextStep <= 0) {
audio->nextEvent = audio->ch2.envelope.nextStep; _updateEnvelope(&audio->ch2.envelope);
if (audio->ch2.envelope.nextStep < audio->nextEvent) {
audio->nextEvent = audio->ch2.envelope.nextStep;
}
} }
} }
@ -132,6 +133,7 @@ int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
} }
if (audio->playingCh3) { if (audio->playingCh3) {
audio->nextCh3 -= audio->eventDiff;
if (audio->nextCh3 <= 0) { if (audio->nextCh3 <= 0) {
audio->nextCh3 += _updateChannel3(&audio->ch3); audio->nextCh3 += _updateChannel3(&audio->ch3);
if (audio->nextCh3 < audio->nextEvent) { if (audio->nextCh3 < audio->nextEvent) {
@ -147,12 +149,15 @@ int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
} }
} }
if (audio->playingCh4) { if (audio->playingCh4 && !audio->ch4.envelope.dead) {
audio->ch4.envelope.nextStep -= audio->eventDiff; audio->nextCh4 -= audio->eventDiff;
if (audio->ch4.envelope.nextStep <= 0) { if (audio->ch4.envelope.nextStep != INT_MAX) {
_updateEnvelope(&audio->ch4.envelope); audio->ch4.envelope.nextStep -= audio->eventDiff;
if (audio->ch4.envelope.nextStep < audio->nextEvent) { if (audio->ch4.envelope.nextStep <= 0) {
audio->nextEvent = audio->ch4.envelope.nextStep; _updateEnvelope(&audio->ch4.envelope);
if (audio->ch4.envelope.nextStep < audio->nextEvent) {
audio->nextEvent = audio->ch4.envelope.nextStep;
}
} }
} }
@ -212,6 +217,12 @@ void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value) {
void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value) { void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value) {
audio->ch1.envelope.packed = value; audio->ch1.envelope.packed = value;
if (audio->ch1.envelope.stepTime) {
audio->ch1.envelope.nextStep = 0;
} else {
audio->ch1.envelope.nextStep = INT_MAX;
}
audio->ch1.envelope.dead = 0;
} }
void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value) { void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value) {
@ -248,6 +259,7 @@ void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value) {
} else { } else {
audio->ch2.envelope.nextStep = INT_MAX; audio->ch2.envelope.nextStep = INT_MAX;
} }
audio->ch2.envelope.dead = 0;
} }
void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) { void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) {
@ -288,6 +300,7 @@ void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value) {
} else { } else {
audio->ch4.envelope.nextStep = INT_MAX; audio->ch4.envelope.nextStep = INT_MAX;
} }
audio->ch4.envelope.dead = 0;
} }
void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) { void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) {
@ -392,6 +405,7 @@ static void _updateEnvelope(struct GBAAudioEnvelope* envelope) {
envelope->nextStep = INT_MAX; envelope->nextStep = INT_MAX;
} else if (envelope->currentVolume <= 0) { } else if (envelope->currentVolume <= 0) {
envelope->currentVolume = 0; envelope->currentVolume = 0;
envelope->dead = 1;
envelope->nextStep = INT_MAX; envelope->nextStep = INT_MAX;
} else { } else {
envelope->nextStep += envelope->stepTime * (GBA_ARM7TDMI_FREQUENCY >> 6); envelope->nextStep += envelope->stepTime * (GBA_ARM7TDMI_FREQUENCY >> 6);

View File

@ -22,6 +22,7 @@ struct GBAAudioEnvelope {
uint16_t packed; uint16_t packed;
}; };
int currentVolume; int currentVolume;
int dead;
int32_t nextStep; int32_t nextStep;
}; };