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,12 +68,9 @@ 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) {
if (audio->playingCh1 && !audio->ch1.envelope.dead) {
audio->nextCh1 -= audio->eventDiff; audio->nextCh1 -= audio->eventDiff;
audio->nextCh2 -= audio->eventDiff; if (audio->ch1.envelope.nextStep != INT_MAX) {
audio->nextCh3 -= audio->eventDiff;
audio->nextCh4 -= audio->eventDiff;
if (audio->playingCh1) {
audio->ch1.envelope.nextStep -= audio->eventDiff; audio->ch1.envelope.nextStep -= audio->eventDiff;
if (audio->ch1.envelope.nextStep <= 0) { if (audio->ch1.envelope.nextStep <= 0) {
_updateEnvelope(&audio->ch1.envelope); _updateEnvelope(&audio->ch1.envelope);
@ -81,6 +78,7 @@ int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
audio->nextEvent = audio->ch1.envelope.nextStep; audio->nextEvent = audio->ch1.envelope.nextStep;
} }
} }
}
if (audio->ch1.nextSweep != INT_MAX) { if (audio->ch1.nextSweep != INT_MAX) {
audio->ch1.nextSweep -= audio->eventDiff; audio->ch1.nextSweep -= audio->eventDiff;
@ -107,7 +105,9 @@ int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
} }
} }
if (audio->playingCh2) { if (audio->playingCh2 && !audio->ch2.envelope.dead) {
audio->nextCh2 -= audio->eventDiff;
if (audio->ch2.envelope.nextStep != INT_MAX) {
audio->ch2.envelope.nextStep -= audio->eventDiff; audio->ch2.envelope.nextStep -= audio->eventDiff;
if (audio->ch2.envelope.nextStep <= 0) { if (audio->ch2.envelope.nextStep <= 0) {
_updateEnvelope(&audio->ch2.envelope); _updateEnvelope(&audio->ch2.envelope);
@ -115,6 +115,7 @@ int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
audio->nextEvent = audio->ch2.envelope.nextStep; audio->nextEvent = audio->ch2.envelope.nextStep;
} }
} }
}
if (audio->nextCh2 <= 0) { if (audio->nextCh2 <= 0) {
audio->nextCh2 += _updateChannel2(&audio->ch2); audio->nextCh2 += _updateChannel2(&audio->ch2);
@ -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,7 +149,9 @@ int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
} }
} }
if (audio->playingCh4) { if (audio->playingCh4 && !audio->ch4.envelope.dead) {
audio->nextCh4 -= audio->eventDiff;
if (audio->ch4.envelope.nextStep != INT_MAX) {
audio->ch4.envelope.nextStep -= audio->eventDiff; audio->ch4.envelope.nextStep -= audio->eventDiff;
if (audio->ch4.envelope.nextStep <= 0) { if (audio->ch4.envelope.nextStep <= 0) {
_updateEnvelope(&audio->ch4.envelope); _updateEnvelope(&audio->ch4.envelope);
@ -155,6 +159,7 @@ int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
audio->nextEvent = audio->ch4.envelope.nextStep; audio->nextEvent = audio->ch4.envelope.nextStep;
} }
} }
}
if (audio->nextCh4 <= 0) { if (audio->nextCh4 <= 0) {
audio->nextCh4 += _updateChannel4(&audio->ch4); audio->nextCh4 += _updateChannel4(&audio->ch4);
@ -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;
}; };