0

i have a python code running as a service on Raspberry Pi 4. The app is basically to record some audio in a certain language using a microphone, save the audio to a .wav file, then use speech_recognition to recognize the language, then i use translators library and gTTS to transcribe, translate and convert the audio recorded to a desired language in .mp3, then i use playsound from betterplaysound library to play the .mp3 file.

Everything works perfectly fine on VS Code virtual environment. However when i run the python code as a background service it does not work. the recording works fine and save it to a .wav file, but playing the audio does not work.

code to save audio to .wav file (works fine)

with wave.open(wav_audio_file, 'w') as f:

    f.setparams((1, 2, 16000, 0, "NONE", "NONE"))
    f.writeframes(struct.pack("h" * len(audio), *audio))

code to convert .wav file to .mp3 file

with sr.AudioFile(wav_audio_file) as source:
     audio = recognizer.record(source)
     try:
        text_to_translate = recognizer.recognize_google(audio, language='en-GB')

what i could be missing! thanks in advance.

here is the error traceback as shown by the service log:

File "/home/pi/projects/my-project/venv/lib/python3.11/site-packages/speech_recognition/__init__.py", line 274, in __enter__
Oct 31 11:55:33 raspberrypi4 python3[1463]:     raise 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")
Oct 31 11:55:33 raspberrypi4 python3[1463]: 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
6
  • could it be a path issue? if you're using relative paths it would probably break if the script is not launched from the same location Commented Oct 30, 2024 at 15:44
  • Thank you @folengateis, i am not using relative paths. please check the updated question with stacktrace. i tried both os.path.abspath and os.path.realpath to access the successfully recorder .wav file. However, i think the problem lays somewhere else :( Commented Oct 31, 2024 at 10:43
  • the traceback talks about wrong file format, but doesn't provide the file path. is it complete? if both methods use the same code with the same environment, it should work. Commented Oct 31, 2024 at 15:40
  • i just noted that you forgot the binary flag, it should be with wave.open(wav_audio_file, 'wb') Commented Oct 31, 2024 at 15:41
  • sorry for the late reply @folengateis, the file path is fine i believe, like i said it works perfectly fine on VS Code with the same file paths. Also, i have tried with both "w" and "wb" and still the same issue. Commented Nov 7, 2024 at 9:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.