NAudio WaveIn.DataAvailable 事件:分离通道
Posted
技术标签:
【中文标题】NAudio WaveIn.DataAvailable 事件:分离通道【英文标题】:NAudio WaveIn.DataAvailable Event: Separating Channels 【发布时间】:2013-11-18 22:01:03 【问题描述】:假设一个立体声 WaveIn
的样本通过 WaveIn.DataAvailable (ByVal sender As Object, ByVal e As WaveInEventArgs)
事件被截获。如何从e.Buffer
中提取特定通道(即右/左)的样本?
【问题讨论】:
【参考方案1】:样本交错存储。因此,如果您正在录制 16 位样本,前两个字节是左声道样本,后两个字节是右声道样本,然后是另一个左声道样本,依此类推。
【讨论】:
【参考方案2】:3 年后回复,以防其他人试图从 WaveIn
读取左右声道。我将Microphone Demo,特别是FormMicrophone.cs 改编为以下内容:
private void cbDevices_SelectedIndexChanged(object sender, EventArgs e)
wvin?.Dispose();
wvin = new NAudio.Wave.WaveInEvent();
wvin.DeviceNumber = cbDevices.SelectedIndex;
//Changed this line to have 2 instead of 1 channel
wvin.WaveFormat = new NAudio.Wave.WaveFormat(rate: SAMPLE_RATE, bits: 16, channels: 2);
wvin.DataAvailable += OnDataAvailable;
wvin.BufferMilliseconds = 20;
wvin.StartRecording();
double[] lastBuffer;
double[] lastBufferRight;
private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args)
int bytesPerSample = wvin.WaveFormat.BitsPerSample / 8;
int samplesRecorded = args.BytesRecorded / bytesPerSample;
if (lastBuffer is null || lastBuffer.Length != samplesRecorded)
lastBuffer = new double[samplesRecorded/2];
lastBufferRight = new double[samplesRecorded/2];
int indexBuffer = 0;
for (int indexSample = 0; indexSample < samplesRecorded; indexSample+=2)
lastBuffer[indexBuffer] = BitConverter.ToInt16(args.Buffer, indexSample * bytesPerSample);
lastBufferRight[indexBuffer] = BitConverter.ToInt16(args.Buffer, (indexSample+1) * bytesPerSample);
++indexBuffer;
如果你想试试这个tone generator 非常适合检查左右通道上的单个正弦波
【讨论】:
以上是关于NAudio WaveIn.DataAvailable 事件:分离通道的主要内容,如果未能解决你的问题,请参考以下文章