以编程方式合并 wav 文件 - Python

Combining wav files programmatically - Python

我希望以各种方式组合 10 个音频样本(格式可能是 wav,但这可以更改为任何格式,因为它们将被预先录制)。

from pydub import AudioSegment
sounds = []
sound1 = AudioSegment.from_wav("Dropbox/PIREAD/1.wav")
sound2 = AudioSegment.from_wav("Dropbox/PIREAD/2.wav")
sound3 = AudioSegment.from_wav("Dropbox/PIREAD/3.wav")
sound4 = AudioSegment.from_wav("Dropbox/PIREAD/4.wav")
sound5 = AudioSegment.from_wav("Dropbox/PIREAD/5.wav")
sound6 = AudioSegment.from_wav("Dropbox/PIREAD/6.wav")
sound7 = AudioSegment.from_wav("Dropbox/PIREAD/7.wav")
sound8 = AudioSegment.from_wav("Dropbox/PIREAD/8.wav")
sound9 = AudioSegment.from_wav("Dropbox/PIREAD/9.wav")
sound0 = AudioSegment.from_wav("Dropbox/PIREAD/0.wav")

sounds=[sound1,sound2,sound3,sound4,sound5,sound6,sound7,sound8,sound9,sound0]

combined_sounds = AudioSegment.empty()

for x in range(10):
    for y in range(10):
        combined_sounds += sounds[y]
    
combined_sounds.export("Dropbox/PIREAD/joinedFile.wav", format="wav")

这实际上是我读取数字 0-9 并将它们组装成一个完整的 wav 文件。

它可以工作 - 但一旦循环扩展 x=100、x=1000 就很慢。

问:我怎样才能加快速度?

数字的实际顺序将从文本中读取$ - 例如“354224848179261915075”恰好是第 100 个斐波那契数。

干杯 格伦

我认为它很慢,因为当您遍历 x 时,您重复操作(遍历 y),这些操作可以在遍历 x 之前计算,然后组装。

我调查了 AudioSegment 并找到了对您可能有用的方法,即 from_mono_audiosegments 但它仅限于单声道声音,您需要测试它是否比 += 快在时间上比较这些选项,即

import time
from pydub import AudioSegment
sounds = []
sound1 = AudioSegment.from_wav("Dropbox/PIREAD/1.wav")
sound2 = AudioSegment.from_wav("Dropbox/PIREAD/2.wav")
sound3 = AudioSegment.from_wav("Dropbox/PIREAD/3.wav")
sound4 = AudioSegment.from_wav("Dropbox/PIREAD/4.wav")
sound5 = AudioSegment.from_wav("Dropbox/PIREAD/5.wav")
sound6 = AudioSegment.from_wav("Dropbox/PIREAD/6.wav")
sound7 = AudioSegment.from_wav("Dropbox/PIREAD/7.wav")
sound8 = AudioSegment.from_wav("Dropbox/PIREAD/8.wav")
sound9 = AudioSegment.from_wav("Dropbox/PIREAD/9.wav")
sound0 = AudioSegment.from_wav("Dropbox/PIREAD/0.wav")

sounds=[sound1,sound2,sound3,sound4,sound5,sound6,sound7,sound8,sound9,sound0]
# option1 using +=
t1 = time.time()
combined_sounds1 = AudioSegment.empty()
for s in sounds
    combined_sounds1 += s
t2 = time.time()
# end of option1
# option2 using from_mono_audiosegments
t3 = time.time()
combined_sounds2 = AudioSegment.from_mono_audiosegments(*sounds)
t4 = time.time()
# end of option2
print('option1 (seconds):',t2-t1)
print('option2 (seconds):',t4-t3)

感谢以上的建议和意见。这是我使用的最终代码和生成视频的 link(使用 ffmpeg 可视化):

# Program to display the Fibonacci sequence up to n-th term
from pydub import AudioSegment
    
combined_sounds = ""
sound1 = AudioSegment.from_wav("1_2.wav")
sound2 = AudioSegment.from_wav("2_2.wav")
sound3 = AudioSegment.from_wav("3_2.wav")
sound4 = AudioSegment.from_wav("4_2.wav")
sound5 = AudioSegment.from_wav("5_2.wav")
sound6 = AudioSegment.from_wav("6_2.wav")
sound7 = AudioSegment.from_wav("7_2.wav")
sound8 = AudioSegment.from_wav("8_2.wav")
sound9 = AudioSegment.from_wav("9_2.wav")
sound0 = AudioSegment.from_wav("0_2.wav")



nterms=1000
# first two terms
n1, n2 = 0, 1
count = 0
fib = ""

# check if the number of terms is valid
if nterms <= 0:
    print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
    print("Fibonacci sequence upto",nterms,":")
    print(n1)
# generate fibonacci sequence
else:
    print("Fibonacci sequence:")
    while count < nterms:
        #print(n1)
        fib += str(n1)
        nth = n1 + n2
       # update values
        n1 = n2
        n2 = nth
        count += 1
        
i=-36
j=0

fibs = [fib[i:i+1000] for i in range(0, len(fib), 1000)]

seg = 0
for a in fibs:
    if seg == 2:
        break
        
    combined_sounds = AudioSegment.empty()
    seg +=1
    for x in a:
        i,j = -36,0
        s = eval("sound"+str(x))    
        s = s.apply_gain_stereo(i,j)
        combined_sounds += s
        i,j = j,i
        

    combined_sounds.export("joinedFile"+str(seg)+".wav", format="wav")

这会将输出拆分为 1000 位的 wav 文件。前 1000 个 Fibonacci 项产生近 15Gb 的波形!

上传到 YouTube:https://www.youtube.com/watch?v=U7Z_HOGqjlE

谢谢大家