Replies: 57 comments 1 reply
-
Yes it's possible. You need a module that will play from a file-like/file object. Instead of doing
|
Beta Was this translation helpful? Give feedback.
-
This would indeed be fantastic. I'm having issues with finding a decent approach. Most audio playback libraries are hectic to use in this scanario. |
Beta Was this translation helpful? Give feedback.
-
@pndurette thanks for help! Before, i used
to play the audio file. Now, i have a an audio object called I tried but is does not work. Do you have another example how to playback the generated audio object? Thanks for helping! Josef |
Beta Was this translation helpful? Give feedback.
-
Try Pyglet - a multimedia and windowing library for Python (~3MB)
|
Beta Was this translation helpful? Give feedback.
-
Thanks @anselm94, but i want to avoid saving the audio data on the file system. Instead, i directly want to play the audio output from module gTTS. Do you think this is possible with pyglet? |
Beta Was this translation helpful? Give feedback.
-
Currently Pyglet doesn't support using file-like objects. As the project documentation here (see Loading Media section) says
|
Beta Was this translation helpful? Give feedback.
-
@jhoelzl |
Beta Was this translation helpful? Give feedback.
-
@rishabhrkaushik unfortunately not. |
Beta Was this translation helpful? Give feedback.
-
Hi, are there any ways to output file.wav , not mp3 ? i don't know how to use python play mp3 file. The pyglet like anselm94 said can't open file .mp3 thank you and regards |
Beta Was this translation helpful? Give feedback.
-
@shaolinkhoa using pyMedia lib |
Beta Was this translation helpful? Give feedback.
-
Use pygame multimedia package |
Beta Was this translation helpful? Give feedback.
-
pygame.mixer.init() |
Beta Was this translation helpful? Give feedback.
-
My solution to unicode-charactered languages:
You have to have a mpg123 to run this program, but it deletes the ".mp3" file everytime it finishes the phrase. |
Beta Was this translation helpful? Give feedback.
-
@yagiz190799 jesus... Also your clear can be safer with Might i also recommend pep8 standard, your using tabs for indentation (smh)... spaces mate!! some people just want to see the world burn :P |
Beta Was this translation helpful? Give feedback.
-
If are you using a web browser,
|
Beta Was this translation helpful? Give feedback.
-
Hello everyone, I would like to share this solution using import io
import time
import pyglet
from gtts import gTTS
pyglet.options["audio"] = ("pulse",)
TICK = .1
def speak(words: str, lang: str="en"):
with io.BytesIO() as f:
gTTS(text=words, lang=lang).write_to_fp(f)
f.seek(0)
sound = pyglet.media.load("_.mp3", file=f)
player = sound.play()
while player.playing:
pyglet.app.platform_event_loop.dispatch_posted_events()
pyglet.clock.tick()
time.sleep(TICK) Edit: Improved using suggestions by @VeNoMouS |
Beta Was this translation helpful? Give feedback.
-
Would it not make more sense to do the following.. import io
import time
import pyglet
from gtts import gTTS
pyglet.options["audio"] = ("pulse",)
def speak(words: str, lang: str="en"):
with io.BytesIO() as f:
gTTS(text=words, lang=lang).write_to_fp(f)
f.seek(0)
player = pyglet.media.load('_.mp3', file=f).play()
while player.source.duration != player.time:
pyglet.app.platform_event_loop.dispatch_posted_events()
pyglet.clock.tick() Or better yet... import io
import time
import pyglet
from gtts import gTTS
pyglet.options["audio"] = ("pulse",)
def speak(words: str, lang: str="en"):
with io.BytesIO() as f:
gTTS(text=words, lang=lang).write_to_fp(f)
f.seek(0)
player = pyglet.media.load('_.mp3', file=f).play()
while player.playing:
pyglet.app.platform_event_loop.dispatch_posted_events()
pyglet.clock.tick() |
Beta Was this translation helpful? Give feedback.
-
@VeNoMouS, that definitely looks nicer, but I can't get it to work: I only get the first small snippet of sound and only when i use |
Beta Was this translation helpful? Give feedback.
-
@sorenmulli updated those examples for you then ;P |
Beta Was this translation helpful? Give feedback.
-
Hey guys im trying to do a voice assistant HELP im trying to make the python file speak what i actually want it to speak. I have tried play sound and the temporary files. NO PIGLET or pygame plz code:
|
Beta Was this translation helpful? Give feedback.
-
@fasterastv2, do you have read access to the location where Voice.mp3 is being saved? |
Beta Was this translation helpful? Give feedback.
-
@themdrizwanansari Just realise that the problem isnt with playing sound. The actual problem is with the saving part. The line |
Beta Was this translation helpful? Give feedback.
-
Here is a solution to play gTTS sound directly using from gtts import gTTS
from tempfile import NamedTemporaryFile
from playsound import playsound
def speak(txt, lang='en'):
gTTS(text=txt, lang=lang).write_to_fp(voice := NamedTemporaryFile())
playsound(voice.name)
voice.close()
txt = "Spank me, Daddy"
speak(txt) Works in Python 3.9 and negates the need to save output to mp3. |
Beta Was this translation helpful? Give feedback.
-
@venzen ,thanks for your solution, only it works for me!
|
Beta Was this translation helpful? Give feedback.
-
Ohh, my hands hurt. |
Beta Was this translation helpful? Give feedback.
-
tank you @venzen and @GrimPixel that worked like a charm |
Beta Was this translation helpful? Give feedback.
-
I tried pyglet, pygame and the tempfile + playsound solution, but they all failed due to various bugs (Windows 10 Python 3.10). Audio play doesn't seem to be a very stable area, seriously, or my PC is broken. Then I tried this solution: from io import BytesIO
from pydub import AudioSegment
from pydub.playback import play
def say(text, lang = "en"):
tts = gTTS(text=text, lang=lang)
fp = BytesIO()
tts.write_to_fp(fp)
fp.seek(0)
song = AudioSegment.from_file(fp, format="mp3")
play(song) which in theory should be efficient because it avoids writing the file on disk. Or so I thought, but it ended up being a few hundred ms slower than the naive solution which simply writes the file to disk. (I have an SSD though.) For a short text (5 words) the solution below is 200 - 300 ms faster. from playsound import playsound
def speak_gtts(words: str, lang: str="en"):
filename = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".mp3"
tts = gTTS(words, lang=lang)
tts.save(filename)
playsound(filename)
os.remove(filename) |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
import io from gtts import gTTS hello = gTTS('hello') fd.seek(0) song = AudioSegment.from_mp3(fd) play(song) |
Beta Was this translation helpful? Give feedback.
-
Loved the repository - thanks! Here's my contribution using sounddevice and soundfile: SAMPLE SCRIPT
|
Beta Was this translation helpful? Give feedback.
-
Hello,
i have following code:
Since i play the output directly after this code, is there a way to get the audio output directly as a variable? So i do not have to save and open the mp3 file again.
Thanks!
Josef
Beta Was this translation helpful? Give feedback.
All reactions