Attempting to fix wasapi audio recording

This commit is contained in:
Mercurio 2024-09-05 12:34:41 +02:00
parent bc7244cc92
commit a57c1c2131
10 changed files with 55 additions and 60 deletions

View file

@ -114,55 +114,48 @@ class Program
{ {
try try
{ {
var date = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); string date = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
_audioFilePath = $"{_ffmpegFolderPath}\\{songName}_{date}.wav"; _audioFilePath = $"{_ffmpegFolderPath}\\{songName}_{date}.wav";
var deviceEnumerator = new MMDeviceEnumerator(); // Ensure the output folder exists
var defaultDevice = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Console); string outputFolder = Path.GetDirectoryName(_audioFilePath);
Directory.CreateDirectory(outputFolder);
if (defaultDevice == null) // Set up the WasapiLoopbackCapture
{ using var capture = new WasapiLoopbackCapture();
Console.WriteLine("No audio device found."); var writer = new WaveFileWriter(_audioFilePath, capture.WaveFormat);
return;
}
_waveSource = new WasapiCapture(defaultDevice); // Handle the DataAvailable event
_waveSource.WaveFormat = new WaveFormat(48000, 2); // 48kHz, Stereo capture.DataAvailable += (sender, args) =>
{
writer.Write(args.Buffer, 0, args.BytesRecorded);
_waveSource.DataAvailable += (_, args) => // Optional: Stop recording after a certain time
if (writer.Position > capture.WaveFormat.AverageBytesPerSecond * 20) // 20 seconds
{ {
if (_waveFile == null) return; capture.StopRecording();
try
{
_waveFile.Write(args.Buffer, 0, args.BytesRecorded);
_waveFile.Flush();
}
catch (Exception ex)
{
Console.WriteLine($"Error writing to file: {ex.Message}");
} }
}; };
_waveSource.RecordingStopped += (_, _) => // Handle the RecordingStopped event
capture.RecordingStopped += (sender, args) =>
{ {
try writer.Dispose();
{ writer = null;
_waveFile?.Dispose(); capture.Dispose();
_waveFile = null;
}
catch (Exception ex)
{
Console.WriteLine($"Error disposing wave file: {ex.Message}");
}
finally
{
_waveSource?.Dispose();
}
}; };
_waveFile = new WaveFileWriter(_audioFilePath, _waveSource.WaveFormat); // Start recording
_waveSource.StartRecording(); capture.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)
{ {
@ -171,6 +164,7 @@ class Program
} }
private static void StartFfmpegRecording(string songName) private static void StartFfmpegRecording(string songName)
{ {
var date = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); var date = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");

View file

@ -7,15 +7,16 @@
// </auto-generated> // </auto-generated>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
using System;
using System.Reflection; using System.Reflection;
[assembly: AssemblyCompany("2dxAutoClip")] [assembly: System.Reflection.AssemblyCompanyAttribute("2dxAutoClip")]
[assembly: AssemblyConfiguration("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+bc7244cc9260f9dc5bd7601f412243f4172cb980")]
[assembly: AssemblyProduct("2dxAutoClip")] [assembly: System.Reflection.AssemblyProductAttribute("2dxAutoClip")]
[assembly: AssemblyTitle("2dxAutoClip")] [assembly: System.Reflection.AssemblyTitleAttribute("2dxAutoClip")]
[assembly: AssemblyVersion("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class. // Generated by the MSBuild WriteCodeFragment class.

View file

@ -1 +1 @@
7635c290b171d12bdaba6f9306bc53a9b3059b9a36a579f0cf03f4dc42cb246c dd66343c077a7a4fb066a7cf71b273cc5e05053f50e94596cc83ffbf03d2e4ab

View file

@ -1,8 +1,8 @@
// <auto-generated/> // <auto-generated/>
global using System; global using global::System;
global using System.Collections.Generic; global using global::System.Collections.Generic;
global using System.IO; global using global::System.IO;
global using System.Linq; global using global::System.Linq;
global using System.Net.Http; global using global::System.Net.Http;
global using System.Threading; global using global::System.Threading;
global using System.Threading.Tasks; global using global::System.Threading.Tasks;

View file

@ -1 +1 @@
17254872235870008 17255280968129270