Big Puncho
Big Puncho

Reputation: 301

.wav file doesn't play any sounds

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)

Upvotes: 3

Views: 2527

Answers (1)

Torsten Marek
Torsten Marek

Reputation: 86502

The vector that you are using to create the wave file contains floats, but scipy.io interprets them as 64 bit ints (as is mentioned in the docs), which is not supported by most players.

Changing the last line to

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

should produce a file that can be played properly.

Upvotes: 6

Related Questions