GB Audio: More sampling cleanup (fixes #1142)

This commit is contained in:
Vicki Pfau 2018-07-28 00:02:33 -07:00
parent d25d4d30ff
commit 162fd0b353
1 changed files with 28 additions and 25 deletions

View File

@ -562,7 +562,7 @@ void GBAudioUpdateFrame(struct GBAudio* audio, struct mTiming* timing) {
if (audio->playingCh4 && !audio->ch4.envelope.dead) { if (audio->playingCh4 && !audio->ch4.envelope.dead) {
--audio->ch4.envelope.nextStep; --audio->ch4.envelope.nextStep;
if (audio->ch4.envelope.nextStep == 0) { if (audio->ch4.envelope.nextStep == 0) {
int8_t sample = (audio->ch4.sample > 0) * 0x8; int8_t sample = audio->ch4.sample > 0;
_updateEnvelope(&audio->ch4.envelope); _updateEnvelope(&audio->ch4.envelope);
if (audio->ch4.envelope.dead == 2) { if (audio->ch4.envelope.dead == 2) {
mTimingDeschedule(timing, &audio->ch4Event); mTimingDeschedule(timing, &audio->ch4Event);
@ -575,52 +575,55 @@ void GBAudioUpdateFrame(struct GBAudio* audio, struct mTiming* timing) {
} }
void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) { void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) {
int sampleLeft = 0; int dcOffset = audio->style == GB_AUDIO_GBA ? 0 : 0x8;
int sampleRight = 0; int sampleLeft = dcOffset;
int sampleRight = dcOffset;
if (audio->playingCh1 && !audio->forceDisableCh[0]) { if (audio->playingCh1 && !audio->forceDisableCh[0]) {
if (audio->ch1Left) { if (audio->ch1Left) {
sampleLeft += audio->ch1.sample; sampleLeft -= audio->ch1.sample;
} }
if (audio->ch1Right) { if (audio->ch1Right) {
sampleRight += audio->ch1.sample; sampleRight -= audio->ch1.sample;
} }
} }
if (audio->playingCh2 && !audio->forceDisableCh[1]) { if (audio->playingCh2 && !audio->forceDisableCh[1]) {
if (audio->ch2Left) { if (audio->ch2Left) {
sampleLeft += audio->ch2.sample; sampleLeft -= audio->ch2.sample;
} }
if (audio->ch2Right) { if (audio->ch2Right) {
sampleRight += audio->ch2.sample; sampleRight -= audio->ch2.sample;
} }
} }
if (audio->playingCh3 && !audio->forceDisableCh[2]) { if (audio->playingCh3 && !audio->forceDisableCh[2]) {
if (audio->ch3Left) { if (audio->ch3Left) {
sampleLeft += audio->ch3.sample; sampleLeft -= audio->ch3.sample;
} }
if (audio->ch3Right) { if (audio->ch3Right) {
sampleRight += audio->ch3.sample; sampleRight -= audio->ch3.sample;
} }
} }
if (audio->playingCh4 && !audio->forceDisableCh[3]) { if (audio->playingCh4 && !audio->forceDisableCh[3]) {
if (audio->ch4Left) { if (audio->ch4Left) {
sampleLeft += audio->ch4.sample; sampleLeft -= audio->ch4.sample;
} }
if (audio->ch4Right) { if (audio->ch4Right) {
sampleRight += audio->ch4.sample; sampleRight -= audio->ch4.sample;
} }
} }
int dcOffset = audio->style == GB_AUDIO_GBA ? 0 : 0x20A; sampleLeft <<= 3;
*left = (sampleLeft - dcOffset) * (1 + audio->volumeLeft); sampleRight <<= 3;
*right = (sampleRight - dcOffset) * (1 + audio->volumeRight);
*left = sampleLeft * (1 + audio->volumeLeft);
*right = sampleRight * (1 + audio->volumeRight);
} }
static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate) { static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate) {
@ -719,7 +722,7 @@ bool _writeEnvelope(struct GBAudioEnvelope* envelope, uint8_t value, enum GBAudi
} }
static void _updateSquareSample(struct GBAudioSquareChannel* ch) { static void _updateSquareSample(struct GBAudioSquareChannel* ch) {
ch->sample = ch->control.hi * ch->envelope.currentVolume * 0x8; ch->sample = ch->control.hi * ch->envelope.currentVolume;
} }
static int32_t _updateSquareChannel(struct GBAudioSquareChannel* ch) { static int32_t _updateSquareChannel(struct GBAudioSquareChannel* ch) {
@ -820,19 +823,17 @@ static void _updateChannel3(struct mTiming* timing, void* user, uint32_t cyclesL
int volume; int volume;
switch (ch->volume) { switch (ch->volume) {
case 0: case 0:
volume = 0;
break;
case 1:
volume = 4; volume = 4;
break; break;
case 2: case 1:
volume = 2; volume = 0;
break; break;
case 3: case 2:
volume = 1; volume = 1;
break; break;
default: default:
volume = 3; case 3:
volume = 2;
break; break;
} }
int start; int start;
@ -870,7 +871,10 @@ static void _updateChannel3(struct mTiming* timing, void* user, uint32_t cyclesL
ch->sample = bitsCarry >> 4; ch->sample = bitsCarry >> 4;
break; break;
} }
ch->sample *= volume * 2; if (ch->volume > 3) {
ch->sample += ch->sample << 1;
}
ch->sample >>= volume;
audio->ch3.readable = true; audio->ch3.readable = true;
if (audio->style == GB_AUDIO_DMG) { if (audio->style == GB_AUDIO_DMG) {
mTimingDeschedule(audio->timing, &audio->ch3Fade); mTimingDeschedule(audio->timing, &audio->ch3Fade);
@ -897,8 +901,7 @@ static void _updateChannel4(struct mTiming* timing, void* user, uint32_t cyclesL
do { do {
int lsb = ch->lfsr & 1; int lsb = ch->lfsr & 1;
ch->sample = lsb * 0x8; ch->sample = lsb * ch->envelope.currentVolume;
ch->sample *= ch->envelope.currentVolume;
ch->lfsr >>= 1; ch->lfsr >>= 1;
ch->lfsr ^= (lsb * 0x60) << (ch->power ? 0 : 8); ch->lfsr ^= (lsb * 0x60) << (ch->power ? 0 : 8);
cycles += baseCycles; cycles += baseCycles;