【问题标题】:Blending together wav files using numpy in python 3在 python 3 中使用 numpy 将 wav 文件混合在一起
【发布时间】:2020-01-26 04:14:26
【问题描述】:

我正在尝试从多个 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中生成的整数或字节数组来做到这一点? 像这样:

【问题讨论】:

  • This answer 关于交叉淡入淡出的音频可能很有用。

标签: python python-3.x numpy pygame wav


【解决方案1】:

我会这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多