1
0

refactor: modernize code, improve performance, and clean up various components.

This commit is contained in:
2026-03-01 21:05:35 +01:00
parent 15f9647f8a
commit a6c7df0a71
37 changed files with 240 additions and 627 deletions

View File

@@ -1,32 +1,24 @@
using System.Diagnostics;
using Toak.Core;
using Toak.IO;
using Toak.Core.Interfaces;
namespace Toak.Audio;
public class AudioRecorder : IAudioRecorder
public class AudioRecorder(IRecordingStateTracker stateTracker, INotifications notifications) : IAudioRecorder
{
private readonly string WavPath = Constants.Paths.RecordingWavFile;
private readonly IRecordingStateTracker _stateTracker;
private readonly INotifications _notifications;
private readonly string _wavPath = Constants.Paths.RecordingWavFile;
private readonly IRecordingStateTracker _stateTracker = stateTracker;
private readonly INotifications _notifications = notifications;
public AudioRecorder(IRecordingStateTracker stateTracker, INotifications notifications)
{
_stateTracker = stateTracker;
_notifications = notifications;
}
public string GetWavPath() => WavPath;
public string GetWavPath() => _wavPath;
public void StartRecording()
{
if (File.Exists(WavPath))
if (File.Exists(_wavPath))
{
Logger.LogDebug($"Deleting old audio file: {WavPath}");
File.Delete(WavPath);
Logger.LogDebug($"Deleting old audio file: {_wavPath}");
File.Delete(_wavPath);
}
Logger.LogDebug("Starting pw-record to record audio...");
@@ -34,7 +26,7 @@ public class AudioRecorder : IAudioRecorder
var pInfo = new ProcessStartInfo
{
FileName = Constants.Commands.AudioRecord,
Arguments = $"--rate=16000 --channels=1 --format=s16 \"{WavPath}\"",
Arguments = $"--rate=16000 --channels=1 --format=s16 \"{_wavPath}\"",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,

View File

@@ -1,33 +1,23 @@
using System;
using System.Diagnostics;
using System.IO;
using Toak.Core;
using Toak.IO;
using Toak.Core.Interfaces;
namespace Toak.Audio;
public class FfmpegAudioRecorder : IAudioRecorder
public class FfmpegAudioRecorder(IRecordingStateTracker stateTracker, INotifications notifications) : IAudioRecorder
{
private readonly string WavPath = Constants.Paths.RecordingWavFile;
private readonly IRecordingStateTracker _stateTracker;
private readonly INotifications _notifications;
private readonly string _wavPath = Constants.Paths.RecordingWavFile;
private readonly IRecordingStateTracker _stateTracker = stateTracker;
private readonly INotifications _notifications = notifications;
public FfmpegAudioRecorder(IRecordingStateTracker stateTracker, INotifications notifications)
{
_stateTracker = stateTracker;
_notifications = notifications;
}
public string GetWavPath() => WavPath;
public string GetWavPath() => _wavPath;
public void StartRecording()
{
if (File.Exists(WavPath))
if (File.Exists(_wavPath))
{
Logger.LogDebug($"Deleting old audio file: {WavPath}");
File.Delete(WavPath);
Logger.LogDebug($"Deleting old audio file: {_wavPath}");
File.Delete(_wavPath);
}
Logger.LogDebug("Starting ffmpeg to record audio...");
@@ -35,7 +25,7 @@ public class FfmpegAudioRecorder : IAudioRecorder
var pInfo = new ProcessStartInfo
{
FileName = Constants.Commands.AudioFfmpeg,
Arguments = $"-f pulse -i default -ac 1 -ar 16000 \"{WavPath}\"",
Arguments = $"-f pulse -i default -ac 1 -ar 16000 \"{_wavPath}\"",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
@@ -53,35 +43,31 @@ public class FfmpegAudioRecorder : IAudioRecorder
public void StopRecording()
{
var pid = _stateTracker.GetRecordingPid();
if (pid.HasValue)
if (!pid.HasValue) return;
Logger.LogDebug($"Found active ffmpeg process with PID {pid.Value}. Attempting to stop...");
try
{
Logger.LogDebug($"Found active ffmpeg process with PID {pid.Value}. Attempting to stop...");
try
var process = Process.GetProcessById(pid.Value);
if (process.HasExited) return;
// Gracefully stop ffmpeg using SIGINT to ensure WAV headers are finalizing cleanly
Process.Start(new ProcessStartInfo
{
var process = Process.GetProcessById(pid.Value);
if (!process.HasExited)
{
// Gracefully stop ffmpeg using SIGINT to ensure WAV headers are finalizing cleanly
Process.Start(new ProcessStartInfo
{
FileName = Constants.Commands.ProcessKill,
Arguments = $"-INT {pid.Value}",
CreateNoWindow = true,
UseShellExecute = false
})?.WaitForExit();
FileName = Constants.Commands.ProcessKill,
Arguments = $"-INT {pid.Value}",
CreateNoWindow = true,
UseShellExecute = false
})?.WaitForExit();
process.WaitForExit(2000); // give it a moment to flush
}
}
catch (Exception ex)
{
// Process might already be dead
Console.WriteLine($"[FfmpegAudioRecorder] Error stopping ffmpeg: {ex.Message}");
}
finally
{
_stateTracker.ClearRecording();
}
process.WaitForExit(2000); // give it a moment to flush
}
catch (Exception ex)
{
// Process might already be dead
Console.WriteLine($"[FfmpegAudioRecorder] Error stopping ffmpeg: {ex.Message}");
}
finally
{
_stateTracker.ClearRecording();
}
}
}