Reputation: 37
I would like to draw out a little period of the waveform from a .wav file to the screen. Here is the code i created so far:
NAudio.Wave.WaveFileReader wave = new NAudio.Wave.WaveFileReader(@"C:\test.wav");
long le = wave.Length;
byte[] data = new byte[le];
wave.Read(data, 0, (int)le);
for (int i = 0; i < 100; i++)
{
System.Console.WriteLine(data[i]);
}
System.Console.ReadKey();
I just tried to get the first 100 sample of the datachunk but i dont fully understand the results. Are these numbers the amplitude values of the voice wave?
Upvotes: 2
Views: 4198
Reputation: 49482
It is likely that your WAV file is 16 bit (you can check this by looking at the WaveFileReader's WaveFormat property and looking at the BitDepth). In that case, every two bytes represents a single sample. You can use BitConverter.ToInt16
to examine the value of each sample one by one. So for example, you could modify your code to be something like this:
NAudio.Wave.WaveFileReader wave = new NAudio.Wave.WaveFileReader(@"C:\test.wav");
byte[] data = new byte[200];
int read = wave.Read(data, 0, data.Length);
for (int i = 0; i < read; i+=2)
{
System.Console.WriteLine(BitConverter.ToInt16(data,i));
}
System.Console.ReadKey();
Upvotes: 2
Reputation: 19956
They are amplitude, but that 'amplitude' changes 44100 times per second for each channel.
Try this article: http://en.wikipedia.org/wiki/Pulse-code_modulation
If it fails, just remember this. Sound is change in air pressure. Air pressure change is produced by speakers by voltage change. Voltage change is produced by rapidly activating various input levels on digital to analog converters. Input levels (numbers) are what your are getting when you read your PCM data from a file.
Upvotes: 0