From 0fb5be2420dc661d12a229b13d3055a0537b72d8 Mon Sep 17 00:00:00 2001 From: goyuken Date: Sat, 15 Dec 2012 16:51:04 +0000 Subject: [PATCH] NES: FDS: audit and clean up audio module. in the process, fix a few bugs; opening to Metroid sounds correct now --- .../Consoles/Nintendo/NES/FDS/FDS.cs | 8 +- .../Consoles/Nintendo/NES/FDS/FDSAudio.cs | 73 +++++++++++++++++-- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/BizHawk.Emulation/Consoles/Nintendo/NES/FDS/FDS.cs b/BizHawk.Emulation/Consoles/Nintendo/NES/FDS/FDS.cs index 7cca1eaec5..1a6c0f7d50 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/NES/FDS/FDS.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/NES/FDS/FDS.cs @@ -116,7 +116,11 @@ namespace BizHawk.Emulation.Consoles.Nintendo Cart.board_type = "FAMICOM_DISK_SYSTEM"; diskdrive = new RamAdapter(); - audio = new FDSAudio(NES.cpuclockrate); + if (NES.apu != null) + { + //audio = new FDSAudio(NES.cpuclockrate); + audio = new FDSAudio(NES.apu.ExternalQueue); + } InsertSide(0); // set mirroring?? @@ -382,6 +386,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo WRAM[addr + 0x2000] = value; } + /* public override void ApplyCustomAudio(short[] samples) { audio.ApplyCustomAudio(samples); @@ -396,5 +401,6 @@ namespace BizHawk.Emulation.Consoles.Nintendo audio = null; } } + */ } } diff --git a/BizHawk.Emulation/Consoles/Nintendo/NES/FDS/FDSAudio.cs b/BizHawk.Emulation/Consoles/Nintendo/NES/FDS/FDSAudio.cs index 65cab075f2..ed9c611030 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/NES/FDS/FDSAudio.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/NES/FDS/FDSAudio.cs @@ -6,7 +6,7 @@ using System.Text; namespace BizHawk.Emulation.Consoles.Nintendo { // http://wiki.nesdev.com/w/index.php/FDS_audio - public class FDSAudio : IDisposable + public class FDSAudio //: IDisposable { public void SyncState(Serializer ser) { @@ -57,30 +57,78 @@ namespace BizHawk.Emulation.Consoles.Nintendo //4040:407f byte[] waveram = new byte[64]; + /// + /// playback position, clocked by main unit + /// int waverampos; //4080 + /// + /// volume level or envelope speed, depending on r4080_7 + /// int volumespd; + /// + /// increase volume with envelope + /// bool r4080_6; + /// + /// disable volume envelope + /// bool r4080_7; //4082:4083 + /// + /// speed to clock main unit + /// int frequency; + /// + /// disable volume and sweep + /// bool r4083_6; + /// + /// silence channel + /// bool r4083_7; //4084 + /// + /// sweep gain or sweep speed, depending on r4084_7 + /// int sweepspd; + /// + /// increase sweep with envelope + /// bool r4084_6; + /// + /// disable sweep unit + /// bool r4084_7; //4085 + /// + /// 7 bit signed + /// int sweepbias; //4086:4087 + /// + /// speed to clock modulation unit + /// int modfreq; + /// + /// disable modulation unit + /// bool r4087_7; //4088 + /// + /// ring buffer, only 32 entries on hardware + /// byte[] modtable = new byte[64]; + /// + /// playback position + /// int modtablepos; //4089 int mastervol_num; int mastervol_den; + /// + /// channel silenced and waveram writable + /// bool waveram_writeenable; //408a int envspeed; @@ -92,15 +140,21 @@ namespace BizHawk.Emulation.Consoles.Nintendo int modoutput; + // read at 4090 int volumegain; + // read at 4092 int sweepgain; int waveramoutput; int latchedoutput; - public FDSAudio(int m2rate) + Action SendDiff; + + public FDSAudio(Action SendDiff) { + this.SendDiff = SendDiff; + /* // minor hack: due to the way the initialization sequence goes, this might get called // with m2rate = 0. such an instance will never be asked for samples, though if (m2rate > 0) @@ -108,8 +162,10 @@ namespace BizHawk.Emulation.Consoles.Nintendo blip = new Sound.Utilities.BlipBuffer(blipsize); blip.SetRates(m2rate, 44100); } + */ } + /* public void Dispose() { if (blip != null) @@ -118,6 +174,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo blip = null; } } + */ void CalcMod() { @@ -151,7 +208,8 @@ namespace BizHawk.Emulation.Consoles.Nintendo if (latchedoutput != tmp) { - dlist.Add(new Delta(sampleclock, tmp - latchedoutput)); + //dlist.Add(new Delta(sampleclock, tmp - latchedoutput)); + SendDiff((tmp - latchedoutput) * 6); latchedoutput = tmp; } } @@ -162,7 +220,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo public void Clock() { // volume envelope unit - if (!r4080_7 && envspeed > 0 && !r4080_6) + if (!r4080_7 && envspeed > 0 && !r4083_6) { volumeclock++; if (volumeclock >= 8 * envspeed * (volumespd + 1)) @@ -209,6 +267,9 @@ namespace BizHawk.Emulation.Consoles.Nintendo case 7: sweepbias -= 1; break; } sweepbias &= 0x7f; + // sign extend + sweepbias <<= 25; + sweepbias >>= 25; modtablepos &= 63; CalcMod(); } @@ -225,7 +286,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo CalcOut(); } } - sampleclock++; + //sampleclock++; } public void WriteReg(int addr, byte value) @@ -326,6 +387,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo return ret; } + /* Sound.Utilities.BlipBuffer blip; struct Delta @@ -375,5 +437,6 @@ namespace BizHawk.Emulation.Consoles.Nintendo samples[j + 1] = samples[j]; } } + */ } }