gTTS direct output - python

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 .

Related

Error 263 for command: open Welcome.mp3 The specified device is not open or is not recognized by MCI

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.

Trying to use playsound as a noob

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()

why speech recognition is not recognizing any sound

I'm working on a Virtual Assistant project that recognizes a speech then convert it to a text and follow instructions according to the speech.
now, my issue is the speech never recognize any word I say, there are no errors popping. however, when I print the speech recognized it is always blank.
please see the below code:
import wikipedia
import webbrowser
import speech_recognition as sr
import pyttsx3
import subprocess
import os
from gtts import gTTS
import datetime
import warnings
import calendar
import random
import pyaudio
#ignore warnings
warnings.filterwarnings('ignore')
def record_audio():
#record
r = sr.Recognizer() #creating recognizer object
#open the mic and record
with sr.Microphone() as source:
print('say somthing!')
audio = r.listen(source)
#use google speech recognition
data = ''
try:
date = r.recognize_google(audio)
print('you said: '+data)
except sr.UnknownValueError:
print('google cant understand the audio !')
except sr.RequestError as e:
print('request results from google speech recognition service error '+ e)
return data
record_audio()
the output is always as per the below:
you said :
what I have tried to solve this:
r.adjust_for_ambient_noise(source, duration=1)
in terminal: pip install pipwin
none of the above worked.
indentations are fine, it is just the formatting of the thread.
There is a typo in your code, that's why you will never know what google understood.
data = r.recognize_google(audio)

cannot import gTTS from gtts:IMPORT ERROR

So this speech to text code in python is having problem in import gTTs when it is imported gtts only then its fine but the problem persists with gTTS. The code is as belows.
from gtts import gTTs
import os
text_to_read = "Read any text written "
language = 'en'
slow_audio_speed = False
filename = 'myfile.mp3'
def reading_from_string():
audio_created = gtts.gTTs(text=text_to_read,lang = language,slow = slow_audio_speed)
audio_created.save("myfile.mp3")
os.system("mpg321 myfile.mp3")
if __name__ == "__main__":
reading_from_string()
the error is as belows
*ImportError: cannot import name 'gTTs' from 'gtts' *
i tried to uninstall and install pip gtts and pip gTTs
again and again but the problem seems to be with gTTs.
Also if possible can you suggest a solution to add some natural voice in this code to make it sound more natural
this should work.
from gtts import gTTS
import os
text_to_read = "Read any text written "
language = 'en'
slow_audio_speed = False
filename = 'myfile.mp3'
def reading_from_string():
audio_created = gTTS(text=text_to_read,lang = language,slow = slow_audio_speed)
audio_created.save("myfile.mp3")
os.system("start myfile.mp3")
if __name__ == "__main__":
reading_from_string()
first it should be gTTS and not gTTs (note small and caps 'S') and in function reading_from_string() it should be gTTS(...) and not gtts.gTTs

Python MP3 Player

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()

Categories

Resources