python - 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
解决方案
我可以确认使用+
运算符的连接不会应用任何交叉淡入淡出(实际上使用 crossfade=0 调用 append 方法)
该设计决定的原因是允许使用 sum() 和 reduce() 以及其他类似方法将一堆块重新组合在一起而不改变总持续时间(由于交叉淡入淡出重叠)
推荐阅读
- c++ - 转换返回值时的 RVO
- c++ - 为什么线程清理程序抱怨这个 std::ranges::views::filter 代码?
- javascript - Promise 构造函数是否需要 resolve 函数?
- python - AttributeError:“NoneType”对象没有属性“get”(MongoDB 和 Django)
- amazon-web-services - 是否有在 AWS Fargate 上安装持久存储的 docker-compose.yml 示例?
- android - 如何获得平滑的渐变
- javascript - 如何更新 JSP 中输入更改的值?
- api - 为什么我的 WHMCS 在 error_log 中记录“hashReq”
- c++ - 使用:avio_alloc_context 从内存写入和读取
- jquery - AJAX POST 的无限滚动触发多次而不是 1