Scipy io read wavfile error

8.4k views Asked by At

Whenever I try to read a .wav file, the following error comes. I have searched everywhere but had no progress upon this.
CODE:

import scipy as sp
import matplotlib.pyplot as plt
sr, y = sp.io.wavfile.read(MY_FILENAME)
print sr


ERROR:

  File "/usr/local/lib/python2.7/dist-packages/scipy/io/wavfile.py", line 252, in read
    fmt_chunk = _read_fmt_chunk(fid, is_big_endian)
  File "/usr/local/lib/python2.7/dist-packages/scipy/io/wavfile.py", line 94, in _read_fmt_chunk
    raise ValueError("Unknown wave file format")
  ValueError: Unknown wave file format

Update:

After I tried converting my file as suggested by @velikodniy using sox:

sox MY_FILENAME.wav MY_FILENAME.wav

But it throws another warning:

sox WARN wav: Premature EOF on .wav input file

And now if I try to play the original .wav file, it says unsupported format in the media player(previously it was playing)

3

There are 3 answers

7
velikodniy On BEST ANSWER

WAVs may contain audio data in different formats. For example, MP3.scipy.io.wavfile.read can read only PCM and floating point formats (WAVE_FORMAT_PCM and WAVE_FORMAT_IEEE_FLOAT, to be exact) at the moment.

So you must convert your audio file with an audio editor (e.g. Audacity or sox).

0
Ângelo Polotto On

I tried many solutions, but I only had success with the following steps:

  1. Goto to the site: https://audio.online-convert.com/convert-to-wav ;
  2. Upload your file ;
  3. In Optional settings change Change bit resolution to 16 bit ;
  4. Click in Start Conversion and download your converted file;
  5. Try open again with scipy.io.wavfile.read(...);
0
Cristeena Joseph On

I converted the wav file to specific format using code and the error got resolved.

import soundfile as sf
y, s = librosa.load(pwd+'\\ans.wav', sr=48000)
sf.write('audio_test_1.wav', y, s, "PCM_24")

rate, data = read(pwd+'\\audio_test_1.wav')