Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# audio
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:
//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 28-Aug-12 9:08am
Edited 28-Aug-12 9:09am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
Member 9180354 at 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 at 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 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

// 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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 393
1 Sergey Alexandrovich Kryukov 232
2 Richard Deeming 180
3 Snehasish_Nandy 171
4 ChauhanAjay 170
0 Sergey Alexandrovich Kryukov 6,367
1 OriginalGriff 5,588
2 CPallini 2,473
3 Richard MacCutchan 1,627
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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