Opening a wave file in Python: unknown format: 49. What's going wrong?
Asked Answered
R

2

10

I try to open a wave file with the wave module, but I keep getting the same error whatever I try. The line with the error is the following:

wav = wave.open(f)

This is the error message:

Traceback (most recent call last):
  File "annotate.py", line 47, in <module>
    play(file)
  File "annotate.py", line 33, in play
    wav = wave.open(f)
  File "C:\Program Files (x86)\Python\lib\wave.py", line 498, in open
    return Wave_read(f)
  File "C:\Program Files (x86)\Python\lib\wave.py", line 163, in __init__
    self.initfp(f)
  File "C:\Program Files (x86)\Python\lib\wave.py", line 143, in initfp
    self._read_fmt_chunk(chunk)
  File "C:\Program Files (x86)\Python\lib\wave.py", line 269, in _read_fmt_chunk
    raise Error('unknown format: %r' % (wFormatTag,))
wave.Error: unknown format: 49

String f is a path to a .WAV file and it works when played in any of my media players.

I have of course imported the wave module. I tried f both as a relative and an absolute path. I tried replacing "WAV" by "wav".

What is the error caused by?

Register answered 25/6, 2013 at 12:14 Comment(6)
Is the wave file compressed?Fortaleza
I don't know, how can I see?Register
Looks like wave module returns that error when you try to read a 32-bit .wav files.Redhanded
Are opening the file to get the data or to play the file?Cardamom
@Cardamom In order to get data. I know that the wave module can't play a file.Register
Found two related posts: forums.devshed.com/python-programming-11/… and bugs.python.org/issue1144504. Seems like your .wav file is a IEEE float wave file, which is not supported yet.Redhanded
M
13

Python's wave module works with a specific type of WAV: PCM (WAVE_FORMAT_PCM: 0x0001).

In your case, you're using a WAV of type WAVE_FORMAT_GSM610 [0x0031 = hex(49)].

You can use a program like Audacity or some lib for converting codecs to change the type of the WAV file.

You can see a list of WAV types here: https://www.videolan.org/developers/vlc/doc/doxygen/html/vlc__codecs_8h.html

Python's wave module source code: https://github.com/python/cpython/blob/master/Lib/wave.py

Midrib answered 24/11, 2014 at 9:47 Comment(6)
This is exactly what I did to solve it. I used ffmpeg to convert the files, and subprocess to call it.Register
@Register : Can you post what command you are using to convert the files? I have also tried converting them but it isn't working.Copley
@Leonardo Andrade the WAVE_FORMAT_PCM = 0x0001 is hard coded in wave.py If we change this to WAVE_FORMAT_GSM610 [0x0031 = hex(49)] will this work ?Probability
I don't know, @DSBLR. I'm not a specialist on WAVE formats. You can try but note that this will change the behaviour of a python's standard lib. Maybe not a good strategy to solve your problem.Midrib
Hi, Did you solve this issue? i am using sox to convert gsm file to wave but encountering the same issue. It is avoidable using audacity, but I am unable to find any python module to directly convert gsm to wav. Kindly comment if you have resolved this using python or bash. ThanksHeyman
@Copley and @Muhammad Usama : I solved a similar problem (converting from WAVE_FORMAT_MULAW 0x0007 to WAVE_FORMAT_PCM 0x0001) by simply using ffmpeg with the default arguments i.e. ffmpeg -i input.wav output.wav. After this, python's wave module could load the file. Apparently it was compressed before, because the file volume doubled in the conversion.Tjaden
R
2

The file is compressed and the wave module does not support this type of compression.

Register answered 25/6, 2013 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.