[在python 3中使用numpy将wav文件混合在一起

问题描述 投票:2回答:1

我正在尝试从多个wav文件中创建一个音频文件。使用tkinter和pygame.mixer,我将按键转换为字典,该字典存储音频样本及其被调用的时间。 {sound1:10000, sound2:10001, ect...}

到目前为止,我已经设计出一种添加沉默块的方法:

def change_speed(seconds):
    '''modifies the metronome beat to loop at different speeds. This is done by creating a new wav file.'''
    #original wav file is 0.1 seconds long, so subtract that from time added
    seconds-=0.1
    #read the original wav file
    original = scipy.io.wavfile.read('Downloads\\sounds\\metronome_original.wav')
    #use sample rate of the original file (should be 44100) to create a new block of silence
    add_secs = np.array([[0]]*round(original[0]*seconds))
    add_secs.dtype='int16'
    #concatenate new block to original
    new = np.concatenate((original[1], add_secs))
    scipy.io.wavfile.write('Downloads\\sounds\\metronome.wav', original[0], new)

是否有某种方法可以将像[[0,0,1,1,2,0], [0,0,0,3,2,1]]这样的重叠数组合并到单个wav文件中?

更新:更具体地说,我正在尝试合并两个在播放时间重叠的音频样本,例如DJ,他先播放一首歌曲,然后播放另一首。有没有办法用python中生成的整数或字节数组来做到这一点?像这样:enter image description here

python python-3.x numpy pygame wav
1个回答
0
投票

这是我的处理方式:

wav1 = [0,0,1,1,2,0]
wav2 = [0,0,0,3,2,1]
combined = np.hstack([wav1, wav2])

from scipy.io import wavfile
import numpy as np

N = 2400  # Samples per second.

wavfile.write('combined.wav', rate=N, data=combined.astype(np.int16))
© www.soinside.com 2019 - 2024. All rights reserved.