Skip to content

Commit

Permalink
Added sound recording window
Browse files Browse the repository at this point in the history
  • Loading branch information
xdanieldzd committed Apr 24, 2022
1 parent 5ce8658 commit a03ec4a
Show file tree
Hide file tree
Showing 12 changed files with 1,574 additions and 92 deletions.
1 change: 0 additions & 1 deletion StoicGoose/GlobalVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public static class GlobalVariables
public static readonly bool EnableSuperVerbosity = false;
public static readonly bool EnableOpenGLDebug = false;

public static readonly bool EnableDebugSoundRecording = false;
public static readonly bool EnableAutostartLastRom = false;

public static readonly bool EnableSkipBootstrapIfFound = false;
Expand Down
29 changes: 0 additions & 29 deletions StoicGoose/Handlers/SoundHandler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

using OpenTK.Audio.OpenAL;
using OpenTK.Audio.OpenAL.Extensions.Creative.EFX;

using StoicGoose.IO;
using StoicGoose.WinForms;

namespace StoicGoose.Handlers
Expand Down Expand Up @@ -40,10 +38,6 @@ public class SoundHandler

float volume = 1.0f;

WaveFileWriter wavWriter = default;

public bool IsRecording { get; private set; }

public SoundHandler(int sampleRate, int numChannels)
{
SampleRate = sampleRate;
Expand Down Expand Up @@ -110,26 +104,6 @@ public void Shutdown()
sampleQueue.Clear();
}

public void BeginRecording()
{
wavWriter = new(SampleRate, NumChannels);

IsRecording = true;
}

public void SaveRecording(string filename)
{
using var stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
wavWriter.Save(stream);

IsRecording = false;
}

public void CancelRecording()
{
IsRecording = false;
}

private void ThreadMainLoop()
{
while (true)
Expand Down Expand Up @@ -185,9 +159,6 @@ public void EnqueueSamples(object sender, EnqueueSamplesEventArgs e)
}

sampleQueue.Enqueue(e.Samples.ToArray());

if (IsRecording)
wavWriter.Write(e.Samples);
}

public void ClearSampleBuffer()
Expand Down
31 changes: 25 additions & 6 deletions StoicGoose/IO/WaveFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,48 @@

namespace StoicGoose.IO
{
public sealed class WaveFileWriter
public sealed class WaveFileWriter : IDisposable
{
readonly Stream outStream = default;

readonly WaveHeader waveHeader = default;
readonly FormatChunk formatChunk = default;
readonly DataChunk dataChunk = default;

public WaveFileWriter(int sampleRate, int numChannels)
public WaveFileWriter(string filename, int sampleRate, int numChannels)
{
outStream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);

waveHeader = new();
formatChunk = new FormatChunk(sampleRate, numChannels);
dataChunk = new();
}

~WaveFileWriter()
{
Dispose();
}

public void Dispose()
{
outStream.Flush();
outStream.Dispose();

GC.SuppressFinalize(this);
}

public void Write(short[] samples) => dataChunk.Write(samples);

public void Save(Stream stream)
public void Save()
{
waveHeader.FileLength += formatChunk.ChunkSize + 8;
waveHeader.FileLength += dataChunk.ChunkSize + 8;

stream.Write(waveHeader.Bytes);
stream.Write(formatChunk.Bytes);
stream.Write(dataChunk.Bytes);
outStream.Write(waveHeader.Bytes);
outStream.Write(formatChunk.Bytes);
outStream.Write(dataChunk.Bytes);

outStream.Flush();
}

private class WaveHeader
Expand Down
101 changes: 59 additions & 42 deletions StoicGoose/MainForm.Designer.cs

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

23 changes: 17 additions & 6 deletions StoicGoose/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public partial class MainForm : Form
ImGuiHandler imGuiHandler = default;
EmulatorHandler emulatorHandler = default;

/* Misc. windows */
SoundRecorderForm soundRecorderForm = default;

/* Misc. runtime variables */
bool isInitialized = false;
Type machineType = default;
Expand Down Expand Up @@ -79,6 +82,8 @@ private void MainForm_Load(object sender, EventArgs e)
machineType = Program.Configuration.General.PreferOriginalWS ? typeof(WonderSwan) : typeof(WonderSwanColor);

InitializeHandlers();
InitializeWindows();

VerifyConfiguration();

SizeAndPositionWindow();
Expand Down Expand Up @@ -152,9 +157,6 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
emulatorHandler.Shutdown();

Program.SaveConfiguration();

if (GlobalVariables.EnableLocalDebugIO && soundHandler.IsRecording)
soundHandler.SaveRecording(@"D:\Temp\Goose\sound.wav");
}

private void InitializeHandlers()
Expand All @@ -171,9 +173,6 @@ private void InitializeHandlers()
soundHandler.SetMute(Program.Configuration.Sound.Mute);
soundHandler.SetLowPassFilter(Program.Configuration.Sound.LowPassFilter);

if (GlobalVariables.EnableDebugSoundRecording)
soundHandler.BeginRecording();

inputHandler = new InputHandler(renderControl) { IsVerticalOrientation = isVerticalOrientation };
inputHandler.SetKeyMapping(Program.Configuration.Input.GameControls, Program.Configuration.Input.SystemControls);
inputHandler.SetVerticalRemapping(emulatorHandler.Machine.Metadata.VerticalControlRemap
Expand Down Expand Up @@ -238,6 +237,13 @@ private void InitializeHandlers()
internalEepromPath = Path.Combine(Program.InternalDataPath, emulatorHandler.Machine.Metadata.InternalEepromFilename);
}

private void InitializeWindows()
{
soundRecorderForm = new(soundHandler.SampleRate, soundHandler.NumChannels);

emulatorHandler.Machine.SoundController.EnqueueSamples += soundRecorderForm.EnqueueSamples;
}

private void VerifyConfiguration()
{
var metadata = emulatorHandler.Machine.Metadata;
Expand Down Expand Up @@ -634,6 +640,11 @@ private void loadROMToolStripMenuItem_Click(object sender, EventArgs e)
UnpauseEmulation();
}

private void saveWAVToolStripMenuItem_Click(object sender, EventArgs e)
{
soundRecorderForm.Show();
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
Expand Down
Loading

0 comments on commit a03ec4a

Please sign in to comment.