I'm trying to read a wav file, then manipulate its contents, sample by sample
Here's what I have so far:
import scipy.io.wavfile
import math
rate, data = scipy.io.wavfile.read('xenencounter_23.wav')
for i in range(len(data)):
data[i][0] = math.sin(data[i][0])
print data[i][0]
The result I get is:
0
0
0
0
0
0
etc
It is reading properly, because if I write print data[i]
instead I get usually non-zero arrays of size 2.
The array
data
returned bywavfile.read
is a numpy array with an integer data type. The data type of a numpy array can not be changed in place, so this line:casts the result of
math.sin
to an integer, which will always be 0.Instead of that line, create a new floating point array to store your computed result.
Or use
numpy.sin
to compute the sine of all the elements in the array at once:From your additional comments, it appears that you want to take the sine of each value and write out the result as a new wav file.
Here is an example that (I think) does what you want. I'll use the file 'M1F1-int16-AFsp.wav' from here: http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Samples.html. The function
show_info
is just a convenient way to illustrate the results of each step. If you are using an interactive shell, you can use it to inspect the variables and their attributes.Here's the output. (The initial warning means there is perhaps some metadata in the file that is not understood by
scipy.io.wavfile.read
.)The new file 'newname.wav' contains two channels of signed 16 bit values.