whitebear
whitebear

Reputation: 12457

Is there a way to make fade out by librosa or another on python

For now I use librosa module to mix audio like this

    audio1 = "a.wav"
    audio2 = "b.wav"
    y1, sample_rate1 = librosa.load(audio1, mono=True,sr=22050,duration=50)
    y2, sample_rate2 = librosa.load(audio2, mono=True,sr=22050,duration=50)


    sf.write('total.wav', (y1 + y2 * 1.5)/2, 22050, 'PCM_16') #adjust volume by * 1.5

I succesfuly get the total.wav but I want to make the fadeout for this audio.

How can I make it??

Upvotes: 1

Views: 4559

Answers (2)

shoegazerstella
shoegazerstella

Reputation: 69

Use pydub, it has all the functionalities for mixing, crossfading, fade in/out and more.

Upvotes: 0

Jon Nordby
Jon Nordby

Reputation: 6289

Fades are done by multiplying your signal with a fade curve (for the section you want to apply the fade).

Here is an executable example code applying a linear fade out (fades to zero). The fade code just uses numpy. The example also requires librosa version 0.8.0+ and soundfile 0.9.0+ for input/output.

import librosa
import numpy
import soundfile


def apply_fadeout(audio, sr, duration=3.0):
    # convert to audio indices (samples)
    length = int(duration*sr)
    end = audio.shape[0]
    start = end - length

    # compute fade out curve
    # linear fade
    fade_curve = numpy.linspace(1.0, 0.0, length)

    # apply the curve
    audio[start:end] = audio[start:end] * fade_curve

path = librosa.ex('brahms')
orig, sr = librosa.load(path, duration=5.0)
out = orig.copy()
apply_fadeout(out, sr, duration=2.0)

soundfile.write('original.wav', orig, samplerate=sr)
soundfile.write('faded.wav', out, samplerate=sr)

The audio files when loaded in Audacity looks like this:

enter image description here

There are many other fade curves, like logaritmic, exponential, cosine and even arbitrary curves specified using Beziers. Maybe the popular for a fade out is the S curve. This can be implemented with a sigmoid function, left as an exercise for the reader.

Upvotes: 4

Related Questions