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).
The function needs two parameters - first the file name and second the mode. The mode can be 'wb' for writing audio data or 'rb' for reading. A mode of 'rb' returns a Wave_read object, while a mode of 'wb' returns a Wave_write object. Close the file if it was opened by wave.
The Scipy.io (Input and Output) package provides a wide range of functions to work around with different format of files. Some of these formats are − Matlab. IDL.
I found that scipy.io.wavfile.write() writes in 16-bit integer, which explains the larger file sizes when trying to use a 32-bit integer (the default) instead. While I couldn't find a way to change this in wavfile.write, I did find that by changing:
data2 = np.asarray(data2)
to
data2 = np.asarray(data2, dtype=np.int16)
I could write a working file.
In creating wav files through scipy.io.wavfile.write(), i found that the amplitude is very important. if you create a sine wave with amplitude 150, it sounds like silence when played in VLC. if the amplitude is 100, it sounds like a distorted sine wave, and if you make it 80, it starts to sound like a normal file.
Definitely have to be careful about the amplitude when creating wave files, but it's not clear to me right now what the maximum level is before it starts clipping or disappearing.
As you discovered by printing out the output at different points and re-saving what you originally loaded, the line data2.append([int(round(math.sin(data[i][0])*3000)), int(round(math.sin(data[i][1])*3000))])
is the source of the problem.
I suspect that 3000 is too large of an amplitude. Try 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With