joesh
joesh

Reputation: 181

reading .wav files in python

I'm trying (for a course) to read a sound file .wav via ipython. When I try the 'normal' code to read a file:

from scipy.io.wavfile import read

(fs,x) = read ('/Users/joehigham/Desktop/Audio_1.wav')

I get the well known traceback call of

ValueError: string size must be a multiple of element size 

Can anyone point me in the right direction as to why this happens, and of course how can I right the problem?

Thanks in advance - I did look round SO for the solution, but nothing (that I found) seems to match this problem with sound files.

Upvotes: 1

Views: 3449

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114781

Your wav file probably has 24 bit data. You can check with:

import wave
w = wave.open("filename.wav")
print(w.getsampwidth())

If the value printed is 3, your data is 24 bit. If that is the case, scipy.io.wavfile won't work. I wrote a reader that handles 24 bit data; see https://github.com/WarrenWeckesser/wavio (which replaced the gist at https://gist.github.com/WarrenWeckesser/7461781). The reader is also on PyPI.

Upvotes: 2

Related Questions