Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am busy with my project and came upon a obstacle I cannot overcome. I have imported an audio file, at this moment only using .wav files, into a byte array. After that I loop through to find the data chunk in the file. My problem now is to convert that byte array to a double array before it is sent to be converted into a Complex array for the final DFT conversion where the complex array is converted to an array of frequencies.

I have gone through multiple web pages and none of the suggested code snippets works on my code. I either get an array of zeros or an array of NaN.

The following code identifies the data chunk:
C#
//http://stackoverflow.com/questions/8754111/how-to-read-the-data-in-a-wav-file-to-an-array
            //Get audio file into array
            byte[] wav = File.ReadAllBytes(sPath);

            //Determine if file is mono = 1 or stereo = 2
            int iChannel = wav[22];

            //Bytes Per Sample = 34 Position
            int iBitsPerSample = wav[34]; 

            //Position of first sub chunk ( ID from 12 to 16)
            int iPos = 12;

            while (!(wav[iPos] == 100 && wav[iPos + 1] == 97 && wav[iPos + 2] == 116 && wav[iPos + 3] == 97))
            {
                iPos += 4;
                int iChunkSize = wav[iPos] + wav[iPos + 1] * 256 + wav[iPos + 2] * 65536 + wav[iPos + 3] * 16777216;
                iPos += 4 + iChunkSize;
            }
            iPos += 8;

            //Position is now at start of actual sound data
            int iSample = (wav.Length - iPos) / 2;
            if (iChannel == 2)
                iSample /= 2;


Following the above code, I need to convert the byte array to a double array. As far as I know the double array needs to be in wave format.

For the complex and DFT conversion I am using the AForge.Math library in conjuction with the Hamming Windowing Function to cancel out any noise in the data.

Steps of the code:
1. Import wav file to byte array
2. Convert byte array to double array
3. Convert double array to complex array
4. Use Hamming function to cancel out noise from the complex array
5. Convert complex array to frequencies with the use of DFT
6. End product - array with frequencies.

The frequency array goes through more loops but that part of the code is working. My goal is to import an audio file and export the frequencies of the notes saved in the audio file.

Can anyone help? Any advice would be appreciated.
Thank you in advance.
Posted
Updated 28-Aug-12 9:09am
v2

Usually the input of a WAV file is represented as Octave band values for music etc. So your reprensentation should depend on what you are measuring. Hamming window is a lowpass filter that would smooth the signal out, and is in most cases used to create a continus signal insted of a descrete signal, noise filters are generally used to remove all the data that is outside the frequencies of interest to get a better SNR.

You should also use a FFT algorithem, and not a DFT algortihm, as FFT is much faster. There are lots of examples on this very site:
http://www.codeproject.com/search.aspx?q=FFT&sbo=kw[^]
 
Share this answer
 
Comments
Janita Potgieter 28-Aug-12 16:06pm    
Thank you for your quick response. I have started looking through the link you have posted. My big problem at the moment is converting the byte array to a double array. I have tried multiple options, but none of them seem to be working correctly. Do you have any advice? I have tried BitConverter and some ohter code I found on the internet but the final output is not what it should be. This is my biggest problem at the moment.

I will try to use the FFT, I only used DFT as it does not have a size limitation for the array you send to the class and its methods.
Kenneth Haugland 28-Aug-12 16:25pm    
You could use the ByteArray directly in your calculations, or transfer it to a double, it dosent really matter. A bitconverter is definatly not needed. The size of your DFT or FFT determins how good resolution you would get in the FFT. The higher the size the better )or rather closer the resulting frequencies would get. It seems you should start reading some basic articles about DFT and signal prossesing in general. You seem to mix up definitions and the needed input. It is really difficult to do a DFT if you dont understand how it works, let alone do Digital Signal Prossessing on the result if you dont understand what Im talking about. So do you?
Janita Potgieter 28-Aug-12 16:31pm    
I have done some research. I understand FFT, I read a pdf which explained it in an easy way. I have never worked with signal prosessing, thus the struggling. I'm still learning as I go.
Kenneth Haugland 28-Aug-12 16:41pm    
I have tried this one before:
http://gerrybeauregard.wordpress.com/2011/04/01/an-fft-in-c/
Its pretty ok, and its in a high level languange that is understandable, and no unneccesary information.
// convert two bytes to one double in the range -1 to 1
static double bytesToDouble(byte firstByte, byte secondByte) {
// convert two bytes to one short (little endian)
short s = (secondByte << 8) | firstByte;
// convert to range from -1 to (just below) 1
return s / 32768.0;
}

//while calling in your actual function, you can use:
data[i] = bytesToDouble(wav[pos], wav[pos + 1]); // data is of type double
 
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