1

I would like to generate workout mp3 file with music in the background and instructions at some timing (ex. "push harder", "do another repetition !")

I generate the instruction with pico2wave and assemble them with pydub.

I do this :

for timing,phrase in phrases.items():
        fileToAdd = pydub.AudioSegment.from_file(rep+"/"+str(timing)+".wav")
        finalFile = finalFile.fade(to_gain=-35, start=(timing*1000)-500, duration=500) # on diminue la chanson, une demi seconde avant
        finalFile = finalFile.fade(to_gain=+35, start=(timing*1000)+len(fileToAdd), duration=500) 
        fichierFinal = fichierFinal.overlay(fileToAdd,position=timing*1000) 

The result file has a VERY bad quality. I try to remove the "fade effect" and the quality is good (but I don't hear very well the "instructions")

How can I change this ? Can I easily make a fade out and fade in effect ?

Thank you,

Regards,

Axel

axel584
  • 235
  • 2
  • 9

1 Answers1

2

I think the problem is that you're fading the audio down and then boosting it again (so you're losing 35dB of dynamic range each time you attenuate and then boost).

I think a better solution would be to split the audio and only reduce the sections you need (without any boosting operations).

The operation you're doing here is sometimes called "ducking" so I'll use that name below:

def duck(sound, position, duration, gain=-15.0, fade_duration=500):
    """
    sound - an AudioSegment object
    position - how many milliseconds into the sound the duck should 
        begin (this is where overlaid audio could begin, the fade down
        will happen before this point)
    duration - how long should the sound stay quiet (milliseconds)
    gain - how much quieter should the sound get (in dB)
    fade_duration - how long sound the fades last (in milliseconds)
    """

    # this part is from the beginning until the end of the ducked section
    first_part = sound[:position+duration]
    first_part = first_part.fade(to_gain=gain, end=position, duration=fade_duration)

    # this part begins where the fade_up happens (will just fade in)
    second_part = sound[position+duration:]
    second_part = second_part.fade(from_gain=gain, start=0, duration=fade_duration)

    return first_part + second_part


for timing, phrase in phrases.items():
    fileToAdd = pydub.AudioSegment.from_file(rep+"/"+str(timing)+".wav")

    finalFile = duck(finalFile, position=timing*1000, duration=len(fileToAdd))

    finalFile = finalFile.overlay(fileToAdd, position=timing*1000) 

After some testing, 35dB is probably more than you want. 15dB sounded pretty good to my ears :)

Jiaaro
  • 74,485
  • 42
  • 169
  • 190