Fix disposing wasapi audio device on chart exit

This commit is contained in:
Mercurio 2024-09-05 12:55:10 +02:00
parent a57c1c2131
commit 426268b512
8 changed files with 29 additions and 36 deletions

View file

@ -13,7 +13,7 @@ class Program
private static Process? _ffmpegProcess; private static Process? _ffmpegProcess;
private static string _ffmpegFolderPath = "E:\\autorecording"; // Output folder path private static string _ffmpegFolderPath = "E:\\autorecording"; // Output folder path
private static WasapiCapture? _waveSource; private static WasapiCapture? _waveSource;
private static WaveFileWriter? _waveFile; private static WaveFileWriter _writer;
private static string _audioFilePath; private static string _audioFilePath;
private static string _videoFilePath; private static string _videoFilePath;
@ -122,40 +122,27 @@ class Program
Directory.CreateDirectory(outputFolder); Directory.CreateDirectory(outputFolder);
// Set up the WasapiLoopbackCapture // Set up the WasapiLoopbackCapture
using var capture = new WasapiLoopbackCapture(); _waveSource = new WasapiLoopbackCapture();
var writer = new WaveFileWriter(_audioFilePath, capture.WaveFormat); _writer = new WaveFileWriter(_audioFilePath, _waveSource.WaveFormat);
// Handle the DataAvailable event // Handle the DataAvailable event
capture.DataAvailable += (sender, args) => _waveSource.DataAvailable += (sender, args) =>
{ {
writer.Write(args.Buffer, 0, args.BytesRecorded); _writer.Write(args.Buffer, 0, args.BytesRecorded);
// Optional: Stop recording after a certain time
if (writer.Position > capture.WaveFormat.AverageBytesPerSecond * 20) // 20 seconds
{
capture.StopRecording();
}
}; };
// Handle the RecordingStopped event // Handle the RecordingStopped event
capture.RecordingStopped += (sender, args) => _waveSource.RecordingStopped += (sender, args) =>
{ {
writer.Dispose(); _writer.Dispose();
writer = null; _writer = null;
capture.Dispose(); _waveSource.Dispose();
_waveSource = null;
}; };
// Start recording // Start recording
capture.StartRecording(); _waveSource.StartRecording();
Console.WriteLine("WASAPI Audio recording started."); Console.WriteLine("WASAPI Audio recording started.");
// Wait for the recording to stop
while (capture.CaptureState != CaptureState.Stopped)
{
Thread.Sleep(500);
}
Console.WriteLine("WASAPI Audio recording stopped.");
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -178,15 +165,13 @@ class Program
_ffmpegProcess.StartInfo.FileName = "ffmpeg"; _ffmpegProcess.StartInfo.FileName = "ffmpeg";
_ffmpegProcess.StartInfo.Arguments = ffmpegArguments; _ffmpegProcess.StartInfo.Arguments = ffmpegArguments;
_ffmpegProcess.StartInfo.UseShellExecute = false; _ffmpegProcess.StartInfo.UseShellExecute = false;
_ffmpegProcess.StartInfo.RedirectStandardOutput = true; _ffmpegProcess.StartInfo.RedirectStandardOutput = false;
_ffmpegProcess.StartInfo.RedirectStandardError = true; _ffmpegProcess.StartInfo.RedirectStandardError = true;
_ffmpegProcess.StartInfo.CreateNoWindow = true; _ffmpegProcess.StartInfo.CreateNoWindow = true;
_ffmpegProcess.OutputDataReceived += (_, args) => Console.WriteLine(args.Data);
_ffmpegProcess.ErrorDataReceived += (_, args) => Console.WriteLine(args.Data); _ffmpegProcess.ErrorDataReceived += (_, args) => Console.WriteLine(args.Data);
_ffmpegProcess.Start(); _ffmpegProcess.Start();
_ffmpegProcess.BeginOutputReadLine();
_ffmpegProcess.BeginErrorReadLine(); _ffmpegProcess.BeginErrorReadLine();
} }
@ -200,12 +185,20 @@ class Program
private static void StopAudioRecording() private static void StopAudioRecording()
{ {
if (_waveSource == null) return; if (_waveSource == null) return;
_waveSource.StopRecording(); _waveSource.StopRecording();
_waveSource.Dispose(); _waveSource.Dispose();
_waveSource = null; _waveSource = null;
if (_writer != null)
{
_writer.Dispose();
_writer = null;
}
Console.WriteLine("Audio recording stopped."); Console.WriteLine("Audio recording stopped.");
} }
private static void StopFfmpegRecording() private static void StopFfmpegRecording()
{ {
if (_ffmpegProcess != null && _ffmpegProcess.HasExited) return; if (_ffmpegProcess != null && _ffmpegProcess.HasExited) return;
@ -219,13 +212,14 @@ class Program
private static void CombineAudioAndVideo(string videoFilePath, string audioFilePath, string songName) private static void CombineAudioAndVideo(string videoFilePath, string audioFilePath, string songName)
{ {
var combinedOutputFilePath = $"{_ffmpegFolderPath}\\{songName}_combined_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.mkv"; var combinedOutputFilePath = $"{_ffmpegFolderPath}\\{songName}_combined_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.mkv";
var ffmpegArgs = $"-y -i \"{videoFilePath}\" -i \"{audioFilePath}\" -c:v copy -c:a aac -strict experimental \"{combinedOutputFilePath}\""; var ffmpegArgs =
$"-y -i \"{videoFilePath}\" -i \"{audioFilePath}\" -c:v copy -c:a aac -strict experimental \"{combinedOutputFilePath}\"";
var processInfo = new ProcessStartInfo var processInfo = new ProcessStartInfo
{ {
FileName = "ffmpeg", FileName = "ffmpeg",
Arguments = ffmpegArgs, Arguments = ffmpegArgs,
RedirectStandardOutput = true, RedirectStandardOutput = false,
RedirectStandardError = true, RedirectStandardError = true,
UseShellExecute = false, UseShellExecute = false,
CreateNoWindow = true CreateNoWindow = true
@ -238,17 +232,16 @@ class Program
return; return;
} }
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError.ReadToEnd(); var error = process.StandardError.ReadToEnd();
process.WaitForExit(); process.WaitForExit();
Console.WriteLine("FFmpeg Output: " + output);
Console.WriteLine("FFmpeg Error: " + error); Console.WriteLine("FFmpeg Error: " + error);
Console.WriteLine(File.Exists(combinedOutputFilePath) Console.WriteLine(File.Exists(combinedOutputFilePath)
? "Audio and video have been successfully combined." ? "Audio and video have been successfully combined."
: "Failed to combine audio and video. Check the logs for errors."); : "Failed to combine audio and video. Check the logs for errors.");
File.Delete(videoFilePath);
File.Delete(audioFilePath);
} }
} }

View file

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("2dxAutoClip")] [assembly: System.Reflection.AssemblyCompanyAttribute("2dxAutoClip")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+bc7244cc9260f9dc5bd7601f412243f4172cb980")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+a57c1c2131efda17587033bc71e491a80341c7a5")]
[assembly: System.Reflection.AssemblyProductAttribute("2dxAutoClip")] [assembly: System.Reflection.AssemblyProductAttribute("2dxAutoClip")]
[assembly: System.Reflection.AssemblyTitleAttribute("2dxAutoClip")] [assembly: System.Reflection.AssemblyTitleAttribute("2dxAutoClip")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View file

@ -1 +1 @@
dd66343c077a7a4fb066a7cf71b273cc5e05053f50e94596cc83ffbf03d2e4ab 3e6cb3d517cf16f5024618620589e2a0f678470e27b98cc7a5015180d535c471