NES: FDS: audit and clean up audio module. in the process, fix a few bugs; opening to Metroid sounds correct now

This commit is contained in:
goyuken 2012-12-15 16:51:04 +00:00
parent 3cfc57f8a1
commit 0fb5be2420
2 changed files with 75 additions and 6 deletions

View File

@ -116,7 +116,11 @@ namespace BizHawk.Emulation.Consoles.Nintendo
Cart.board_type = "FAMICOM_DISK_SYSTEM"; Cart.board_type = "FAMICOM_DISK_SYSTEM";
diskdrive = new RamAdapter(); 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); InsertSide(0);
// set mirroring?? // set mirroring??
@ -382,6 +386,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
WRAM[addr + 0x2000] = value; WRAM[addr + 0x2000] = value;
} }
/*
public override void ApplyCustomAudio(short[] samples) public override void ApplyCustomAudio(short[] samples)
{ {
audio.ApplyCustomAudio(samples); audio.ApplyCustomAudio(samples);
@ -396,5 +401,6 @@ namespace BizHawk.Emulation.Consoles.Nintendo
audio = null; audio = null;
} }
} }
*/
} }
} }

View File

@ -6,7 +6,7 @@ using System.Text;
namespace BizHawk.Emulation.Consoles.Nintendo namespace BizHawk.Emulation.Consoles.Nintendo
{ {
// http://wiki.nesdev.com/w/index.php/FDS_audio // http://wiki.nesdev.com/w/index.php/FDS_audio
public class FDSAudio : IDisposable public class FDSAudio //: IDisposable
{ {
public void SyncState(Serializer ser) public void SyncState(Serializer ser)
{ {
@ -57,30 +57,78 @@ namespace BizHawk.Emulation.Consoles.Nintendo
//4040:407f //4040:407f
byte[] waveram = new byte[64]; byte[] waveram = new byte[64];
/// <summary>
/// playback position, clocked by main unit
/// </summary>
int waverampos; int waverampos;
//4080 //4080
/// <summary>
/// volume level or envelope speed, depending on r4080_7
/// </summary>
int volumespd; int volumespd;
/// <summary>
/// increase volume with envelope
/// </summary>
bool r4080_6; bool r4080_6;
/// <summary>
/// disable volume envelope
/// </summary>
bool r4080_7; bool r4080_7;
//4082:4083 //4082:4083
/// <summary>
/// speed to clock main unit
/// </summary>
int frequency; int frequency;
/// <summary>
/// disable volume and sweep
/// </summary>
bool r4083_6; bool r4083_6;
/// <summary>
/// silence channel
/// </summary>
bool r4083_7; bool r4083_7;
//4084 //4084
/// <summary>
/// sweep gain or sweep speed, depending on r4084_7
/// </summary>
int sweepspd; int sweepspd;
/// <summary>
/// increase sweep with envelope
/// </summary>
bool r4084_6; bool r4084_6;
/// <summary>
/// disable sweep unit
/// </summary>
bool r4084_7; bool r4084_7;
//4085 //4085
/// <summary>
/// 7 bit signed
/// </summary>
int sweepbias; int sweepbias;
//4086:4087 //4086:4087
/// <summary>
/// speed to clock modulation unit
/// </summary>
int modfreq; int modfreq;
/// <summary>
/// disable modulation unit
/// </summary>
bool r4087_7; bool r4087_7;
//4088 //4088
/// <summary>
/// ring buffer, only 32 entries on hardware
/// </summary>
byte[] modtable = new byte[64]; byte[] modtable = new byte[64];
/// <summary>
/// playback position
/// </summary>
int modtablepos; int modtablepos;
//4089 //4089
int mastervol_num; int mastervol_num;
int mastervol_den; int mastervol_den;
/// <summary>
/// channel silenced and waveram writable
/// </summary>
bool waveram_writeenable; bool waveram_writeenable;
//408a //408a
int envspeed; int envspeed;
@ -92,15 +140,21 @@ namespace BizHawk.Emulation.Consoles.Nintendo
int modoutput; int modoutput;
// read at 4090
int volumegain; int volumegain;
// read at 4092
int sweepgain; int sweepgain;
int waveramoutput; int waveramoutput;
int latchedoutput; int latchedoutput;
public FDSAudio(int m2rate) Action<int> SendDiff;
public FDSAudio(Action<int> SendDiff)
{ {
this.SendDiff = SendDiff;
/*
// minor hack: due to the way the initialization sequence goes, this might get called // 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 // with m2rate = 0. such an instance will never be asked for samples, though
if (m2rate > 0) if (m2rate > 0)
@ -108,8 +162,10 @@ namespace BizHawk.Emulation.Consoles.Nintendo
blip = new Sound.Utilities.BlipBuffer(blipsize); blip = new Sound.Utilities.BlipBuffer(blipsize);
blip.SetRates(m2rate, 44100); blip.SetRates(m2rate, 44100);
} }
*/
} }
/*
public void Dispose() public void Dispose()
{ {
if (blip != null) if (blip != null)
@ -118,6 +174,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
blip = null; blip = null;
} }
} }
*/
void CalcMod() void CalcMod()
{ {
@ -151,7 +208,8 @@ namespace BizHawk.Emulation.Consoles.Nintendo
if (latchedoutput != tmp) if (latchedoutput != tmp)
{ {
dlist.Add(new Delta(sampleclock, tmp - latchedoutput)); //dlist.Add(new Delta(sampleclock, tmp - latchedoutput));
SendDiff((tmp - latchedoutput) * 6);
latchedoutput = tmp; latchedoutput = tmp;
} }
} }
@ -162,7 +220,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
public void Clock() public void Clock()
{ {
// volume envelope unit // volume envelope unit
if (!r4080_7 && envspeed > 0 && !r4080_6) if (!r4080_7 && envspeed > 0 && !r4083_6)
{ {
volumeclock++; volumeclock++;
if (volumeclock >= 8 * envspeed * (volumespd + 1)) if (volumeclock >= 8 * envspeed * (volumespd + 1))
@ -209,6 +267,9 @@ namespace BizHawk.Emulation.Consoles.Nintendo
case 7: sweepbias -= 1; break; case 7: sweepbias -= 1; break;
} }
sweepbias &= 0x7f; sweepbias &= 0x7f;
// sign extend
sweepbias <<= 25;
sweepbias >>= 25;
modtablepos &= 63; modtablepos &= 63;
CalcMod(); CalcMod();
} }
@ -225,7 +286,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
CalcOut(); CalcOut();
} }
} }
sampleclock++; //sampleclock++;
} }
public void WriteReg(int addr, byte value) public void WriteReg(int addr, byte value)
@ -326,6 +387,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
return ret; return ret;
} }
/*
Sound.Utilities.BlipBuffer blip; Sound.Utilities.BlipBuffer blip;
struct Delta struct Delta
@ -375,5 +437,6 @@ namespace BizHawk.Emulation.Consoles.Nintendo
samples[j + 1] = samples[j]; samples[j + 1] = samples[j];
} }
} }
*/
} }
} }