3

I want to create a .wav file in Python using the numpy and scipy libraries, where multiple tones are played, the way I intend to do it is by storing my frequencies in an array and then the generated signals are stored in another one. I've managed to create such file with the desired playtime, but it doesn't play any sound. Am I missing something?

Thank you.

import numpy as np
from scipy.io import wavfile

freq =np.array([440,493,523,587,659,698,783,880]) #tone frequencies

fs=22050    #sample rate
duration=1 #signal duration
music=[]
t=np.arange(0,duration,1./fs) #time

for i in range(0,len(freq)):

    x=10000*np.cos(2*np.pi*freq[i]*t) #generated signals
    music=np.hstack((music,x))

wavfile.write('music.wav',fs,music)
4

1 回答 1

5

您用于创建波形文件的向量包含浮点数,但scipy.io将它们解释为 64 位整数(如docs 中所述),大多数播放器不支持。

将最后一行更改为

wavfile.write('music.wav',fs,music.astype(np.dtype('i2')))

应该会生成一个可以正常播放的文件。

于 2012-05-11T23:02:16.637 回答