C# how to record general audio from output device

2019-06-07 08:20发布

问题:

I'm trying to record the Speaker Output to detect volume and BPM from any playing music with C# and NAudio API.

The problem is, i don't know how to do that :/

i have a sample code from http://opensebj.blogspot.de/2009/04/naudio-tutorial-5-recording-audio.html where they record simple input with less code...

waveInStream = new WaveIn(44100,2);

what does the "44100, 2" means ? does that targets the device to record from ???

how can i target speaker output ?

does anyone can help me out ? or even with another API ?

thx

回答1:

What you're probably looking for is the WasapiLoopbackCapture class, which allows you to record all the sound your computer is producing. NOTE: This works in Windows Vista/7 only!

To start recording, do this:

waveIn = new WasapiLoopbackCapture();
waveIn.DataAvailable += InputBufferToFileCallback;    
waveIn.StartRecording();

Then, every time the recording buffer is full, the InputBufferToFileCallback function will be called:

public void InputBufferToFileCallback(object sender, WaveInEventArgs e)
{
  // The recorder bytes can be found in e.Buffer
  // The number of bytes recorded can be found in e.BytesRecorded
  // Process the audio data any way you wish...
}

I think you've been put on the wrong track by the tutorial you linked, because in the current release of NAudio I don't see the new WaveIn(44100,2); constructor. NAudio probably has been modified since the tutorial was first written.

As a final note, the numbers 44100 and 2 denote the sample rate and the number of channels respectively.