首页 > 解决方案 > pydub append - 引擎盖下行为的澄清

问题描述

我一直在使用 pydub 将短声音文件连接成更大的声音文件。基本代码如下所示:

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")

我的问题是这个。在 API 中,它指出默认情况下有 100 毫秒的淡入淡出。但是,下面给出的示例表明,如果您使用 + 运算符连接样本,则它不使用交叉淡入淡出。我想知道是否有人可以澄清这一点?我已链接 API,因为复制示例不可读。它在 AudioSegment(...).append() 下。

AudioSegment(…).append()

返回一个新的AudioSegment,通过将另一个附加 AudioSegment到这个(即,将其添加到末尾)创建,可选地使用交叉淡入淡出。与运算符一起AudioSegment(…).append()添加对象时在内部使用。AudioSegment+

默认情况下,使用 100 毫秒(0.1 秒)的淡入淡出来消除爆裂声和噼啪声。

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

支持的关键字参数

  • crossfade| 示例:3000| 默认值:(100整个持续时间AudioSegment)当指定时,方法以 X 毫秒为单位返回帧数AudioSegment

标签: pythonpython-3.xaudiopydub

解决方案


我可以确认使用+运算符的连接不会应用任何交叉淡入淡出(实际上使用 crossfade=0 调用 append 方法

该设计决定的原因是允许使用 sum() 和 reduce() 以及其他类似方法将一堆块重新组合在一起而不改变总持续时间(由于交叉淡入淡出重叠)


推荐阅读