forked from Mercury/2dxAutoClip
Attempting to fix wasapi audio recording
This commit is contained in:
parent
bc7244cc92
commit
a57c1c2131
|
@ -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();
|
||||||
|
var writer = new WaveFileWriter(_audioFilePath, capture.WaveFormat);
|
||||||
|
|
||||||
|
// Handle the DataAvailable event
|
||||||
|
capture.DataAvailable += (sender, args) =>
|
||||||
{
|
{
|
||||||
Console.WriteLine("No audio device found.");
|
writer.Write(args.Buffer, 0, args.BytesRecorded);
|
||||||
return;
|
|
||||||
|
// 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);
|
Console.WriteLine("WASAPI Audio recording stopped.");
|
||||||
_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.");
|
|
||||||
}
|
}
|
||||||
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");
|
||||||
|
|
|
@ -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.
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
7635c290b171d12bdaba6f9306bc53a9b3059b9a36a579f0cf03f4dc42cb246c
|
dd66343c077a7a4fb066a7cf71b273cc5e05053f50e94596cc83ffbf03d2e4ab
|
||||||
|
|
|
@ -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;
|
||||||
|
|
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