Chat gpt is great to assist you to code, maybe it will completely replace coder, but for the moment it can be a valid help. I remember that I made the right code to make the computer speak in any language using the google tts speech api, without having to save any audio file, but for some reason some code I found did not work. So I tried to get some help from chat gpt and “he” was incredibly fast and helpful. First it found a way to avoid an error and then I asked a way to avoid saving the audio file and he immediately found the way to do it, that I think was the way I found too in the past, but for some reason I forget where the right code was. So let’s explain this thing.
I had this code
from gtts import gTTS from io import BytesIO import pygame def speak(text, language='en'): mp3_fo = BytesIO() tts = gTTS(text, lang=language) tts.write_to_fp(mp3_fo) pygame.mixer.music.load(mp3_fo, 'mp3') pygame.mixer.music.play() # return mp3_fo pygame.init() pygame.mixer.init() # sound.seek(0) speak("Python is cool always")
this gave me this error
pygame 2.1.3 (SDL 2.0.22, Python 3.11.2) Hello from the pygame community. https://www.pygame.org/contribute.html Traceback (most recent call last): File "E:\tts\text_on_the_fly2.py", line 16, in <module> speak("Python is cool always") File "E:\tts\text_on_the_fly2.py", line 9, in speak pygame.mixer.music.load(mp3_fo, 'mp3') pygame.error: music_drmp3: corrupt mp3 file (bad tags). >>>
So, I asked chatgpt why there was this error, and he said
from gtts import gTTS from io import BytesIO import pygame def speak(text, language='en'): mp3_fo = BytesIO() tts = gTTS(text, lang=language) tts.write_to_fp(mp3_fo) mp3_fo.seek(0) with open('temp.mp3', 'wb') as f: f.write(mp3_fo.read()) pygame.mixer.music.load('temp.mp3') pygame.mixer.music.play() pygame.init() pygame.mixer.init() speak("Python is cool always")
This actually worked, the only problem is that it leaves a temporary file. I remember that once it worked without having to write file on the drive. So I asked chat gpt if he could do it
can you avoid writing an audio file?
Yes, you can avoid writing the audio file to disk by using pygame.mixer.Sound()
instead of pygame.mixer.music.load()
. Here’s the modified code that should work without writing to a file:
cssCopy codefrom gtts import gTTS
from io import BytesIO
import pygame
def speak(text, language='en'):
mp3_fo = BytesIO()
tts = gTTS(text, lang=language)
tts.write_to_fp(mp3_fo)
mp3_fo.seek(0)
sound = pygame.mixer.Sound(mp3_fo)
sound.play()
pygame.init()
pygame.mixer.init()
speak("Python is cool always")
In the updated code, we first create a BytesIO
object called mp3_fo
and write the audio data to it using the gTTS
library. Then, we reset the file pointer to the beginning of the file using mp3_fo.seek(0)
.
Next, instead of writing the audio data to a temporary file, we create a pygame.mixer.Sound()
object directly from the BytesIO
object. Finally, we call the play()
method on the Sound
object to play the audio.
And it worked!
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame