Click here to Skip to main content
14,218,294 members
Rate this:
Please Sign up or sign in to vote.
See more:
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

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.

Updated 10-Nov-16 20:52pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

You are using Buffer.BlockCopy to copy from data to sampleBuffer, and then from shortBuffer to InpBuf. But you never put any data into shortBuffer. Your code is quite confusing with all those different buffers. All you really need is one in which to read the data. You can then process specific byte groups directly in that buffer.
Member 1898210 10-Nov-16 7:20am
Thanks - sorry for the confusion, my bad --> shortBuffer is sampleBuffer, I just renamed that to be more clear naming...
Richard MacCutchan 10-Nov-16 7:29am
Well it is still confusing, you need to edit your question and use the same name. You probably also need to use your debugger to check the actual data that you are copying, as it is impossible for anyone here to guess what is happening.
Member 1898210 10-Nov-16 7:22am
Anyways I know it's quote confusing so just for the sake of simplicity... :

data - original Byte[] with audio data
shortBuffer - short[] with audio data
inpBuff - chunk of audio data that should contain 20ms only
Rate this:
Please Sign up or sign in to vote.

Solution 2

I think Buffer.BlockCopy copies bytes, not shorts. Since you use 16bit samples, you have to copy twice as much bytes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100