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
{
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";
var deviceEnumerator = new MMDeviceEnumerator();
var defaultDevice = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Console);
if (defaultDevice == null)
// Ensure the output folder exists
string outputFolder = Path.GetDirectoryName(_audioFilePath);
Directory.CreateDirectory(outputFolder);
// Set up the WasapiLoopbackCapture
using var capture = new WasapiLoopbackCapture();
var writer = new WaveFileWriter(_audioFilePath, capture.WaveFormat);
// Handle the DataAvailable event
capture.DataAvailable += (sender, args) =>
{
Console.WriteLine("No audio device found.");
return;
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
capture.RecordingStopped += (sender, args) =>
{
writer.Dispose();
writer = null;
capture.Dispose();
};
// Start recording
capture.StartRecording();
Console.WriteLine("WASAPI Audio recording started.");
// Wait for the recording to stop
while (capture.CaptureState != CaptureState.Stopped)
{
Thread.Sleep(500);
}
_waveSource = new WasapiCapture(defaultDevice);
_waveSource.WaveFormat = new WaveFormat(48000, 2); // 48kHz, Stereo
_waveSource.DataAvailable += (_, args) =>
{
if (_waveFile == null) return;
try
{
_waveFile.Write(args.Buffer, 0, args.BytesRecorded);
_waveFile.Flush();
}
catch (Exception ex)
{
Console.WriteLine($"Error writing to file: {ex.Message}");
}
};
_waveSource.RecordingStopped += (_, _) =>
{
try
{
_waveFile?.Dispose();
_waveFile = null;
}
catch (Exception ex)
{
Console.WriteLine($"Error disposing wave file: {ex.Message}");
}
finally
{
_waveSource?.Dispose();
}
};
_waveFile = new WaveFileWriter(_audioFilePath, _waveSource.WaveFormat);
_waveSource.StartRecording();
Console.WriteLine("WASAPI Audio recording started.");
Console.WriteLine("WASAPI Audio recording stopped.");
}
catch (Exception ex)
{
@ -171,6 +164,7 @@ class Program
}
private static void StartFfmpegRecording(string songName)
{
var date = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");

View file

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

View file

@ -1 +1 @@
7635c290b171d12bdaba6f9306bc53a9b3059b9a36a579f0cf03f4dc42cb246c
dd66343c077a7a4fb066a7cf71b273cc5e05053f50e94596cc83ffbf03d2e4ab

View file

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

View file

@ -1 +1 @@
17254872235870008
17255280968129270