问题
I've been using pydub to concatenate short sound files into a larger sound file. The basic code for this looks like this:
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 states there is by default an 100ms crossfade. However, the example given below suggests that if you use a + operator to concatenate samples it uses no crossfade. I was wondering if anyone can clarify this? I've linked the API as copying the example was not readable. It's under AudioSegment(...).append().
AudioSegment(…).append()
Returns a new
AudioSegment
, created by appending anotherAudioSegment
to this one (i.e., adding it to the end), Optionally using a crossfade.AudioSegment(…).append()
is used internally when addingAudioSegment
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 ofAudioSegment
) When specified, method returns number of frames in X milliseconds of theAudioSegment
回答1:
I can confirm that concatenation using the +
operator doesn’t apply any cross fade (and in fact calls the append method with crossfade=0)
The reason for that design decision was to allow using sum() and reduce() and other similar methods to put a bunch of chunks back together without altering the total duration (due to the crossfade overlaps)
来源:https://stackoverflow.com/questions/50938152/pydub-append-clarification-of-under-the-hood-behaviour