C# how to record general audio from output device ( speaker ) with NAudio API

拟墨画扇 提交于 2019-12-10 11:46:55

问题


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.




回答2:


As said in the accepted answer, the code on the tutorial you are following is from an older version of NAudio, in that version the sample rate and channel count needed to be passed as parameters to WaveIn constructor.

As the answer suggest you to use the WasapiLoopbackCapture, maybe this can complement it.

The newer implementations of NAudio doesn't takes the sample rate (44100) and number of channels (2) as parameters for WaveIn constructor. You can simply call the WaveIn constructor without parameters:

waveInStream = new WaveIn();


来源:https://stackoverflow.com/questions/11828437/c-sharp-how-to-record-general-audio-from-output-device-speaker-with-naudio-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!