pydub追加对幕后行为的澄清

2024-09-29 21:34:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用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中,它指出默认情况下有一个100ms的交叉淡入。但是,下面给出的示例表明,如果使用+运算符连接样本,则不使用交叉淡入。我想知道是否有人能澄清这一点?我链接了API,因为复制示例不可读。它在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

Tags: ofthetonoinfromforwav
1条回答
网友
1楼 · 发布于 2024-09-29 21:34:18

我可以确认使用+运算符的连接不会应用任何交叉淡入(实际上calls the append method with crossfade=0

设计决定的原因是允许使用sum()和reduce()以及其他类似的方法将一堆块放回一起,而不改变总持续时间(由于交叉渐变重叠)

相关问题 更多 >

    热门问题