问题
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?
回答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.
来源:https://stackoverflow.com/questions/10477697/how-to-get-sound-data-sample-value-in-c-sharp