从naudio.Wave.WaveIn到Stream? [英] From naudio.Wave.WaveIn to Stream ?

查看:243
本文介绍了从naudio.Wave.WaveIn到Stream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我复制了这段代码,但我听不懂,但是我知道它完成后会做什么(输出-sourceStream)...

I copied this code and i don't understand it but i know what it does when it's finished (output -- sourceStream) ...

 NAudio.Wave.WaveIn sourceStream = null;
 NAudio.Wave.DirectSoundOut waveOut = null;
  NAudio.Wave.WaveFileWriter waveWriter = null;

        sourceStream = new NAudio.Wave.WaveIn();
    sourceStream.DeviceNumber = 2;
    sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(16000, NAudio.Wave.WaveIn.GetCapabilities(2).Channels);

    NAudio.Wave.WaveInProvider waveIn = new NAudio.Wave.WaveInProvider(sourceStream);

    waveOut = new NAudio.Wave.DirectSoundOut();
    waveOut.Init(waveIn);
    //sourceStream.DataAvailable.
    sourceStream.StartRecording();
    waveOut.Play();
    sourceStream.StopRecording();

我知道这段代码记录了所选麦克风的声音,并为其提供了输出(sourceStream)

What I know that this code records sound from a selected microphone and it gives an output for it (sourceStream)

所以我需要做的第一件事->如何从此代码中获取流(像代替WaveIn一样[从WaveIn转换为Stream])?

So the first thing I need is -- > How can i get a stream out from this code (Like instead of WaveIn a Stream [Convert from WaveIn to Stream]) ?

你们能解释一下代码吗?我尝试在NAudio网站上进行解释,但我听不懂->我是音频和流媒体的初学者...

And can you guys please Explain the code ... I tried the NAudio website for the explanation but i didn't understand --> I'm a beginner for audio and Streams...

推荐答案

您需要捕获WaveIn提供的DataAvailable event,类似这样

You need to capture the DataAvailable event provided by WaveIn, something like this

//Initialization of the event handler for event DataAvailable
sourceStream.DataAvailable += new EventHandler<WaveInEventArgs>(sourceStream_DataAvailable); 

并提供event handler,所需的数据位于event handlerWaveInEventArgs输入参数中.请注意,音频的数据类型为short,所以我通常会这样做,

And provide the event handler, the data that you need is in the WaveInEventArgs input argument of the event handler. Note that the data type of the audio is short so I would normally do like this,

private void sourceStream_DataAvailable(object sender, WaveInEventArgs e) {
  if (sourceStream == null)
    return;
  try {
    short[] audioData = new short[e.Buffer.Length / 2]; //this is your data! by default is in the short format
    Buffer.BlockCopy(e.Buffer, 0, audioData, 0, e.Buffer.Length);
    float[] audioFloat = Array.ConvertAll(audioData, x => (float)x); //I typically like to convert it to float for graphical purpose
    //Do something with audioData (short) or audioFloat (float)          
  } catch (Exception exc) { //if some happens along the way...
  }
}

但是,如果要转换为byte[]格式的文件,则应直接使用e.Buffer或将其复制到byte[]的较短名称中,以更合适的方式.对于我来说,复制到byte[]数组然后在其他地方处理它通常更合适,因为WaveIn流仍然存在.

However, if you want to convert get it in the byte[] format, then you should simply use e.Buffer directly or copying it to some short of byte[] whichever is more appropriate. As for me copying to byte[] array and then process it somewhere else is often more appropriate because the WaveIn stream is alive.

byte[] bytes = new short[e.Buffer.Length]; //your local byte[]
byteList.AddRange(bytes); //your global var, to be processed by timer or some other methods, I prefer to use List or Queue of byte[] Array

然后将您的byteList转换为Stream,只需将MemoryStreambyte[]

Then to convert your byteList to Stream, you can simply use MemoryStream with input of byte[]

Stream stream = new MemoryStream(byteList.ToArray()); //here is your stream!

这篇关于从naudio.Wave.WaveIn到Stream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆