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

JVE999
  • 3,327
  • 10
  • 54
  • 89
  • what happens if you reload it with scipy? is it zeros or the values you saved? – Mike Vella Sep 05 '13 at 20:41
  • It returns the same thing as it printed before writing. – JVE999 Sep 05 '13 at 20:45
  • 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. – Mike Vella Sep 05 '13 at 20:47
  • 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 similar – JVE999 Sep 05 '13 at 20:49
  • 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. – Mike Vella Sep 05 '13 at 20:51
  • That worked fine. It must be something with the data2 array – JVE999 Sep 05 '13 at 20:57

3 Answers3

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.

JVE999
  • 3,327
  • 10
  • 54
  • 89
  • This fixed my issue as well. Would be nice for scipy to to note this in the docs though :S – TTT May 17 '15 at 11:23
  • Thanks a ton for that sharing man! You're awesome. – JavaRunner Apr 14 '21 at 03:25
  • By the way to others who is intrested, look at the example down below here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.write.html – JavaRunner Apr 14 '21 at 03:26
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.

Milothicus
  • 123
  • 2
  • 7
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.

Mike Vella
  • 10,187
  • 14
  • 59
  • 86
  • It did the same thing. I think sample values between -32768 and 32767 should be fine. – JVE999 Sep 05 '13 at 21:05
  • 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. – Mike Vella Sep 05 '13 at 21:07
  • 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.write – JVE999 Sep 05 '13 at 21:11
  • what is the output of `data1.shape == data2.shape` ? I am beginning to suspect the second array has the wrong dimensions. – Mike Vella Sep 05 '13 at 21:15
  • is 'xenencounter_23.wav' definitely uncompressed wav? – Mike Vella Sep 05 '13 at 22:03
  • Yes, I exported it with Audacity into signed 16bit PCM wave format. – JVE999 Sep 05 '13 at 22:12
  • 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. – Mike Vella Sep 05 '13 at 22:19
  • 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. – JVE999 Sep 05 '13 at 22:22
  • Yeah, all 0's would play no sound..., as in it's silent, not that it doesn't play. – JVE999 Sep 06 '13 at 04:26