Py学习  »  Python

在python 3中使用numpy混合wav文件

Bugbeeb • 5 年前 • 1351 次点击  

{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/Django开发社区
本文地址:http://www.python88.com/topic/53688
 
1351 次点击  
文章 [ 1 ]  |  最新文章 5 年前
kwinkunks
Reply   •   1 楼
kwinkunks    5 年前

我会这样做:

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