Thursday, 5 September 2013

Writing wav file in Python with wavfile.write from SciPy

Writing wav file in Python with wavfile.write from SciPy

I have this code:
import numpy as np
import scipy.io.wavfile
import math
rate, data = scipy.io.wavfile.read('xenencounter_23.wav')
data2 = []
for i in range(len(data)):
data2.append([int(round(math.sin(data[i][0])*3000)),
int(round(math.sin(data[i][1])*3000))])
data2 = np.asarray(data2)
print data2
scipy.io.wavfile.write('xenencounter_23sin3.wav',rate,data2)
This prints (truncated):
[[-2524 2728]
[ -423 -2270]
[ 2270 423]
...,
[-2524 0]
[ 2524 -2728]
[-2270 838]]
The wav file opens and plays in Windows Media Player, so at least its the
proper format. However, when opening it with Audacity and looking at the
individual samples, they're all 0, and concordantly the file plays no
sound at all.
What I don't understand is how that numpy array listed above becomes all
0's. It should be below the maximum value for a sample (or above, if it's
negative).

No comments:

Post a Comment