cleanup NESMusicRipper.cs

This commit is contained in:
adelikat 2020-02-28 12:11:51 -06:00
parent 0d3606da14
commit 402acad0f6
2 changed files with 212 additions and 190 deletions

View File

@ -56,7 +56,7 @@
this.btnControl.TabIndex = 0;
this.btnControl.Text = "Start";
this.btnControl.UseVisualStyleBackColor = true;
this.btnControl.Click += new System.EventHandler(this.btnControl_Click);
this.btnControl.Click += new System.EventHandler(this.BtnControl_Click);
//
// txtDivider
//
@ -84,7 +84,7 @@
this.btnExport.TabIndex = 3;
this.btnExport.Text = "Export XRNS File";
this.btnExport.UseVisualStyleBackColor = true;
this.btnExport.Click += new System.EventHandler(this.btnExport_Click);
this.btnExport.Click += new System.EventHandler(this.Export_Click);
//
// lblContents
//

View File

@ -1,14 +1,17 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml.XPath;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Emulation.Cores.Nintendo.NES;
using BizHawk.Emulation.Common;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
namespace BizHawk.Client.EmuHawk
{
@ -41,100 +44,105 @@ namespace BizHawk.Client.EmuHawk
// Do nothing
}
bool IsRunning;
private bool _isRunning;
//http://www.phy.mtu.edu/~suits/notefreqs.html
//begins at C0. ends at B8
static readonly float[] freqtbl = {0,
16.35f,17.32f,18.35f,19.45f,20.6f,21.83f,23.12f,24.5f,25.96f,27.5f,29.14f,30.87f,32.7f,34.65f,36.71f,38.89f,41.2f,43.65f,46.25f,49f,51.91f,55f,58.27f,61.74f,65.41f,69.3f,73.42f,77.78f,82.41f,87.31f,92.5f,98f,103.83f,110f,116.54f,123.47f,130.81f,138.59f,146.83f,155.56f,164.81f,174.61f,185f,196f,207.65f,220f,233.08f,246.94f,261.63f,277.18f,293.66f,311.13f,329.63f,349.23f,369.99f,392f,415.3f,440f,466.16f,493.88f,523.25f,554.37f,587.33f,622.25f,659.25f,698.46f,739.99f,783.99f,830.61f,880f,932.33f,987.77f,1046.5f,1108.73f,1174.66f,1244.51f,1318.51f,1396.91f,1479.98f,1567.98f,1661.22f,1760f,1864.66f,1975.53f,2093f,2217.46f,2349.32f,2489.02f,2637.02f,2793.83f,2959.96f,3135.96f,3322.44f,3520f,3729.31f,3951.07f,4186.01f,4434.92f,4698.63f,4978.03f,5274.04f,5587.65f,5919.91f,6271.93f,6644.88f,7040f,7458.62f,7902.13f,
1000000
// http://www.phy.mtu.edu/~suits/notefreqs.html
// begins at C0. ends at B8
private static readonly float[] FreqTable =
{
0, 16.35f,17.32f,18.35f,19.45f,20.6f,21.83f,23.12f,24.5f,25.96f,27.5f,29.14f,30.87f,32.7f,34.65f,36.71f,38.89f,41.2f,43.65f,46.25f,49f,51.91f,55f,58.27f,61.74f,65.41f,69.3f,73.42f,77.78f,82.41f,87.31f,92.5f,98f,103.83f,110f,116.54f,123.47f,130.81f,138.59f,146.83f,155.56f,164.81f,174.61f,185f,196f,207.65f,220f,233.08f,246.94f,261.63f,277.18f,293.66f,311.13f,329.63f,349.23f,369.99f,392f,415.3f,440f,466.16f,493.88f,523.25f,554.37f,587.33f,622.25f,659.25f,698.46f,739.99f,783.99f,830.61f,880f,932.33f,987.77f,1046.5f,1108.73f,1174.66f,1244.51f,1318.51f,1396.91f,1479.98f,1567.98f,1661.22f,1760f,1864.66f,1975.53f,2093f,2217.46f,2349.32f,2489.02f,2637.02f,2793.83f,2959.96f,3135.96f,3322.44f,3520f,3729.31f,3951.07f,4186.01f,4434.92f,4698.63f,4978.03f,5274.04f,5587.65f,5919.91f,6271.93f,6644.88f,7040f,7458.62f,7902.13f, 1000000
};
static readonly string[] noteNames = { "C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-" };
private static readonly string[] NoteNames = { "C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-" };
string NameForNote(int note)
private string NameForNote(int note)
{
int tone = note % 12;
int octave = note / 12;
return noteNames[tone] + octave;
return NoteNames[tone] + octave;
}
//this isnt thoroughly debugged but it seems to work OK
//pitch bends are massively broken anyway
int FindNearestNote(float freq)
// this isn't thoroughly debugged but it seems to work OK
// pitch bends are massively broken anyway
private int FindNearestNote(float freq)
{
for (int i = 1; i < freqtbl.Length; i++)
for (int i = 1; i < FreqTable.Length; i++)
{
float a = freqtbl[i - 1];
float b = freqtbl[i];
float c = freqtbl[i + 1];
float a = FreqTable[i - 1];
float b = FreqTable[i];
float c = FreqTable[i + 1];
var range = ((a + b) / 2).RangeTo((b + c) / 2);
if (range.Contains(freq)) return i - 1;
if (range.Contains(freq))
{
return i - 1;
}
}
return 95; //I guess?
return 95; // I guess?
}
struct PulseState
private struct PulseState
{
public bool en;
public byte vol, type;
public int note;
public bool En;
public byte Vol;
public byte Type;
public int Note;
}
struct TriangleState
private struct TriangleState
{
public bool en;
public int note;
public bool En;
public int Note;
}
struct NoiseState
private struct NoiseState
{
public bool en;
public byte vol;
public int note;
public bool En;
public byte Vol;
public int Note;
}
class ApuState
private class ApuState
{
public PulseState pulse0, pulse1;
public TriangleState triangle;
public NoiseState noise;
public PulseState Pulse0;
public PulseState Pulse1;
public TriangleState Triangle;
public NoiseState Noise;
}
class Stupid : ICSharpCode.SharpZipLib.Zip.IStaticDataSource
private class Stupid : IStaticDataSource
{
public Stream stream;
public Stream GetSource() { return stream; }
public Stream Stream { get; set; }
public Stream GetSource() => Stream;
}
private void btnExport_Click(object sender, EventArgs e)
private void Export_Click(object sender, EventArgs e)
{
//acquire target
using var sfd = new SaveFileDialog
{
Filter = new FilesystemFilter("Renoise Song Files", new[] { "xrns" }).ToString()
};
if (sfd.ShowDialog() != DialogResult.OK)
if (sfd.ShowDialog().IsOk())
{
return;
}
//configuration:
// configuration:
var outPath = sfd.FileName;
string templatePath = Path.Combine(Path.GetDirectoryName(outPath), "template.xrns");
string templatePath = Path.Combine(Path.GetDirectoryName(outPath) ?? "", "template.xrns");
int configuredPatternLength = int.Parse(txtPatternLength.Text);
//load template
MemoryStream msSongXml = new MemoryStream();
var zfTemplate = new ICSharpCode.SharpZipLib.Zip.ZipFile(templatePath);
// load template
var msSongXml = new MemoryStream();
var zfTemplate = new ZipFile(templatePath);
{
int zfSongXmlIndex = zfTemplate.FindEntry("Song.xml", true);
using var zis = zfTemplate.GetInputStream(zfTemplate.GetEntry("Song.xml"));
byte[] buffer = new byte[4096]; // 4K is optimum
ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(zis, msSongXml, buffer);
byte[] buffer = new byte[4096]; // 4K is optimum
StreamUtils.Copy(zis, msSongXml, buffer);
}
var templateRoot = XElement.Parse(System.Text.Encoding.UTF8.GetString(msSongXml.ToArray()));
var templateRoot = XElement.Parse(Encoding.UTF8.GetString(msSongXml.ToArray()));
//get the pattern pool, and whack the child nodes
var xPatterns = templateRoot.XPathSelectElement("//Patterns");
@ -145,16 +153,16 @@ namespace BizHawk.Client.EmuHawk
writer.WriteLine("<Patterns>");
int pulse0_lastNote = -1;
int pulse0_lastType = -1;
int pulse1_lastNote = -1;
int pulse1_lastType = -1;
int tri_lastNote = -1;
int noise_lastNote = -1;
int pulse0LastNote = -1;
int pulse0LastType = -1;
int pulse1LastNote = -1;
int pulse1LastType = -1;
int triLastNote = -1;
int noiseLastNote = -1;
int patternCount = 0;
int time = 0;
while (time < Log.Count)
while (time < _log.Count)
{
patternCount++;
@ -164,86 +172,95 @@ namespace BizHawk.Client.EmuHawk
writer.WriteLine("<Tracks>");
//write the pulse tracks
for (int TRACK = 0; TRACK < 2; TRACK++)
for (int track = 0; track < 2; track++)
{
writer.WriteLine("<PatternTrack type=\"PatternTrack\">");
writer.WriteLine("<Lines>");
int lastNote = TRACK == 0 ? pulse0_lastNote : pulse1_lastNote;
int lastType = TRACK == 0 ? pulse0_lastType : pulse1_lastType;
int lastNote = track == 0 ? pulse0LastNote : pulse1LastNote;
int lastType = track == 0 ? pulse0LastType : pulse1LastType;
for (int i = 0; i < configuredPatternLength; i++)
{
int patLine = i;
int index = i + time;
if (index >= Log.Count) continue;
if (index >= _log.Count) continue;
var rec = Log[index];
var rec = _log[index];
PulseState pulse = new PulseState();
if (TRACK == 0) pulse = rec.pulse0;
if (TRACK == 1) pulse = rec.pulse1;
if (track == 0) pulse = rec.Pulse0;
if (track == 1) pulse = rec.Pulse1;
//transform quieted notes to dead notes
//blech its buggy, im tired
//if (pulse.vol == 0)
// pulse.en = false;
// transform quieted notes to dead notes
// blech its buggy, im tired
////if (pulse.vol == 0)
//// pulse.en = false;
bool keyoff = false, keyon = false;
if (lastNote != -1 && !pulse.en)
bool keyOff = false, keyOn = false;
if (lastNote != -1 && !pulse.En)
{
lastNote = -1;
lastType = -1;
keyoff = true;
keyOff = true;
}
else if (lastNote != pulse.note && pulse.en)
keyon = true;
if (lastType != pulse.type && pulse.note != -1)
keyon = true;
if (pulse.en)
else if (lastNote != pulse.Note && pulse.En)
{
lastNote = pulse.note;
lastType = pulse.type;
keyOn = true;
}
if (lastType != pulse.Type && pulse.Note != -1)
{
keyOn = true;
}
if (pulse.En)
{
lastNote = pulse.Note;
lastType = pulse.Type;
}
writer.WriteLine("<Line index=\"{0}\">", patLine);
writer.WriteLine("<NoteColumns>");
writer.WriteLine("<NoteColumn>");
if (keyon)
if (keyOn)
{
writer.WriteLine("<Note>{0}</Note>", NameForNote(pulse.note));
writer.WriteLine("<Instrument>{0:X2}</Instrument>", pulse.type);
writer.WriteLine("<Note>{0}</Note>", NameForNote(pulse.Note));
writer.WriteLine("<Instrument>{0:X2}</Instrument>", pulse.Type);
}
else if (keyOff)
{
writer.WriteLine("<Note>OFF</Note>");
}
else if (keyoff) writer.WriteLine("<Note>OFF</Note>");
if(lastNote != -1)
writer.WriteLine("<Volume>{0:X2}</Volume>", pulse.vol * 8);
if (lastNote != -1)
{
writer.WriteLine("<Volume>{0:X2}</Volume>", pulse.Vol * 8);
}
writer.WriteLine("</NoteColumn>");
writer.WriteLine("</NoteColumns>");
writer.WriteLine("</Line>");
}
//close PatternTrack
// close PatternTrack
writer.WriteLine("</Lines>");
writer.WriteLine("</PatternTrack>");
if (TRACK == 0)
if (track == 0)
{
pulse0_lastNote = lastNote;
pulse0_lastType = lastType;
pulse0LastNote = lastNote;
pulse0LastType = lastType;
}
else
{
pulse1_lastNote = lastNote;
pulse1_lastType = lastType;
pulse1LastNote = lastNote;
pulse1LastType = lastType;
}
} //pulse tracks loop
} // pulse tracks loop
//triangle track generation
// triangle track generation
{
writer.WriteLine("<PatternTrack type=\"PatternTrack\">");
writer.WriteLine("<Lines>");
@ -253,37 +270,40 @@ namespace BizHawk.Client.EmuHawk
int patLine = i;
int index = i + time;
if (index >= Log.Count) continue;
if (index >= _log.Count) continue;
var rec = Log[index];
var rec = _log[index];
TriangleState tri = rec.triangle;
TriangleState tri = rec.Triangle;
{
bool keyoff = false, keyon = false;
if (tri_lastNote != -1 && !tri.en)
bool keyOff = false, keyOn = false;
if (triLastNote != -1 && !tri.En)
{
tri_lastNote = -1;
keyoff = true;
triLastNote = -1;
keyOff = true;
}
else if (tri_lastNote != tri.note && tri.en)
keyon = true;
else if (triLastNote != tri.Note && tri.En)
keyOn = true;
if(tri.en)
tri_lastNote = tri.note;
if(tri.En)
triLastNote = tri.Note;
writer.WriteLine("<Line index=\"{0}\">", patLine);
writer.WriteLine("<NoteColumns>");
writer.WriteLine("<NoteColumn>");
if (keyon)
if (keyOn)
{
writer.WriteLine("<Note>{0}</Note>", NameForNote(tri.note));
writer.WriteLine("<Note>{0}</Note>", NameForNote(tri.Note));
writer.WriteLine("<Instrument>08</Instrument>");
}
else if (keyoff) writer.WriteLine("<Note>OFF</Note>");
else if (keyOff)
{
writer.WriteLine("<Note>OFF</Note>");
}
//no need for tons of these
//if(keyon) writer.WriteLine("<Volume>80</Volume>");
// no need for tons of these
////if(keyon) writer.WriteLine("<Volume>80</Volume>");
writer.WriteLine("</NoteColumn>");
writer.WriteLine("</NoteColumns>");
@ -291,12 +311,12 @@ namespace BizHawk.Client.EmuHawk
}
}
//close PatternTrack
// close PatternTrack
writer.WriteLine("</Lines>");
writer.WriteLine("</PatternTrack>");
}
//noise track generation
// noise track generation
{
writer.WriteLine("<PatternTrack type=\"PatternTrack\">");
writer.WriteLine("<Lines>");
@ -306,42 +326,46 @@ namespace BizHawk.Client.EmuHawk
int patLine = i;
int index = i + time;
if (index >= Log.Count) continue;
if (index >= _log.Count) continue;
var rec = Log[index];
var rec = _log[index];
NoiseState noise = rec.noise;
NoiseState noise = rec.Noise;
//transform quieted notes to dead notes
//blech its buggy, im tired
//if (noise.vol == 0)
// noise.en = false;
// transform quieted notes to dead notes
// blech its buggy, im tired
////if (noise.vol == 0)
//// noise.en = false;
{
bool keyoff = false, keyon = false;
if (noise_lastNote != -1 && !noise.en)
bool keyOff = false, keyOn = false;
if (noiseLastNote != -1 && !noise.En)
{
noise_lastNote = -1;
keyoff = true;
noiseLastNote = -1;
keyOff = true;
}
else if (noiseLastNote != noise.Note && noise.En)
{
keyOn = true;
}
else if (noise_lastNote != noise.note && noise.en)
keyon = true;
if (noise.en)
noise_lastNote = noise.note;
if (noise.En)
{
noiseLastNote = noise.Note;
}
writer.WriteLine("<Line index=\"{0}\">", patLine);
writer.WriteLine("<NoteColumns>");
writer.WriteLine("<NoteColumn>");
if (keyon)
if (keyOn)
{
writer.WriteLine("<Note>{0}</Note>", NameForNote(noise.note));
writer.WriteLine("<Note>{0}</Note>", NameForNote(noise.Note));
writer.WriteLine("<Instrument>04</Instrument>");
}
else if (keyoff) writer.WriteLine("<Note>OFF</Note>");
else if (keyOff) writer.WriteLine("<Note>OFF</Note>");
if (noise_lastNote != -1)
writer.WriteLine("<Volume>{0:X2}</Volume>", noise.vol * 8);
if (noiseLastNote != -1)
writer.WriteLine("<Volume>{0:X2}</Volume>", noise.Vol * 8);
writer.WriteLine("</NoteColumn>");
writer.WriteLine("</NoteColumns>");
@ -349,13 +373,13 @@ namespace BizHawk.Client.EmuHawk
}
}
//close PatternTrack
// close PatternTrack
writer.WriteLine("</Lines>");
writer.WriteLine("</PatternTrack>");
} //noise track generation
} // noise track generation
//write empty track for now for pcm
for (int TRACK = 4; TRACK < 5; TRACK++)
// write empty track for now for pcm
for (int track = 4; track < 5; track++)
{
writer.WriteLine("<PatternTrack type=\"PatternTrack\">");
writer.WriteLine("<Lines>");
@ -363,19 +387,19 @@ namespace BizHawk.Client.EmuHawk
writer.WriteLine("</PatternTrack>");
}
//we definitely need a dummy master track now
// we definitely need a dummy master track now
writer.WriteLine("<PatternMasterTrack type=\"PatternMasterTrack\">");
writer.WriteLine("</PatternMasterTrack>");
//close tracks
// close tracks
writer.WriteLine("</Tracks>");
//close pattern
// close pattern
writer.WriteLine("</Pattern>");
time += configuredPatternLength;
} //main pattern loop
} // main pattern loop
writer.WriteLine("</Patterns>");
writer.Flush();
@ -407,100 +431,98 @@ namespace BizHawk.Client.EmuHawk
templateRoot.Save(msOutXml);
msOutXml.Flush();
msOutXml.Position = 0;
var zfOutput = new ICSharpCode.SharpZipLib.Zip.ZipFile(outPath);
var zfOutput = new ZipFile(outPath);
zfOutput.BeginUpdate();
zfOutput.Add(new Stupid { stream = msOutXml }, "Song.xml");
zfOutput.Add(new Stupid { Stream = msOutXml }, "Song.xml");
zfOutput.CommitUpdate();
zfOutput.Close();
//for easier debugging, write patterndata XML
//DUMP_TO_DISK(msOutXml.ToArray())
// for easier debugging, write patterndata XML
////DUMP_TO_DISK(msOutXml.ToArray())
}
private readonly List<ApuState> _log = new List<ApuState>();
List<ApuState> Log = new List<ApuState>();
void DebugCallback()
private void DebugCallback()
{
//fpulse = fCPU/(16*(t+1)) (where fCPU is 1.789773 MHz for NTSC, 1.662607 MHz for PAL, and 1.773448 MHz for Dendy)
//ftriangle = fCPU/(32*(tval + 1))
////fpulse = fCPU/(16*(t+1)) (where fCPU is 1.789773 MHz for NTSC, 1.662607 MHz for PAL, and 1.773448 MHz for Dendy)
////ftriangle = fCPU/(32*(tval + 1))
var apu = Nes.apu;
//evaluate the pitches
int pulse0_period = apu.pulse[0].timer_reload_value;
float pulse0_freq = 1789773.0f / (16.0f * (pulse0_period + 1));
int pulse0_note = FindNearestNote(pulse0_freq);
// evaluate the pitches
int pulse0Period = apu.pulse[0].timer_reload_value;
float pulse0Freq = 1789773.0f / (16.0f * (pulse0Period + 1));
int pulse0Note = FindNearestNote(pulse0Freq);
int pulse1_period = apu.pulse[1].timer_reload_value;
float pulse1_freq = 1789773.0f / (16.0f * (pulse1_period + 1));
int pulse1_note = FindNearestNote(pulse1_freq);
int pulse1Period = apu.pulse[1].timer_reload_value;
float pulse1Freq = 1789773.0f / (16.0f * (pulse1Period + 1));
int pulse1Note = FindNearestNote(pulse1Freq);
int tri_period = apu.triangle.Debug_PeriodValue;
float tri_freq = 1789773.0f / (32.0f * (tri_period + 1));
int tri_note = FindNearestNote(tri_freq);
int triPeriod = apu.triangle.Debug_PeriodValue;
float triFreq = 1789773.0f / (32.0f * (triPeriod + 1));
int triNote = FindNearestNote(triFreq);
//uncertain
int noise_period = apu.noise.Debug_Period;
float noise_freq = 1789773.0f / (16.0f * (noise_period + 1));
int noise_note = FindNearestNote(noise_freq);
int noisePeriod = apu.noise.Debug_Period;
float noiseFreq = 1789773.0f / (16.0f * (noisePeriod + 1));
int noiseNote = FindNearestNote(noiseFreq);
// create the record
var rec = new ApuState
{
pulse0 =
Pulse0 =
{
en = !apu.pulse[0].Debug_IsSilenced,
vol = (byte) apu.pulse[0].Debug_Volume,
note = pulse0_note,
type = (byte) apu.pulse[0].Debug_DutyType
En = !apu.pulse[0].Debug_IsSilenced,
Vol = (byte) apu.pulse[0].Debug_Volume,
Note = pulse0Note,
Type = (byte) apu.pulse[0].Debug_DutyType
},
pulse1 =
Pulse1 =
{
en = !apu.pulse[1].Debug_IsSilenced,
vol = (byte) apu.pulse[1].Debug_Volume,
note = pulse1_note,
type = (byte) apu.pulse[1].Debug_DutyType
En = !apu.pulse[1].Debug_IsSilenced,
Vol = (byte) apu.pulse[1].Debug_Volume,
Note = pulse1Note,
Type = (byte) apu.pulse[1].Debug_DutyType
},
triangle =
Triangle =
{
en = !apu.triangle.Debug_IsSilenced,
note = tri_note
En = !apu.triangle.Debug_IsSilenced,
Note = triNote
},
noise =
Noise =
{
en = !apu.noise.Debug_IsSilenced,
vol = (byte) apu.noise.Debug_Volume,
note = noise_note
En = !apu.noise.Debug_IsSilenced,
Vol = (byte) apu.noise.Debug_Volume,
Note = noiseNote
}
};
Log.Add(rec);
_log.Add(rec);
SyncContents();
}
void SyncContents()
private void SyncContents()
{
lblContents.Text = $"{Log.Count} Rows";
lblContents.Text = $"{_log.Count} Rows";
}
private void btnControl_Click(object sender, EventArgs e)
private void BtnControl_Click(object sender, EventArgs e)
{
if(IsRunning)
if(_isRunning)
{
SyncContents();
Nes.apu.DebugCallback = null;
Nes.apu.DebugCallbackDivider = 0;
IsRunning = false;
_isRunning = false;
btnControl.Text = "Start";
}
else
{
Log.Clear();
_log.Clear();
Nes.apu.DebugCallback = DebugCallback;
Nes.apu.DebugCallbackDivider = int.Parse(txtDivider.Text);
IsRunning = true;
_isRunning = true;
btnControl.Text = "Stop";
}
}