Search code examples
audiowavvolumepcm

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?


Solution

  • 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.