Reputation: 636
I am writing a a rangefinder program at the moment (think sonar). So I fire a pulse of sound and measure how long it takes to come back. Currently, I am simply opening up the sound file and manually measuring the time to rise. I have linked an image for clarity.
I'm wondering if there are any .NET libraries to do this sort of thing. I have already looked at NAudio but didn't find anything useful.
It looks like I might have to do this manually, in which case, how do I best go about converting a WAV into a series of raw values (such as each point on the image)? If I can do that I can write my own algorithm.
Upvotes: 0
Views: 342
Reputation: 6151
You can do it by reading the file into a byte array, extracting some info about it and then searching for the desired sample, which in your case I believe it's the first sample above some threshold.
The first stage is easy, like in this question.
For the second stage, you have to know a bit about wav
file's structure. As can be seen here -
it contains info about the number of channels (in your case it looks like you have only one channel), sampling rate and number of bits per sample.
After knowing these parameters, you can start to read the audio's data - if it's 8 bits per sample, then every sample is one byte. If it's 16, you'll have to read 2 bytes, and calculate the sample's value, since it is written in little endian format.
When you find that sample number X is the one you're looking for, divide that number by the sampling rate and you'll get the exact time of the sample since the file's start.
The image was taken from here.
Upvotes: 2