2

I need to get the sample values of sound data of a WAV file so that by using those sample values i need to get the amplitude values of that sound data in every second.

Important: Is there any way to get audio data sample values using Naudio library or wmp library?

I am getting the sample values in this way:

byte[] data = File.ReadAllBytes(File_textBox.Text);
            var samples=new int[data.Length];
            int x = 0;
            for (int i = 44; i <data.Length; i += 2)
            {
                samples[x] = BitConverter.ToInt16(data, i);
                x++;
            }

But I am getting negative values more like (-326260). so is this right or wrong? I mean can a sample value be negative or not and if it is correct then what does it mean a sound or silence?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
azeem
  • 103
  • 2
  • 7

1 Answers1

1

NAudio can do this for you, it's in the library (I think it's the WaveStream or WaveReader class, or something similar). I can recommend it's use, if it's not too much overhead.

If you want to roll-your-own, and want to deal with arbitrary wave files, you'll have to read up on the WAV file format, and analyze the header yourself.

Although in general a WAV file contains 16bit samples, it doesn't have to, and depending on the exact format, they might be stored in little endian or big endian.

The header contains information about sample rate, number of channels, bits per sample, bytes per sample and such like, which allow you to do the actual math to get exactly one sample.

Willem van Rumpt
  • 6,490
  • 2
  • 32
  • 44
  • I have studied the WAV file format and come to know that after 44th byte there is sound data. but all what i need to know that if i am getting the negative values of samples, what does they means SILENCE of SOUND? because i am getting a lot of negative sample values but i have a remarkable sound in my WAV file – azeem May 07 '12 at 07:26
  • Negative values are to be expected. You're reading shorts, so it's completely normal to have negative values. 16 bit samples are stored as signed values. A zero would mean no sound. Also note that your samples array are ints (which shouldn't be a problem), and that its size is longer than necessary ((data.Length - 44) / 2 would suffice). – Willem van Rumpt May 07 '12 at 08:00
  • no i am not reading the shorts. as it can be seen by code i am just converting the byte to INT16. but i am getting the negative values. It is happening the in Sample Array suppose on the index 1 the value is -32465 and on the index 2 is 32465. so i am confused that what is this... which one is the sound and which one is the silence OR which one is the louder sound and which one is the slower sound. – azeem May 07 '12 at 08:16
  • Euh...a short IS a Int16. You're looping through the array (that's what I meant by "reading") in blocks of two. BitConverter.ToInt16() will return a Int16, is a short, is a 16bit signed integer. This will work ASSUMING it's a 16bit WAV file. Without more information about the WAV file (number of channels, bytes per sample, etc) it's impossible to tell more. – Willem van Rumpt May 07 '12 at 08:31