Skip to content

Instantly share code, notes, and snippets.

@margusmartsepp
Last active February 6, 2016 12:33
Show Gist options
  • Save margusmartsepp/6615d19fddd150bf877e to your computer and use it in GitHub Desktop.
Save margusmartsepp/6615d19fddd150bf877e to your computer and use it in GitHub Desktop.
Record audio output device
using System;
using NAudio.Lame;
using NAudio.Wave;
namespace CaptureAudio
{
class Program
{
static LameMP3FileWriter _writer;
/// <summary>
/// Install-Package NAudio
/// Install-Package NAudio.Lame
/// </summary>
static void Main(string[] args)
{
var filename = string.Format("rec [{0:yyyyMMddHHmmssfff}].mp3", DateTime.Now);
using (var waveIn = new WasapiLoopbackCapture())
using (var writer = _writer = new LameMP3FileWriter(filename, waveIn.WaveFormat, 32))
{
try
{
waveIn.DataAvailable += Append;
waveIn.StartRecording();
Console.WriteLine("Recording. (Press any key to stop)");
while (!Console.KeyAvailable)
System.Threading.Thread.Sleep(50);
}
finally
{
writer.Flush();
}
}
}
static void Append(object sender, WaveInEventArgs e)
{
if (_writer != null)
_writer.Write(e.Buffer, 0, e.BytesRecorded);
}
}
}
using System;
using NAudio.Wave;
namespace SoundCapture
{
class Program
{
static WaveFileWriter _writer;
/// <summary>
/// Install-Package NAudio
/// Install-Package NAudio.Lame
/// </summary>
static void Main(string[] args)
{
var filename = $"rec [{DateTime.Now:yyyyMMddHHmmssfff}].wav";
using (var waveIn = new WasapiLoopbackCapture())
using (var writer = _writer = new WaveFileWriter(filename, waveIn.WaveFormat))
{
try
{
waveIn.DataAvailable += Append;
waveIn.StartRecording();
Console.WriteLine("Recording. (Press any key to stop)");
while (!Console.KeyAvailable)
System.Threading.Thread.Sleep(50);
}
finally
{
writer.Flush();
}
}
}
static void Append(object sender, WaveInEventArgs e)
{
_writer?.Write(e.Buffer, 0, e.BytesRecorded);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment