本文介绍了C#和wave - 如何处理20ms的音频数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我遇到问题,我确定你们可以提供帮助。

我是尝试迭代Wave文件并每次处理20ms的音频数据。但是,我对字节数组和转换感到困惑,如果有人可以通过示例帮助解决这个问题,我将不胜感激...



最后我将需要处理几种类型(2通道,16Khz等),但现在我有一个 8000hz,1通道16位文件。



我正在使用 NAudio 来阅读文件。



我的尝试:



Hi all,

I'm having issues with achieving something I'm sure you guys can assist.
I'm trying to iterate over a Wave file and process 20ms of the audio data each time. However, I'm getting confused with the bytes arrays and conversions and I'd be grateful if someone can assist on this with examples...

At the end I will need to handle several types (2-channels, 16Khz etc.) but for now I have a ready 8000hz, 1 channel 16bit file.

I'm using NAudio for reading the file.

What I have tried:

/*
Pcm 8000Hz 1 channels 16 bits per sample
Extra Size: 0 Block Align: 2 Average Bytes Per Second: 16000
WaveFormat: 16 bit PCM: 8kHz 1 channels
Length: 351360 bytes: 00:00:21.9600000 
*/

WaveFileReader wHeader= new WaveFileReader(FullFileName);

byte[] data = new byte[wHeader.Length];
int read = wHeader.Read(data, 0, data.Length);  //Length = 351360
short[] shortBuffer = new short[read / 2]; // = 175680
int tBuf = 159; //Not sure this is 20ms?
short[] InpBuf = new short[tBuf]; //This is the 20ms inputBuffer I will process
int offst = 0;

Buffer.BlockCopy(data, 0, shortBuffer, 0, data.Length);


for (int i = 0; i < read; i += 2)
            {
                Buffer.BlockCopy(shortBuffer , offst, InpBuf, 0, tBuf);  //I'm taking the tBuf (=159) data from the full shortBuffer into InpBuf (with offset of 0)
                offst += tBuf+1; //advancing the next offset from 0 to 160

                Process20ms(InpBuf);  //now I can process the 20ms of data

                Array.Clear(InpBuf, 0, InpBuf.Length); //resetting the InpBuf for the next iteration
            }





无论如何,当我将 inpBuf 块与完整的 shortBuffer 进行比较时,这种方法效果不佳我可以看到inpBuf [0]到inpBufp [78]中的数据是相同的,但之后我得到00000直到inpBuf [159]。

在inpBuf [160]之后我得到完全不同的数据。



所以,我对Byte,Short,Blockcopy和样本感到困惑,虽然一旦我拥有完整的音频数据阵列,听起来很容易实现。



一旦我理解了这一点,我相信处理Stereo(2个频道)文件也会容易得多。



谢谢...



Anyways, this doesn't work well, when I'm comparing the inpBuf chunks to the full shortBuffer I can see that the data in inpBuf[0] to inpBufp[78] is identical, but afterwards i get 00000 till inpBuf[159].
After inpBuf[160] I'm getting totally different data.

So, I'm confused with the Byte, Short, Blockcopy and samples, although it sounds very easy to achieve once I have the full audio data array.

Once I understand this I believe it will be much easier to process Stereo (2 channels) files as well.

Thanks...

推荐答案



这篇关于C#和wave - 如何处理20ms的音频数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 21:44