1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| using NAudio.Wave; using System; using System.IO; using System.Threading;
namespace AegisubHelper { public static class AudioHelper { private static WasapiLoopbackCapture capture = null; private static readonly string outputFolder = "NAudio"; public static readonly string outputFilePath = Path.Combine(outputFolder, "recorded.wav"); private static WaveFileWriter writer = null; public static void StartRecord() { Directory.CreateDirectory(outputFolder); File.Delete(outputFilePath); capture = new WasapiLoopbackCapture(); writer = new WaveFileWriter(outputFilePath, capture.WaveFormat = new(44000, 32, 2)); capture.DataAvailable += (s, a) => { writer.Write(a.Buffer, 0, a.BytesRecorded); }; capture.StartRecording(); } public static void StopRecord() { if (capture != null) { capture.StopRecording(); writer.Dispose(); writer = null; capture.Dispose(); } } } }
|