Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
/*
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.

Thanks...
Posted
Updated 10-Nov-16 20:52pm
v2

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.
 
Share this answer
 
Comments
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
I think Buffer.BlockCopy copies bytes, not shorts. Since you use 16bit samples, you have to copy twice as much bytes.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900