Search code examples
pythonaudiopydub

Mix audio files make clipping on python


I have some audio files.

I mixed audio files.

for idx,f in enumerate(files):
    if idx == 0:
        sound = pydub.AudioSegment.from_file(f)
    else: 
        temp = pydub.AudioSegment.from_file(f)
        sound = sound.overlay(temp, position=0)
  
    sound.export("totakmix.wav",format="wav")
    

Each audio file is not clipping.

However, mix file is clipping.

Is there any way to prevent this??


Solution

  • The easiest thing you can do to prevent clipping while using overlay is to apply negative gain correction with gain_during_overlay like this:

    sound = sound.overlay(temp, position=0, gain_during_overlay=-3)
    

    to changes the audio by 3 dB while overlaying audio. Why 3 dB? It translates to roughly twice power gain, so if your original audio was not clipping, the end result should not either.