简体   繁体   中英

C++ - normalize volume level of a wav-file

Any idea how to normalize a wav-file so the volume level become maximal by using c++? What's the formula? I have googled whole afternoon but found no answer. Any help pls!

Suppose NewMaxVal is the new maximum value in the sound stream you want. The simplest solution is to find the current maximum value (CurMaxVal) in the sound stream and then multiply all values by NewMaxVal/CurMaxVal. A more advanced form is to make use of a dynamic compressor .

In a wav file the values are represented in the form of 0-1 (or -1 to 1) depending on your signed/unsigned representation. Every word (two bytes) you read is directly the sample - so there is no other processing required. In case of 8 bit representation 1 will be 256 and in case of 16 bit representation, 1 will be 65536.

If you multiply all samples by a number, the volume will increase. It is that simple! In order to convert the volume to the maximum possible level run through the entire file to find out maximum value. and then multiply the numbers such that the highest value will reach the max value without overflowing.

You can use libsndfile to process all your samples. Wav files are terribly simple to use but there are many combinations that you need to be aware of.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM