I have to make a simple module where I'm getting a .wav file of at most 3 seconds in length, then I have to first extract Amplitudes & then Frequency from this file.
I'm doing this by creating a stream of the wave file, then reading & simultaneously adding the data into a List (but named it arr
) as follows:
do
{ i = wav.ReadByte(); if (i != -1) { arr.Add(i); } }
while (i != -1); Console.WriteLine(arr.Count); Console.ReadLine();
Then starting from 44th position I'm either printing this info on the console or dumping it into a text file. Right now I'm seeing integers in the range of 0-255. I know in order to get the frequency I have to convert data from time domain to frequency domain using Fast Fourier Transform (FFT). Right now my confusions are:
- What does the data in my List is representing in its current form?
- I read a lot about Sampling the signal first, but since the file is already in a digital format so should I be worried about sampling because I think it is done during ADC only. On the other hand if it is still needed to be done, then why should I be doing that as I'm already using the whole file.
- Is the data in its current form is ready to for the FFT?
- What are the things I'm missing right now for preparing the data for FFT?
Right now I required just a naive approach for getting frequencies. Later I will add more detailed extraction of data if asked to do so.
I'm researching on the theoretical parts of sound processing for a long time now. But no book or article exactly shows the preparation of the data for the FFT.
Some useful links that I found out are:
Importing a .wav file in c# and extracting the signal an array of doubles
How to get sound data sample value in c#
Storing a wav file in an array
This article supports my logic that sampling is required during ADC only
http://www.relisoft.com/science/physics/sampling.html
THE CODE BELOW WAS NOT INTENDED FOR THE PUBLIC VIEWING as right now it contains a lot of redundancies that I introduced at various points in order to get visualize the output of every step.
class MyClass
{
static void Main(string[] args)
{
string path = @"C:\wav_samples\";
Console.WriteLine("Select a song between 1 to 10");
string choice = Console.ReadLine();
string finalpath = path + choice+".wav";
//Creating a Stream for my .WAV File
FileStream wav = new FileStream(finalpath , FileMode.Open);
/*Declaring the ints & the list for the storage of bytes from the Stream for FFT */
int i;
List<double> arr = new List<double>();
///////////////////////////////////////////////////////////////////////////////////////////////
/*Reading Bytes from the .WAV File & then printing out the corresponding integer values */
do
{ i = wav.ReadByte();
if (i != -1)
{ arr.Add(i); } }
while (i != -1); Console.WriteLine(arr.Count); Console.ReadLine();
////////////////////////////////////////////////////////////////////////////////
//*Removing first 44 bytes of data as they include header information & other metadata*/
arr.RemoveRange(0, 44);
/*No method to find the size of list hence copying the data of list to a new array*/
double[] storage = new double[arr.Count];
arr.CopyTo(storage);
Console.WriteLine(storage.LongLength);
//Dumping results on screen or in a text file
Console.WriteLine("Do you want to get results on the screen, press 1 for yes or 2 for dumping this in a text_file in wav_sample folder");
int a = Convert.ToInt16(Console.ReadLine());
if (a == 1)
{
for (int limit = 0; limit < storage.LongLength; limit++ )
Console.WriteLine(arr[limit]);
}
if (a == 2)
{
System.IO.StreamWriter file = new System.IO.StreamWriter(path + "Results.txt", true);
for (int limit = 0; limit < storage.LongLength; limit++ )
{ file.WriteLine( storage[limit] ); }
}
Console.ReadLine();
}
}