Attempting to fix wasapi audio recording
This commit is contained in:
parent
bc7244cc92
commit
a57c1c2131
|
@ -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);
|
||||
// Ensure the output folder exists
|
||||
string outputFolder = Path.GetDirectoryName(_audioFilePath);
|
||||
Directory.CreateDirectory(outputFolder);
|
||||
|
||||
if (defaultDevice == null)
|
||||
{
|
||||
Console.WriteLine("No audio device found.");
|
||||
return;
|
||||
}
|
||||
// Set up the WasapiLoopbackCapture
|
||||
using var capture = new WasapiLoopbackCapture();
|
||||
var writer = new WaveFileWriter(_audioFilePath, capture.WaveFormat);
|
||||
|
||||
_waveSource = new WasapiCapture(defaultDevice);
|
||||
_waveSource.WaveFormat = new WaveFormat(48000, 2); // 48kHz, Stereo
|
||||
// Handle the DataAvailable event
|
||||
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;
|
||||
try
|
||||
{
|
||||
_waveFile.Write(args.Buffer, 0, args.BytesRecorded);
|
||||
_waveFile.Flush();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error writing to file: {ex.Message}");
|
||||
capture.StopRecording();
|
||||
}
|
||||
};
|
||||
|
||||
_waveSource.RecordingStopped += (_, _) =>
|
||||
// Handle the RecordingStopped event
|
||||
capture.RecordingStopped += (sender, args) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
_waveFile?.Dispose();
|
||||
_waveFile = null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error disposing wave file: {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_waveSource?.Dispose();
|
||||
}
|
||||
writer.Dispose();
|
||||
writer = null;
|
||||
capture.Dispose();
|
||||
};
|
||||
|
||||
_waveFile = new WaveFileWriter(_audioFilePath, _waveSource.WaveFormat);
|
||||
_waveSource.StartRecording();
|
||||
// Start recording
|
||||
capture.StartRecording();
|
||||
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)
|
||||
{
|
||||
|
@ -171,6 +164,7 @@ class Program
|
|||
}
|
||||
|
||||
|
||||
|
||||
private static void StartFfmpegRecording(string songName)
|
||||
{
|
||||
var date = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
7635c290b171d12bdaba6f9306bc53a9b3059b9a36a579f0cf03f4dc42cb246c
|
||||
dd66343c077a7a4fb066a7cf71b273cc5e05053f50e94596cc83ffbf03d2e4ab
|
||||
|
|
|
@ -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;
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1 +1 @@
|
|||
17254872235870008
|
||||
17255280968129270
|
Loading…
Reference in a new issue