Python, speech_recognition tool does not recognize .wav file

Question:

I have generated a .wav audio file containing some speech with some other interference speech in the background.
This code worked for me for a test .wav file:

    import speech_recognition as sr

    r = sr.Recognizer()
    with sr.WavFile(wav_path) as source:
        audio = r.record(source)

    text = r.recognize_google(audio)

If I use my .wav file, I get the following error:

ValueError: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format

The situation slightly improves if I save this .wav file with soundfile:

    import soundfile as sf        

    wav, samplerate = sf.read(wav_path)
    sf.write(saved_wav_path, original_wav, fs)

and then load the new saved_wav_path back into the first block of code, this time I get:

if not isinstance(actual_result, dict) or len(actual_result.get(“alternative”, [])) == 0: raise UnknownValueError()

The audio files were saved as

    wavfile.write(wav_path, fs, data)

where wav_path = ‘data.wav’. Any ideas?

SOLUTION:

Saving the audio data the following way generates the correct .wav files:

    import wavio
    wavio.write(wav_path, data, fs ,sampwidth=2)
Asked By: Phys

||

Answers:

From a brief look at the code in the speech_recognition package, it appears that it uses wave from the Python standard library to read WAV files. Python’s wave library does not handle floating point WAV files, so you’ll have to ensure that you use speech_recognition with files that were saved in an integer format.

SciPy’s function scipy.io.wavfile.write will create an integer file if you pass it an array of integers. So if data is a floating point numpy array, you could try this:

from scipy.io import wavfile

# Convert `data` to 32 bit integers:
y = (np.iinfo(np.int32).max * (data/np.abs(data).max())).astype(np.int32)

wavfile.write(wav_path, fs, y)

Then try to read that file with speech_recognition.

Alternatively, you could use wavio (a small library that I created) to save your data to a WAV file. It also uses Python’s wave library to create its output, so speech_recognition should be able to read the files that it creates.

Answered By: Warren Weckesser

I couldn’t figure out what the sampwidth should be for wavio from its documentation; however, I added the following line sounddevice.default.dtype='int32', 'int32' which allowed sounddevice, scipy.io.wavfile.write / soundfile, and speech_recognizer to finally work together. The default dtype for sounddevice was float32 for both input and output. I tried changing only the output but it didnt work. Weirdly, audacity still thinks the output files are in float32. I am not suggesting this is a better solution, but it did work with both soundfile and scipy.

I also noticed another oddity. When sounddevice.default.dtype was left at the default [float32, float32] and I opened the resulting file in audacity. From audacity, I exported it and this exported wav would work with speechrecognizer. Audacity says its export is float32 and the same samplerate, so I don’t fully understand. I am a noob but looked at both files in a hex editor and they look the same for the first 64 hex values then they differ… so it seems like the header is the same. Those two look very different than the file I made using int32 output, so seems like there’s another factor at play…

Answered By: rfii

Similar to Warren’s answer, I was able to resolve this issue by rewriting the WAV file using pydub:

from pydub import AudioSegment

filename = "payload.wav"  # File that already exists.
                       
sound = AudioSegment.from_mp3(filename)
sound.export(filename, format="wav")
Answered By: Tim