Writing wav file in Python with wavfile.write from SciPy
Asked Answered
O

3

20

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).

Oaf answered 5/9, 2013 at 20:40 Comment(6)
what happens if you reload it with scipy? is it zeros or the values you saved?Overtone
It returns the same thing as it printed before writing.Oaf
Can you add a print data statement after the line rate, data = scipy.io.wavfile.read('xenencounter_23.wav'). I want to see what that data looks like.Overtone
It prints this [[-1 2] [-3 4] [-4 3] ..., [-1 0] [ 1 -2] [ 4 -6]] However, in another part of the array it lists: [-2050 -1856] [-1814 -1621] [-1493 -1295] [-2042 -1848], so pretty similarOaf
try scipy.io.wavfile.write('xenencounter_23sin3.wav',rate,data) - you want to figure out if the write method or the operation you do on the data is the problem.Overtone
That worked fine. It must be something with the data2 arrayOaf
O
17

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.

Oaf answered 6/9, 2013 at 9:11 Comment(3)
This fixed my issue as well. Would be nice for scipy to to note this in the docs though :SPorbeagle
Thanks a ton for that sharing man! You're awesome.Sanitation
By the way to others who is intrested, look at the example down below here: docs.scipy.org/doc/scipy/reference/generated/…Sanitation
I
2

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.

Indebtedness answered 15/4, 2014 at 23:12 Comment(0)
O
1

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.

Overtone answered 5/9, 2013 at 20:59 Comment(9)
It did the same thing. I think sample values between -32768 and 32767 should be fine.Oaf
Try data2.append([int(data[i][0]), [int(data[i][1])]) - unfortunately you need to keep eliminating things until you discover the function which breaks your code.Overtone
That did the same thing. I understand wave file data is stored as a two's complement number per each sample. I don't know if that would make a difference. It doesn't print as a two's complement number, so I figure that conversion is in wavefile.writeOaf
what is the output of data1.shape == data2.shape ? I am beginning to suspect the second array has the wrong dimensions.Overtone
is 'xenencounter_23.wav' definitely uncompressed wav?Overtone
Yes, I exported it with Audacity into signed 16bit PCM wave format.Oaf
And the file saved from scipy (i.e data2) does play in Windows Media but not Audacity? I'm thinking it's a wma file.Overtone
It plays in both, with no errors. Just in Audacity I can look at the individual samples, and they all appear to be 0 exactly.Oaf
Yeah, all 0's would play no sound..., as in it's silent, not that it doesn't play.Oaf

© 2022 - 2024 — McMap. All rights reserved.