C# recording audio from soundcard [closed]
Asked Answered
I

1

25

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?

Incubate answered 15/9, 2013 at 12:11 Comment(0)
B
42

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();
    }
}
Bliss answered 15/9, 2013 at 12:14 Comment(6)
in this code, where the result audio file is stored ? I mean i did not understand the path in which sound is stored.Geodynamics
@abidinberkay, you can change "dump.wav" to @"C:\Users\youruser\folder\filename.wav"Psychoneurosis
Florian can i solve my issue #38241112 using your answer.Macromolecule
I 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?Jewel
Why not working on Win10? DataAvailable not get called, wav file is empty.Frodeen
Would it be possible to manually feed audios into microphone?Outbalance

© 2022 - 2024 — McMap. All rights reserved.