Skip to content

Commit

Permalink
Merge pull request #404 from ogamespec/main
Browse files Browse the repository at this point in the history
Dump audio/video
  • Loading branch information
ogamespec authored May 13, 2023
2 parents 277a856 + 2bd994c commit dc89594
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Breaknes/Breaknes/FormAbout.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Breaknes/Breaknes/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ private void loadROMDumpToolStripMenuItem_Click(object sender, EventArgs e)
}
BreaksCore.Reset();
Text = original_title + " - " + filename;
vid_out = new(OnRenderField);
var settings = FormSettings.LoadSettings();
var rom_name = Path.GetFileNameWithoutExtension(filename);
vid_out = new(OnRenderField, settings.DumpVideo, settings.DumpVideoDir, rom_name);
vid_out.SetOutputPictureBox(pictureBox1);
snd_out = new(Handle);
snd_out = new(Handle, settings.DumpAudio, settings.DumpAudioDir, rom_name);
board.Paused = debuggers.Count != 0;

foreach (var inst in debuggers)
Expand Down
26 changes: 26 additions & 0 deletions Breaknes/Breaknes/FormSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ static BreaknesSettings SetDefaultSettings()
BreaknesSettings settings = new();

settings.MainBoard = "NES-001 (PCB rev. -01 to -04) (1985-1986)";
settings.DumpAudio = false;
settings.DumpAudioDir = "";
settings.DumpVideo = false;
settings.DumpVideoDir = "";

SaveSettings(settings);

Expand Down Expand Up @@ -90,6 +94,28 @@ public class BreaknesSettings
[DefaultValue("")]
[TypeConverter(typeof(FormatStringConverter_MainBoard))]
public string? MainBoard { get; set; }

[Category("Debug")]
[Description("Enable sound dump. Format: 16-bit, little-endian, mono, 48000 Hz")]
[DefaultValue(false)]
public bool DumpAudio { get; set; }

[Category("Debug")]
[Description("The directory where the sound dump will be saved. File name RomName_aux.bin")]
[DefaultValue("")]
[EditorAttribute(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
public string DumpAudioDir { get; set; }

[Category("Debug")]
[Description("Turn on the PPU fields dump. This will dump a field with raw pixels and .bmp")]
[DefaultValue(false)]
public bool DumpVideo { get; set; }

[Category("Debug")]
[Description("The directory where the field dumps will be stored. The names of the files you can figure out by yourself when you see the dumps.")]
[DefaultValue("")]
[EditorAttribute(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
public string DumpVideoDir { get; set; }
}

// https://stackoverflow.com/questions/24503462/how-to-show-drop-down-control-in-property-grid
Expand Down
34 changes: 33 additions & 1 deletion Breaknes/Breaknes/SoundProcessing.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SharpTools;
using System.IO;
using System.Reflection.Metadata;
using System.Windows.Forms;

Expand All @@ -19,8 +20,16 @@ public class AudioRender
private int DecimateEach = 1;
private int DecimateCounter = 0;

public AudioRender(System.IntPtr handle)
private bool dump_audio = false;
private string dump_audio_dir = "";
private string dump_rom_name;

public AudioRender(System.IntPtr handle, bool dump, string dump_dir, string rom_name)
{
dump_audio = dump;
dump_audio_dir = dump_dir;
dump_rom_name = rom_name;

audio_backend = new DSound(handle);

// SRC
Expand Down Expand Up @@ -69,10 +78,33 @@ private void Playback()
{
Console.WriteLine("Play 1 second");
Dma = true;
DumpAudio();
audio_backend.PlaySampleBuf(OutputSampleRate, SampleBuf, OutputDC);
SampleBuf.Clear();
Dma = false;
}
}

private void DumpAudio()
{
if (dump_audio)
{
int numberOfSamples = SampleBuf.Count;
byte[] bytes = new byte[numberOfSamples * 2];

for (int i = 0; i < numberOfSamples; i++)
{
short value = (short)((SampleBuf[i]) * Int16.MaxValue);

bytes[2 * i] = (byte)((value >> 0) & 0xff);
bytes[2 * i + 1] = (byte)((value >> 8) & 0xff);
}

using (var stream = new FileStream(dump_audio_dir + "/" + dump_rom_name + "_aux.bin", FileMode.Append))
{
stream.Write(bytes, 0, bytes.Length);
}
}
}
}
}
41 changes: 38 additions & 3 deletions Breaknes/Breaknes/VideoProcessing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class VideoRender
private int SyncPos = -1;

private Color[] field = new Color[256 * 240];
private byte[] raw_field = new byte[256 * 240 * 2];
private int CurrentScan = 0;

private Bitmap? field_pic = null;
Expand All @@ -24,11 +25,19 @@ public class VideoRender

private long field_counter = 0;

public VideoRender(OnRenderField _onRender)
private bool dump_video = false;
private string dump_video_dir = "";
private string dump_rom_name;

public VideoRender(OnRenderField _onRender, bool dump, string dump_dir, string rom_name)
{
onRenderField = _onRender;
BreaksCore.GetPpuSignalFeatures(out ppu_features);

dump_video = dump;
dump_video_dir = dump_dir;
dump_rom_name = rom_name;

SamplesPerScan = ppu_features.PixelsPerScan * ppu_features.SamplesPerPCLK;
ScanBuffer = new BreaksCore.VideoOutSample[2 * SamplesPerScan];
WritePtr = 0;
Expand Down Expand Up @@ -84,7 +93,7 @@ public void ProcessSample (BreaksCore.VideoOutSample sample)
}
}

void ProcessScanRAW()
private void ProcessScanRAW()
{
int ReadPtr = SyncPos;

Expand All @@ -107,6 +116,13 @@ void ProcessScanRAW()
BreaksCore.ConvertRAWToRGB(ScanBuffer[ReadPtr].raw, out r, out g, out b);

field[CurrentScan * 256 + i] = Color.FromArgb(r, g, b);

if (dump_video)
{
UInt16 raw_color = ScanBuffer[ReadPtr].raw;
raw_field[2 * CurrentScan * 256 + i] = (byte)((raw_color >> 0) & 0xff);
raw_field[2 * CurrentScan * 256 + i + 1] = (byte)((raw_color >> 0) & 0xff);
}
}

ReadPtr += ppu_features.SamplesPerPCLK;
Expand All @@ -120,7 +136,7 @@ void ProcessScanRAW()
}
}

void VisualizeField()
private void VisualizeField()
{
int w = 256;
int h = 240;
Expand All @@ -142,6 +158,8 @@ void VisualizeField()
}
}

DumpVideo();

if (output_picture_box != null)
{
output_picture_box.Image = field_pic;
Expand All @@ -152,5 +170,22 @@ void VisualizeField()

onRenderField?.Invoke();
}

private void DumpVideo()
{
if (dump_video)
{
string raw_name = dump_video_dir + "/" + dump_rom_name + "_" + field_counter.ToString("D5") + ".bin";
string bmp_name = dump_video_dir + "/" + dump_rom_name + "_" + field_counter.ToString("D5") + ".bmp";

File.WriteAllBytes(raw_name, raw_field);
if (field_pic != null)
{
field_pic.Save(bmp_name, System.Drawing.Imaging.ImageFormat.Bmp);
}

Console.WriteLine("field: {0}", field_counter);
}
}
}
}

0 comments on commit dc89594

Please sign in to comment.