from ..queues import QueueTtS
from ..components import _BaseComponent
from gtts import gTTS
from .playsound import playsound

import os


class TextToSpeech(_BaseComponent):

    def __init__(self):
        super().__init__()
        self.queue_tts = QueueTtS()

    def run(self):
        sentence = self.queue_tts.get()
        print('To say out loud : {}'.format(sentence))
        # TODO change the language to match user's settings
        tts = gTTS(text=sentence, lang='en')
        tts.save("tts.mp3")
        playsound("tts.mp3")
        os.remove("tts.mp3")
        self.queue_tts.task_done()

@@ -2,72 +2,43 @@
 rom 
-..queues import QueueTtS%0Afrom ..components import _BaseComponent
+tempfile import NamedTemporaryFile%0A
 %0Afro
@@ -56,16 +56,17 @@
 rt gTTS%0A
+%0A
 from .pl
@@ -94,18 +94,77 @@
 und%0A
-%0Aimport os
+from ..queues import QueueTtS%0Afrom ..components import _BaseComponent
 %0A%0A%0Ac
@@ -413,189 +413,171 @@
     
-# TODO change the language to match user's settings%0A        tts = gTTS(text=sentence, lang='en')%0A        tts.save(%22tts.mp3%22)%0A        playsound(%22tts.mp3%22)%0A        os.remove(%22tts.mp3%22
+tts = gTTS(text=sentence, lang='en')%0A        with NamedTemporaryFile() as audio_file:%0A            tts.write_to_fp(audio_file)%0A            playsound(audio_file.name
 )%0A  

use temporary file for text to speech