just need some help troubleshooting an issue that I am having with the playsound module. I installed it via this command:
pip install playsound
I was told that it does not have dependencies at all, but am unable to use it in my code:
import numpy as np
import cv2
import random
from playsound import playsound
playsound('/home/pi/Documents/Rover/Sounds/StillThere.wav')
Ultimately, my goal is just to be able to import a sound into a code, have it play once after a print statement, and stop. As I am sure you can see, I attempted to fix the issue by installing vext, but that just added more issues. Please help me figure out what I am doing wrong, how I can fix it, and if there is another module that I can install that is easier for a n00b and more able to do what I am trying to, I read here that play-sound only plays the sound and does not stop playing the sound:
"https://stackoverflow.com/questions/57158779/stopping-audio-with-playsound-module?answertab=active#tab-top"
I also read that someone else had this issue, but had been using anaconda/miniconda3, not sure what that is but seemed vital to the solution and since I am not using it, thought I would post. I am thinking it has to do with installing playsound/gi while in the opencv working area (workon cv) Please post your answers in an ELI5-type way, so that I may understand. Thank you!
Error:
This is the error I get:
```Traceback (most recent call last):
File "Soundtest.py", line 13, in <module>
playsound('/home/pi/Documents/Rover/Sounds/StillThere.wav')
File "/home/pi/.virtualenvs/cv/lib/python3.7/site-packages/playsound.py", line 91, in _playsoundNix
import gi
File "/home/pi/.virtualenvs/cv/lib/python3.7/site-packages/vext/gatekeeper/__init__.py", line 204, in load_module
raise ImportError("No module named %s" % modulename)
ImportError: No module named gi```
You can use other module (sounddevice and soundfile). You should 'pip install sounddevice' and 'pip install soundfile' in uour cmd. Put your StillThere.wav file in your Python folder. If you want to thank me, just mark this post as answer please.
import sounddevice as sd
import soundfile as sf
data, fs = sf.read('StillThere.wav', dtype='float32')
# time (in seconds) of start an audio file
start = 0
# time (in seconds) of end an audio file
end = 10000
sd.play(data[fs * start : fs * end, :], fs)
status = sd.wait()
You can use Pyaudio module. You should 'pip install pyaudio' and 'pip install wave' in cmd. Also you should put your '.py' file and audio file in the same folder.
import pyaudio
import wave
filename = 'StillThere.wav'
chunk = 1024
wf = wave.open(filename, 'rb')
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
data = wf.readframes(chunk)
while data != '':
stream.write(data)
data = wf.readframes(chunk)
stream.close()
p.terminate()
Related
import speech_recognition as sr
import requests
from gtts import gTTS
from playsound import playsound
import os
import subprocess
bot_message = ""
message = ""
myobj = gTTS(text="Hello I am Shilpa Sheety Speak Anything I am Listening", lang='en', tld='com.au')
myobj.save("starting.mp3")
playsound("starting.mp3")
while bot_message !="Bye":
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
message = r.recognize_google(audio)
print("You said : {}".format(message))
except:
print("Sorry Could not recognize your voice")
if len(message) == 0:
continue
print("Sending Message Now")
r = requests.post("http://localhost:5002/webhooks/rest/webhook", json={'message':message})
print("Bot Says,", end=' ')
for i in r.json():
bot_message = i['text']
print(f"{i['text']}")
myobj = gTTS(text=bot_message)
myobj.save("Welcome.mp3")
playsound("Welcome.mp3")
In above program I am playing welcome.mp3 in a loop. It is working fine for first 2 iterations but in 3rd iteration of for loop I am getting the following error:
Error 263 for command:
open Welcome.mp3
The specified device is not open or is not recognized by MCI.
Error 263 for command:
close Welcome.mp3
The specified device is not open or is not recognized by MCI. Failed to close the file: Welcome.mp3 Traceback (most recent call last): File "Voice_bot.py", line 31, in <module>
playsound("Welcome.mp3") File "C:\Users\DJ9004\anaconda4\lib\site-packages\playsound.py", line 72, in _playsoundWin
winCommand(u'open {}'.format(sound)) File "C:\Users\DJ9004\anaconda4\lib\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundException(exceptionMessage) playsound.PlaysoundException:
Error 263 for command:
open Welcome.mp3
The specified device is not open or is not recognized by MCI.*
It worked for me when I uninstalled playsound module and installed sn older version like this:
pip uninstall playsound
pip install playsound==1.2.2
It works when I install the version of playsound by uninstalling the previous version like this
pip uninstall playsound
pip install playsound==1.2.2
Try it
I had the same issue, it looks like the file is still being saved when you are trying to open it.
I added a couple of lines and it worked just fine:
for i in r.json():
bot_message = i['text']
print(f"{i['text']}")
myobj = gTTS(text=bot_message)
os.remove('Welcome.mp3') #LINE ADDED
myobj.save("Welcome.mp3")
time.sleep(1) #LINE ADDED
playsound("Welcome.mp3")
I had the same error as you and not finding answers I started to do tests. The way I found is not very practical, but it works for me. In a new file I wrote this piece of code (For the example we will call the 'function_sound_file' file):
from playsound import playsound
def function_sound():
playsound('complete/path/file.wav')
And in the file where I had the problem, I call the function I created after importing it (below).
from function_sound_file import function_sound
function_sound()
I tried an illogical solution but it worked every time.
just change the name of your audio file to 'audio.mp3'
and don't forget to close it by using "os.close('audio.mp3')" .
The code below never worked:
from gtts import gTTS
import os
#greetings
def start():
tts = gTTS(text="hi, its kate here! how may i help you?", lang='en' ,
slow=False)
tts.save('lol.mp3')
from playsound import playsound
playsound('lol.mp3')
os.remove('lol.mp3')
start()
But it worked every time:
from gtts import gTTS
import os
#greetings
def start():
tts = gTTS(text="hi, its kate here! how may i help you?", lang='en' ,
slow=False)
tts.save('audio.mp3')
from playsound import playsound
playsound('audio.mp3')
os.remove('audio.mp3')
start()
As you can see I just changed 'lol.mp3' to 'audio.mp3'.
Hope it works.
I am making a simple program for my entertainment using the playsound module. Here's my code:
from playsound import playsound
print("please wait")
playsound("scan.wav")
and the error messsage:
Traceback (most recent call last):
File "D:\Programi\python\lab\lab.py", line 9, in <module>
playsound("scan.wav")
File "C:\Users\ONT Studios\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\playsound.py", line 35, in _playsoundWin
winCommand('open "' + sound + '" alias', alias)
File "C:\Users\ONT Studios\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\playsound.py", line 31, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 266 for command:
open "scan.wav" alias playsound_0.98994089730741
Unknown problem while loading the specified device driver.
This is my path: D:\Programi\python\lab with files: lab.py, scan.mp3, and scan.wav.
I tried this, but with no success.
I was facing the same issue today and I was able to fix it in one way. It is probably a bug but you can get around it. So basically when importing several modules in a row, this will create the error. What I did is import first the playsound module then play one sound and print a bunch of random stuff then import the rest of the modules. This fixed my issue.
import mouse
from playsound import playsound
playsound('D:/rakkiz.mp3')
print("\n")
print("\n")
print("\n")
print("\n")
import re
import pywinauto
import time
import keyboard
I want to make a chatbot's response in audio and text.
All the example code using gTTS seem like one needs to 'save the text into a file then play the file'.
Is there another way to simplify the process such as, play the 'response from chatbot' automatically, using gTTS?
If you look even briefly at the docs, you'll see that, of the three examples, only one of them requires you to call save, and the third one is specifically called "Playing sound directly".
So, just do exactly what's in that example, but substitute your string in place of the literal 'hello':
>>> from gtts import gTTS
>>> from io import BytesIO
>>>
>>> my_variable = 'hello' # your real code gets this from the chatbot
>>>
>>> mp3_fp = BytesIO()
>>> tts = gTTS(my_variable, 'en')
>>> tts.write_to_fp(mp3_fp)
But notice that gTTS doesn't come with an MP3 player; you need a separate audio library to play that mp3_fp buffer:
>>> # Load `audio_fp` as an mp3 file in
>>> # the audio library of your choice
As the docs say, there are many such libraries, and Stack Overflow is not a good place to get recommendations for libraries. I happen to have a library installed, named musicplayer, and a sample app that can be easily adapted here, but it's probably not the simplest one by a long shot (it's made for doing more powerful, low-level stuff):
>>> import musicplayer
>>> class Song:
... def __init__(self, f):
... self.f = f
... def readPacket(self, size):
... return self.f.read(size)
... def seekRaw(self, offset, whence):
... self.f.seek(offset, whence)
... return f.tell()
>>> player = musicplayer.createPlayer()
>>> player.queue = [Song(mp3_fp)]
>>> player.playing = True
if you want to call speak function again and again without any error.
Basically, this serves the purpose.
from gtts import gTTS
import os
import playsound
def speak(text):
tts = gTTS(text=text, lang='en')
filename = "abc.mp3"
tts.save(filename)
playsound.playsound(filename)
os.remove(filename)
One of the solution that I found is by using pygame.mixer. In this case, import time is only used to ensure audio finishes before program ends.
from gtts import gTTS
from io import BytesIO
from pygame import mixer
import time
def speak():
mp3_fp = BytesIO()
tts = gTTS('hello, Welcome to Python Text-to-Speech!', lang='en')
tts.write_to_fp(mp3_fp)
return mp3_fp
mixer.init()
sound = speak()
sound.seek(0)
mixer.music.load(sound, "mp3")
mixer.music.play()
time.sleep(5)
[Linux] Speech in Python
Installation
[Terminal] Upgrade pip: pip install --upgrade pip
[Terminal] Install Google Text to Speech: pip install gTTS
[Terminal] Install pygame: pip install pygame
[Coding IDE] Add speech.py: See listing below
[Coding IDE] Call speak: See listing below
speech.py
from gtts import gTTS
from io import BytesIO
import pygame
class Speech():
#classmethod
def speak(cls, text):
mp3_file_object = BytesIO()
tts = gTTS(text, lang='en')
tts.write_to_fp(mp3_file_object)
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(mp3_file_object, 'mp3')
pygame.mixer.music.play()
Example
from .speech import Speech
Speech.speak('hello world')
Warning
It's a female voice and sounds realistic. It sounds like there's a woman in the room, fwiw.
You can also use the playsound library.
>>>import playsound
>>>playsound.playsound('sound.mp3')
For more information on playsound.Visit Playsound Docs .
I Am Trying To Play An MP3 File On Python , But I Can't Find The Right Module!
I've Tried This:
import os
os.startfile('hello.mp3')
But I Just Got The Error:
Traceback (most recent call last):
File "/Applications/Youtube/text 2 speech/test.py", line 2, in <module>
os.startfile('hello.mp3')
AttributeError: 'module' object has no attribute 'startfile'
I Have Also Tried This:
import vlc
p = vlc.MediaPlayer("file:hello.mp3")
p.play()
But I Get The Error:
Traceback (most recent call last):
File "/Applications/Youtube/text 2 speech/test.py", line 1, in <module>
import vlc
ImportError: No module named 'vlc'
But I Still Can't Find The Right Module. Could Someone Please Help?
You will need to install the vlc.py module from https://wiki.videolan.org/Python_bindings
The absolute bare bones for this would be something like:
import vlc
Inst = vlc.Instance()
player = Inst.media_player_new()
Media = Inst.media_new_path('/home/rolf/vp1.mp3')
player.set_media(Media)
player.play()
Although you would need to check player.get_state() to see if it is still running, paused, stopped etc and player.stop() to stop the audio once started
As far as I am aware os.startfile() is only available for the windows operating system.
Using the play command line instruction (Linux and probably OS X)
import os
os.system('play /home/rolf/vp1.mp3')
You could also look at Gstreamer, although the documentation could do with some improvement.
import gst
player = gst.element_factory_make("playbin")
player.set_property("uri", "file:///home/james/vp1.mp3")
player.set_state(gst.STATE_PLAYING)
play is stopped with player.set_state(gst.STATE_NULL),
pause with player.set_state(gst.STATE_PAUSED)
Note: Avoid tutorials for Gstreamer version 0.10, search instead for version 1.0
Both vlc and Gstreamer allow you to play audio and video, although, in my opinion, vlc in simpler to use but Gstreamer is more flexible.
import pygame
from pygame import mixer
def play_music():
global paused
if (paused):
mixer.music.unpause()
paused = False
else:
music = choose_music[0]
mixer.music.load(music)
mixer.music.play()
show_details(music)
musicpage.mainloop()
What I want to do:
I want to import a python module (pocketsphinx) and use the output from the Decoder attribute. However when I try to use it, I'm informed that module attribute 'Decoder' doesn't exist.
decoder = Decoder(configSwitches)
It does exist, though, which is what makes it so strange.
What I've done so far:
When I pull up a python console and input import pocketsphinx, it imports without any issue. Running pocketsphinx.file returns:
'/usr/local/lib/python2.7/dist-packages/pocketsphinx-0.0.8-py2.7-linux-armv7l.egg/pocketsphinx/__init__.pyc'
Looking in '/usr/local/lib/python2.7/dist-packages/pocketsphinx-0.0.8-py2.7-linux-armv7l.egg/pocketsphinx/__init__.py', I see: from pocketsphinx import * and that's it.
When I go back up to /usr/local/lib/python2.7/dist-packages/pocketsphinx/pocketsphinx.py and open it in a text editor, I see that pocketsphinx.py does indeed have a Decoder class with a healthy number of defined methods.
My Ask:
What other steps can I take to diagnose what's wrong with my use of the pocketsphinx module?
Here's the example code I was trying to run before really digging into the project:
import pocketsphinx
hmmd = r"/home/michael/Desktop/sphinxASR/pocketsphinx-5prealpha/model/en-us/en-us"
lmdir = r"/home/michael/Desktop/sphinxASR/pocketsphinx-5prealpha/model/en-us/en-us.lm.bin"
dictp = r"/home/michael/Desktop/sphinxASR/pocketsphinx-5prealpha/model/en-us/cmudict-en-us.dict"
fileName = r'/home/michael/Desktop/sphinxASR/voice_message.wav'
if __name__ == "__main__":
wavFile = open(fileName, "rb")
speechRec = pocketsphinx.Decoder(hmm=hmmd, lm=lmdir, dictionary=dictp)
wavFile.seek(44)
speechRec.decode_raw(wavFile)
result = speechRec.get_hyp()
print(result)
Stack trace:
Traceback (most recent call last):
File "/home/michael/PycharmProjects/27test/getHypTest.py", line 14, in <module>
speechRec = pocketsphinx.Decoder(lm=lmdir, dictionary=dictp)
AttributeError: 'module' object has no attribute 'Decoder'
By looking at the pocketsphinx example code, it seems that your import should be:
from pocketsphinx.pocketsphinx import *
My first step diagnosing this issue would be to type the following, so that I can see what is being imported:
import pocketsphinx
dir(pocketsphinx)
You should import Decoder from pocketsphinx.
Instead of
import pocketsphinx
try:
from pocketsphinx.pocketsphinx import Decoder