From c76ba893bd5bd6258545c0afda6562519baf8b2b Mon Sep 17 00:00:00 2001 From: FIX94 Date: Sun, 8 Apr 2018 02:36:53 +0200 Subject: [PATCH] improved audio, now all the random popping noises that were very audible in older versions should be gone, also improved volume scaling on multi-chip nsf files --- apu.c | 60 +++++++++++++++++++++++----------------------------- audio_mmc5.c | 17 ++++++--------- audio_vrc6.c | 21 +++++------------- main.c | 2 +- 4 files changed, 39 insertions(+), 61 deletions(-) diff --git a/apu.c b/apu.c index d1bb4cc..4e32db4 100644 --- a/apu.c +++ b/apu.c @@ -385,10 +385,12 @@ static float lastHPOut = 0, lastLPOut = 0; #else static int32_t lastHPOut = 0, lastLPOut = 0; #endif -static uint8_t lastP1Out = 0, lastP2Out = 0, lastTriOut = 0, lastNoiseOut = 0; +static uint8_t p1Out = 0, p2Out = 0, triOut = 0, noiseOut = 0; extern bool emuSkipVsync, emuSkipFrame; +static float ampVol[7] = { 3.0f, 2.0f, 1.5f, 1.2f, 1.0f, 0.85f, 0.75f }; + bool apuCycle() { if(curBufPos == apuBufSize) @@ -415,75 +417,66 @@ bool apuCycle() } curBufPos = 0; } - uint8_t p1Out = lastP1Out, p2Out = lastP2Out, - triOut = lastTriOut, noiseOut = lastNoiseOut; if(p1LengthCtr && (APU_IO_Reg[0x15] & P1_ENABLE)) { - if(p1seq[p1Cycle] && !p1Sweep.mute && freq1 >= 8 && freq1 < 0x7FF) - lastP1Out = p1Out = (p1Env.constant ? p1Env.vol : p1Env.decay); - else - p1Out = 0; + if(!p1Sweep.mute && freq1 >= 8 && freq1 < 0x7FF) + p1Out = p1seq[p1Cycle] ? (p1Env.constant ? p1Env.vol : p1Env.decay) : 0; } if(p2LengthCtr && (APU_IO_Reg[0x15] & P2_ENABLE)) { - if(p2seq[p2Cycle] && !p2Sweep.mute && freq2 >= 8 && freq2 < 0x7FF) - lastP2Out = p2Out = (p2Env.constant ? p2Env.vol : p2Env.decay); - else - p2Out = 0; + if(!p2Sweep.mute && freq2 >= 8 && freq2 < 0x7FF) + p2Out = p2seq[p2Cycle] ? (p2Env.constant ? p2Env.vol : p2Env.decay) : 0; } if(triLengthCtr && triCurLinearCtr && (APU_IO_Reg[0x15] & TRI_ENABLE)) { - if(triSeq[triCycle] && triFreq >= 2) - lastTriOut = triOut = triSeq[triCycle]; - else - triOut = 0; + if(triFreq >= 2) + triOut = triSeq[triCycle]; } if(noiseLengthCtr && (APU_IO_Reg[0x15] & NOISE_ENABLE)) { - if((noiseShiftReg&1) == 0 && noiseFreq > 0) - lastNoiseOut = noiseOut = (noiseEnv.constant ? noiseEnv.vol : noiseEnv.decay); - else - noiseOut = 0; + if(noiseFreq > 0) + noiseOut = (noiseShiftReg&1) == 0 ? (noiseEnv.constant ? noiseEnv.vol : noiseEnv.decay) : 0; } #if AUDIO_FLOAT float curIn = pulseLookupTbl[p1Out + p2Out] + tndLookupTbl[(3*triOut) + (2*noiseOut) + dmcVol]; + uint8_t ampVolPos = 0; //very rough still if(vrc6enabled) { vrc6AudioCycle(); curIn += ((float)vrc6Out)*0.008f; - curIn *= 0.6667f; + ampVolPos++; } if(fdsEnabled) { fdsAudioCycle(); curIn += ((float)fdsOut)*0.00617f; - curIn *= 0.75f; + ampVolPos++; } if(mmc5enabled) { mmc5AudioCycle(); curIn += pulseLookupTbl[mmc5Out]+(mmc5pcm*0.002f); - curIn *= 0.75f; + ampVolPos++; } if(vrc7enabled) { curIn += (((float)(vrc7Out>>7))/32768.f); - curIn *= 0.75f; + ampVolPos++; } if(n163enabled) { curIn += ((float)n163Out)*0.0008f; - curIn *= 0.6667f; + ampVolPos++; } if(s5Benabled) { s5BAudioCycle(); curIn += ((float)s5BOut)/32768.f; - curIn *= 0.6667f; + ampVolPos++; } //amplify input - curIn *= 3.0f; + curIn *= ampVol[ampVolPos]; float curLPout = lastLPOut+(lpVal*(curIn-lastLPOut)); float curHPOut = hpVal*(lastHPOut+lastLPOut-curLPout); //set output @@ -492,43 +485,44 @@ bool apuCycle() lastHPOut = curHPOut; #else int32_t curIn = pulseLookupTbl[p1Out + p2Out] + tndLookupTbl[(3*triOut) + (2*noiseOut) + dmcVol]; + uint8_t ampVolPos = 0; //very rough still if(vrc6enabled) { vrc6AudioCycle(); curIn += ((int32_t)vrc6Out)*262; - curIn <<= 1; curIn /= 3; + ampVolPos++; } if(fdsEnabled) { fdsAudioCycle(); curIn += ((int32_t)fdsOut)*202; - curIn *= 3; curIn >>= 2; + ampVolPos++; } if(mmc5enabled) { mmc5AudioCycle(); curIn += pulseLookupTbl[mmc5Out]+(mmc5pcm<<6); - curIn *= 3; curIn >>= 2; + ampVolPos++; } if(vrc7enabled) { curIn += vrc7Out>>7; - curIn *= 3; curIn >>= 2; + ampVolPos++; } if(n163enabled) { curIn += n163Out*26; - curIn <<= 1; curIn /= 3; + ampVolPos++; } if(s5Benabled) { s5BAudioCycle(); curIn += s5BOut; - curIn <<= 1; curIn /= 3; + ampVolPos++; } //amplify input - curIn *= 3; + curIn *= ampVol[ampVolPos]; int32_t curOut; //gen output curOut = lastLPOut+((lpVal*(curIn-lastLPOut))>>15); //Set Lowpass Output diff --git a/audio_mmc5.c b/audio_mmc5.c index e5a63af..d644678 100644 --- a/audio_mmc5.c +++ b/audio_mmc5.c @@ -103,26 +103,21 @@ void mmc5AudioPCMWrite(uint8_t val) mmc5pcm = val; } -static uint8_t mmc5_lastP1Out = 0, mmc5_lastP2Out = 0; +static uint8_t mmc5_p1Out = 0, mmc5_p2Out = 0; void mmc5AudioCycle() { - uint8_t p1Out = mmc5_lastP1Out, p2Out = mmc5_lastP2Out; if(mmc5_p1LengthCtr && (MMC5_IO_Reg[0x15] & P1_ENABLE)) { - if(mmc5_p1seq[mmc5_p1Cycle] && mmc5_freq1 >= 8 && mmc5_freq1 < 0x7FF) - mmc5_lastP1Out = p1Out = (mmc5_p1Env.constant ? mmc5_p1Env.vol : mmc5_p1Env.decay); - else - p1Out = 0; + if(mmc5_freq1 >= 8 && mmc5_freq1 < 0x7FF) + mmc5_p1Out = mmc5_p1seq[mmc5_p1Cycle] ? (mmc5_p1Env.constant ? mmc5_p1Env.vol : mmc5_p1Env.decay) : 0; } if(mmc5_p2LengthCtr && (MMC5_IO_Reg[0x15] & P2_ENABLE)) { - if(mmc5_p2seq[mmc5_p2Cycle] && mmc5_freq2 >= 8 && mmc5_freq2 < 0x7FF) - mmc5_lastP2Out = p2Out = (mmc5_p2Env.constant ? mmc5_p2Env.vol : mmc5_p2Env.decay); - else - p2Out = 0; + if(mmc5_freq2 >= 8 && mmc5_freq2 < 0x7FF) + mmc5_p2Out = mmc5_p2seq[mmc5_p2Cycle] ? (mmc5_p2Env.constant ? mmc5_p2Env.vol : mmc5_p2Env.decay) : 0; } - mmc5Out = p1Out + p2Out; + mmc5Out = mmc5_p1Out + mmc5_p2Out; } void mmc5AudioLenCycle() diff --git a/audio_vrc6.c b/audio_vrc6.c index fbc4394..58111ea 100644 --- a/audio_vrc6.c +++ b/audio_vrc6.c @@ -43,29 +43,18 @@ void vrc6AudioInit() //printf("VRC6 Audio Inited!\n"); } -static uint8_t vrc6_lastP1Out = 0, vrc6_lastP2Out = 0, vrc6_lastSawOut = 0; +static uint8_t vrc6_p1Out = 0, vrc6_p2Out = 0, vrc6_sawOut = 0; void vrc6AudioCycle() { - uint8_t p1Out = vrc6_lastP1Out, p2Out = vrc6_lastP2Out, sawOut = vrc6_lastSawOut; if(vrc6_p1enable) - { - if(vrc6_p1const || vrc6_p1Cycle <= vrc6_p1Duty) - vrc6_lastP1Out = p1Out = vrc6_p1Vol; - else - p1Out = 0; - } + vrc6_p1Out = (vrc6_p1const || vrc6_p1Cycle <= vrc6_p1Duty) ? vrc6_p1Vol : 0; if(vrc6_p2enable) - { - if(vrc6_p2const || vrc6_p2Cycle <= vrc6_p2Duty) - vrc6_lastP2Out = p2Out = vrc6_p2Vol; - else - p2Out = 0; - } + vrc6_p2Out = (vrc6_p2const || vrc6_p2Cycle <= vrc6_p2Duty) ? vrc6_p2Vol : 0; if(vrc6_sawenable) - vrc6_lastSawOut = sawOut = (vrc6_sawVol>>3); + vrc6_sawOut = (vrc6_sawVol>>3); - vrc6Out = p1Out+p2Out+sawOut; + vrc6Out = vrc6_p1Out+vrc6_p2Out+vrc6_sawOut; } void vrc6AudioClockTimers() diff --git a/main.c b/main.c index df67cfd..d7a11fb 100644 --- a/main.c +++ b/main.c @@ -35,7 +35,7 @@ #define DEBUG_KEY 0 #define DEBUG_LOAD_INFO 1 -static const char *VERSION_STRING = "fixNES Alpha v1.0.5"; +static const char *VERSION_STRING = "fixNES Alpha v1.0.6"; static char window_title[256]; static char window_title_pause[256];