Python – pydub attach – clarification of behavior under the hood

pydub attach – clarification of behavior under the hood… here is a solution to the problem.

pydub attach – clarification of behavior under the hood

I’ve been using pydub to concatenate short sound files into larger sound files. The basic code is as follows:

def permuPhrase(iterations, joins): # Builds a single phrase and does various permutations of it
sampleSet = entryMatcher()
sampleSet.inputVars()
sampleSet.match()
concat = 0
if len(sampleSet.results) != 0:
    for x in range(iterations):
        for i in range(joins):
            rand = rn.randint(0, len(sampleSet.results)- 1)
            choice = str(sampleSet[rand])
            concat += (AudioSegment.from_wav(source+choice+affix))
        numIter = str(x) # convert parent for loop to string for naming .wav files.
        concat.export(newDir+numIter+affix, format="wav") # export
else:
    print("No samples matched")

My question is this. In the API, it declares that there is a fade of 100 milliseconds by default. However, the example given below shows that if you concatenate the samples using the + operator, it does not use crossfades. I wonder if anyone can clarify this? I’ve linked the API because the copy example is not readable. It is located in the AudioSegment(…). append().

AudioSegment(…). append()

Returns a new AudioSegment, created by appending another
AudioSegment to this one (i.e., adding it to the end), Optionally
using a crossfade. AudioSegment(...). append() is used internally when
adding AudioSegment objects together with the + operator.

By default a 100ms (0.1 second) crossfade is used to eliminate pops
and crackles.

from pydub import AudioSegment
sound1 = AudioSegment.from_file("sound1.wav")
sound2 =AudioSegment.from_file("sound2.wav")

# default 100 ms crossfade
combined = sound1.append(sound2)

# 5000 ms crossfade
combined_with_5_sec_crossfade = sound1.append(sound2, crossfade=5000)

# no crossfade
no_crossfade1 = sound1.append(sound2, crossfade=0)

# no crossfade
no_crossfade2 = sound1 + sound2

Supported keyword arguments:

  • crossfade | example: 3000 | default: 100 (entire duration of AudioSegment) When specified, method returns number of frames in X
    milliseconds of the AudioSegment

Solution

I can confirm that a connection using the + operator doesn’t have any fades applied (actually calls the append method with crossfade=0 )

The reason for this design decision is to allow the use of sum() and reduce() and other similar methods to put a bunch of blocks back together without changing the total duration (due to fade overlap).

Related Problems and Solutions