'How can format a wav file such that scipy.io.wavfile.read can read it?

I have been making a simple audio frequency analyzer program, but have run into a problem: scipy.io.wavfile.read fails for every wav file I can find and appears to be nonfunctional.

The code in question is using a tk file picker to open a wav file and pass the open file object to scipy.io.wavfile.read, then save the output to a predefined container object:

import scipy.io.wavfile as wf
from tkinter import filedialog
import os
...
wavFile = filedialog.askopenfile(title='Select wav file to open',
                initialdir=os.getcwd(),filetypes=(('wav files','*.wav'),))

container.rate,container.wavData = wf.read(wavFile)

The tk dialog works correctly, but I always get an error on the actual read, so I am 95% sure that I am calling this function correctly but I have bad wav files.

Some error messages I have received on different files:

Traceback (most recent call last):
  File "...Python-DataViewer-Demo\Code\IO\WAVUtils.py", line 30, in callback
    container.rate,container.wavData = wf.read(wavFile)
  File "...Python-DataViewer-Demo\venv\lib\site-packages\scipy\io\wavfile.py", line 650, in read
    file_size, is_big_endian = _read_riff_chunk(fid)
  File "...Python-DataViewer-Demo\venv\lib\site-packages\scipy\io\wavfile.py", line 521, in _read_riff_chunk
    raise ValueError(f"File format {repr(str1)} not understood. Only "
ValueError: File format 'RIFF' not understood. Only 'RIFF' and 'RIFX' supported.`
Traceback (most recent call last):
  File "...Python-DataViewer-Demo\Code\IO\WAVUtils.py", line 30, in callback
    container.rate,container.wavData = wf.read(wavFile)
  File "...Python-DataViewer-Demo\venv\lib\site-packages\scipy\io\wavfile.py", line 650, in read
    file_size, is_big_endian = _read_riff_chunk(fid)
  File "...Python-DataViewer-Demo\venv\lib\site-packages\scipy\io\wavfile.py", line 512, in _read_riff_chunk
    str1 = fid.read(4)  # File signature
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 78: character maps to <undefined>`

Following past advice I have attempted to convert the wav files to 16 bit using https://audio.online-convert.com/convert-to-wav and https://cloudconvert.com/wav-converter but these do not work either.

Is there a reliable method to process a wav file such that scipy.io.wavfile.read can handle it (or if anyone could give an example of ANY wav file that this module can read)? If not, is there another wav file -> numpy array reader that is more robust that someone can recommend?

The scipy version is 1.8.0, python is 3.10.4, Windows 10. I am developing this inside a venv virtual environment that has up to data pip installs of the relevant modules. I notice in the second stack trace that it is calling a codec from my main python install instead of one located in venv, which I did not think was supposed to happen, so could that also be an error?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source