Xeoncross
Xeoncross

Reputation: 57194

How do you convert a stream of PCM audio samples to volume?

I can get an array of unsigned 32 bit ints from a WAV file.

65509
65402
65518
65520
65521
...

How can I convert these ints (or the original bytes) into a volume level for the stream?

Upvotes: 0

Views: 2813

Answers (2)

Roman Ryltsov
Roman Ryltsov

Reputation: 69687

You might want to define volume level more accurately to get to specific formula. Loudness of PCM stream question/answer gives one the popular options.

Your 32-bit INTs don't look good, by the way. With 16+ bits/sample integers, the samples are typically zero-centered (that is, you are to treat them as signed integers!) and the value sequence in your question more looks like 16-bit PCM values incorrectly converted to 32 bits ignoring sign bit.

Either way, you typically have a window of samples and then you apply a formula like referenced above to the values to aggregate into volume level. A sliding window gives you volume changes over time.

Another specification/algorithm, is mentioned by this answer: get loudness level from raw data received from microphone in DirectShow.

Upvotes: 1

Michał Zaborowski
Michał Zaborowski

Reputation: 4387

Specification says that samples can be stored as unsigned bytes, or signed integers. If you simply pass them as binary data - that does not matter. Information is information. To interpret that data as volume level you need to know that, and BitsPerSample shows that.

So there is stream of samples, but there are channels. So you need to take every second, to convert stereo to mono. Again if you just want to play - just configure player to use stereo.

To me it is quite hard to tell if ReadRawSample allows access to bytestream, or that is access to bytearray of row data for particular chunk? Then you need to know what was already processed. But if you are sure that, that are actual samples - then you know that.

Here is sample code that plays sound. Instead of reading data from input binary stream, you can fill out buffer with your wave samples. If that is not what you are planning to do - you can check if your samples sounds as you expect. It helped me a lot sometime ago.

Hope it helps you.

Upvotes: 1

Related Questions