I want to record audio from my soundcard(output). I've found CSCore on codeplex but I could not find any examples. Does anyone know how to use the library to record audio from my soundcard and write the record data onto the harddrive? Or does anyone know a few tutorials on that library?
Asked
Active
Viewed 3.2k times
1 Answers
43
Take a look at the CSCore.SoundIn namespace. The WasapiLoopbackCapture class is able to record directly from any output device. But keep in mind that WasapiLoopbackCapture is only available since Windows Vista.
EDIT: This code should work for you.
using CSCore;
using CSCore.SoundIn;
using CSCore.Codecs.WAV;
...
using (WasapiCapture capture = new WasapiLoopbackCapture())
{
//if nessesary, you can choose a device here
//to do so, simply set the device property of the capture to any MMDevice
//to choose a device, take a look at the sample here: http://cscore.codeplex.com/
//initialize the selected device for recording
capture.Initialize();
//create a wavewriter to write the data to
using (WaveWriter w = new WaveWriter("dump.wav", capture.WaveFormat))
{
//setup an eventhandler to receive the recorded data
capture.DataAvailable += (s, e) =>
{
//save the recorded audio
w.Write(e.Data, e.Offset, e.ByteCount);
};
//start recording
capture.Start();
Console.ReadKey();
//stop recording
capture.Stop();
}
}

Florian
- 5,918
- 3
- 47
- 86
-
in this code, where the result audio file is stored ? I mean i did not understand the path in which sound is stored. – abidinberkay Apr 06 '16 at 12:17
-
1@abidinberkay, you can change `"dump.wav"` to `@"C:\Users\youruser\folder\filename.wav"` – John Apr 07 '16 at 20:05
-
Florian can i solve my issue http://stackoverflow.com/questions/38240612/how-transfer-system-mic-audio-stream-to-attached-device-mic-audio-stream?noredirect=1#comment63908020_38240612 using your answer. – Hot Cool Stud Jul 08 '16 at 03:14
-
1I tried the above code on Windows 10 with Visual Studio 2017 and version 1.2.1.2 of CSCore, and got an exception of IAudioClient::Initialize caused an error: 0x800401fb, "Object is not registered" when I called capture.Initialize(). Is there some additional code I need here? – Greg Thatcher May 09 '18 at 21:17
-
1Why not working on Win10? DataAvailable not get called, wav file is empty. – jw_ Feb 20 '20 at 01:26
-
Would it be possible to manually feed audios into microphone? – BAKE ZQ May 16 '20 at 05:26