1

I have multiple wave files, some are small volume and others are large volume.

I want to "Normalize" sound amplitude.

(Like the "Normalize" function that Some audio sequencer application has. Making volume bigger to the peak comes to the 0db.)

For example librosa library, it has librosa.util.normalize, however I am not sure it is what I meant.

I want to align the volume of audio, is there any practice?

whitebear
  • 11,200
  • 24
  • 114
  • 237

1 Answers1

2

Find the maximum peak (positive or negative, so use abs) in any channel. For instance in a 16 bits file, imagine you find 25000. Compute the ratio of that value relatively to the maximum in the signed 16 bit range and invert it :

ratio = 32767.0 / 25000 #(equivalent to 1 / (25000 / 32767.0))

Now iterate all samples and multiply them by the inverted ratio so that 25000 becomes 32767 :

for(sample in samples):
    sample = round(sample * ratio)

This operation is called "normalization" or "optimization" depending on the software.

dspr
  • 2,383
  • 2
  • 15
  • 19
  • Thank you very much!!. Bit , How can I find the maximum peak from samples at first? `(y,sr) = librosa.load(out_file,sr=44100,mono=False)` `print(y[1])` `[-3.0517578e-05 -3.0517578e-05 0.0000000e+00 ... -1.0467529e-02 -1.0253906e-02 -1.0070801e-02]`... – whitebear Feb 05 '21 at 16:44
  • Librosa seems to work with float32, so the sample values should vary between -1.0 and 1.0. Apart of that, the principle is the same : you have to maximize the absolute value which would result in a value between 0 and 1. The inverted ratio is then 1 divided by that value. Note also that, if the max peak is 1.0, there is nothing you can do since the wave is already normalized. – dspr Feb 05 '21 at 17:10
  • 1
    THank you very much. It works. `(y,sr) = librosa.load(out_file,sr=44100,mono=False)` `max_peak = np.max(np.abs(y))` `ratio = 1 / max_peak` `y = y * ratio` – whitebear Feb 05 '21 at 17:31