Scipy io read wavfile error
Asked Answered
S

3

6

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)

Stoltz answered 28/8, 2017 at 8:31 Comment(0)
S
5

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

Seumas answered 28/8, 2017 at 8:42 Comment(7)
@velikodnly, u mean to convert from wav to wav using sox?Stoltz
Yes, I do. Sox supports a wide range of internal WAV formats. Such as μ-law, A-law and so on. Maybe your file contains data encoded with one of them. Unfortunately, sox doesn't support mp3 inside WAVs.Seumas
it still throws a warning and now the file is not playing even in the media playerStoltz
Could you test your program with WAV files from Wikipedia?Seumas
I've just tested. sp.io.wavfile.read works fine with 11k16bitpcm.wav.Seumas
i found the solution, by converting my files to .ogg format and back to .wav, and it worked!!!Stoltz
i had tested sp.io.wavfile.read with harvard audio files provided for testing, but it was not working for any of my .wav filesStoltz
S
2

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(...);
Steenbok answered 4/7, 2019 at 17:25 Comment(0)
S
0

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')
Starling answered 9/11, 2021 at 4:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.