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
with wave.open(wav_audio_file, 'wb')