I am trying to create a simple Assistant for my project. I crating an mp3 file and saving it using gtts module. After that i try to play that file but i cannot get it to play. I have tried many different modules. i tried os.system() to play the mp3 file but it gives me permission denied error. I am using anaconda environment inside pycharm to run my project. The python version i am using in anaconda environment is 3.6. I have tried pygame to try to runt the audio file but it didn't work either.
Below is the code i tried and the error i get.
import time
import speech_recognition as sr
from gtts import gTTS
from pydub import AudioSegment
from pydub.playback import play
def playMp3(filename):
mp3file = AudioSegment.from_mp3(filename)
play(mp3file)
def playWav(filename):
wavfile = AudioSegment.from_file(filename)
play(wavfile)
def speak(sentence):
tts = gTTS(text=sentence, lang='en')
filename = './new.wav'
tts.save(filename)
playMp3(filename)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print('say something')
audio = r.listen(source)
said = ''
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print('Exception: ' + str(e))
return said
# text = get_audio()
# if 'hello' or 'hi' in text:
speak('hello, how are you?')
After running the above code, i get the following error
ALSA lib conf.c:3723:(snd_config_hooks_call) Cannot open shared library libasound_module_conf_pulse.so (libasound_module_conf_pulse.so: libasound_module_conf_pulse.so: cannot open shared object file: No such file or directory)
ALSA lib control.c:1379:(snd_ctl_open_noupdate) Invalid CTL hw:0
ALSA lib pcm_dsnoop.c:642:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1090:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_jack.so (libasound_module_pcm_jack.so: libasound_module_pcm_jack.so: cannot open shared object file: No such file or directory)
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_jack.so (libasound_module_pcm_jack.so: libasound_module_pcm_jack.so: cannot open shared object file: No such file or directory)
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_oss.so (libasound_module_pcm_oss.so: libasound_module_pcm_oss.so: cannot open shared object file: No such file or directory)
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_oss.so (libasound_module_pcm_oss.so: libasound_module_pcm_oss.so: cannot open shared object file: No such file or directory)
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_pulse.so (libasound_module_pcm_pulse.so: libasound_module_pcm_pulse.so: cannot open shared object file: No such file or directory)
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_pulse.so (libasound_module_pcm_pulse.so: libasound_module_pcm_pulse.so: cannot open shared object file: No such file or directory)
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_upmix.so (libasound_module_pcm_upmix.so: libasound_module_pcm_upmix.so: cannot open shared object file: No such file or directory)
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_upmix.so (libasound_module_pcm_upmix.so: libasound_module_pcm_upmix.so: cannot open shared object file: No such file or directory)
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_vdownmix.so (libasound_module_pcm_vdownmix.so: libasound_module_pcm_vdownmix.so: cannot open shared object file: No such file or directory)
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_vdownmix.so (libasound_module_pcm_vdownmix.so: libasound_module_pcm_vdownmix.so: cannot open shared object file: No such file or directory)
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_usb_stream.so (libasound_module_pcm_usb_stream.so: libasound_module_pcm_usb_stream.so: cannot open shared object file: No such file or directory)
ALSA lib dlmisc.c:340:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_usb_stream.so (libasound_module_pcm_usb_stream.so: libasound_module_pcm_usb_stream.so: cannot open shared object file: No such file or directory)
ALSA lib pcm_dsnoop.c:642:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1090:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm_dmix.c:1090:(snd_pcm_dmix_open) unable to open slave
Expression 'paInvalidSampleRate' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2043
Expression 'PaAlsaStreamComponent_InitialConfigure( &self->playback, outParams, self->primeBuffers, hwParamsPlayback, &realSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2717
Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2838
Traceback (most recent call last):
File "/home/nabeel/PycharmProjects/EmotionsAgent/PersonalAssistant.py", line 44, in <module>
speak('hello, how are you?')
File "/home/nabeel/PycharmProjects/EmotionsAgent/PersonalAssistant.py", line 24, in speak
playMp3(filename)
File "/home/nabeel/PycharmProjects/EmotionsAgent/PersonalAssistant.py", line 11, in playMp3
play(mp3file)
File "/home/nabeel/anaconda3/envs/EmotionsAgent/lib/python3.8/site-packages/pydub/playback.py", line 67, in play
_play_with_pyaudio(audio_segment)
File "/home/nabeel/anaconda3/envs/EmotionsAgent/lib/python3.8/site-packages/pydub/playback.py", line 26, in _play_with_pyaudio
stream = p.open(format=p.get_format_from_width(seg.sample_width),
File "/home/nabeel/anaconda3/envs/EmotionsAgent/lib/python3.8/site-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/home/nabeel/anaconda3/envs/EmotionsAgent/lib/python3.8/site-packages/pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9997] Invalid sample rate
I ran the following script to check the default sample rate and got the following results:
from pydub import AudioSegment
from pydub.playback import play
import pyaudio
import sounddevice as sd
pa = pyaudio.PyAudio()
print(pa.get_default_input_device_info())
print(pyaudio.pa.__file__)
samplerates = 32000, 44100.0, 48000, 96000, 128000
device = 0
supported_samplerates = []
for fs in samplerates:
try:
sd.check_output_settings(device=device, samplerate=fs)
except Exception as e:
print(fs, e)
else:
supported_samplerates.append(fs)
print(supported_samplerates)
# import pyglet
#
# song = pyglet.media.load('new.mp3')
# song.play()
# pyglet.app.run()
# pyglet.app.exit()
The output for above code i got is:
/home/nabeel/anaconda3/envs/EmotionsAgent/bin/python3 /home/nabeel/PycharmProjects/EmotionsAgent/test.py
{'index': 0, 'structVersion': 2, 'name': 'HDA Intel PCH: CX20751/2 Analog (hw:1,0)', 'hostApi': 0, 'maxInputChannels': 2, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.005804988662131519, 'defaultLowOutputLatency': -1.0, 'defaultHighInputLatency': 0.034829931972789115, 'defaultHighOutputLatency': -1.0, 'defaultSampleRate': 44100.0}
/home/nabeel/anaconda3/envs/EmotionsAgent/lib/python3.8/site-packages/_portaudio.cpython-38-x86_64-linux-gnu.so
32000 Invalid number of channels [PaErrorCode -9998]
44100.0 Invalid number of channels [PaErrorCode -9998]
48000 Invalid number of channels [PaErrorCode -9998]
96000 Invalid number of channels [PaErrorCode -9998]
128000 Invalid number of channels [PaErrorCode -9998]
[]
After that i ran the following command to check my soundcard
lspci -knn|grep -iA2 audio
and got the following results
00:03.0 Audio device [0403]: Intel Corporation Broadwell-U Audio Controller [8086:160c] (rev 09)
Subsystem: Lenovo Broadwell-U Audio Controller [17aa:390b]
Kernel driver in use: snd_hda_intel
Kernel modules: snd_hda_intel
--
00:1b.0 Audio device [0403]: Intel Corporation Wildcat Point-LP High Definition Audio Controller [8086:9ca0] (rev 03)
Subsystem: Lenovo Wildcat Point-LP High Definition Audio Controller [17aa:390b]
Kernel driver in use: snd_hda_intel
Kernel modules: snd_hda_intel
I also debug my code and got into pyaudio.py file. It has a break point at the line it gives the error. But, i really do not know what it is. Maybe the samplerate i have to change manually in pyaudio. Because the default sample rate is 44100 and in pyaudio.py the rate is 24000.
below are the arguments for self._stream = pa.open(**arguments)
{'rate': 24000, 'channels': 1, 'format': 8, 'input': False, 'output': True, 'input_device_index': None, 'output_device_index': None, 'frames_per_buffer': 1024}
For my system information, I am using ubuntu 20.04 and the default python version is 3.8 but in my anaconda environment i am using python 3.6
I ran alsamixer command in the terminal and got the following infromation about sound card
0 HDA Intel HDMI
1 HDA Intel PCH
Please help me out of this situation. I have been stuck at it for too long and have been searching for it but cannot seem to find a solution. If any more information is needed regarding my hardware or any other configurations, I can provide.