File size: 1,602 Bytes
5ffb1d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

from gtts import gTTS
from CaesarFolderInterface.caesarfolderinterface import CaesarFolderInterface
import os

class CaesarMobileTTS(CaesarFolderInterface):
    def __init__(self) -> None:
        super().__init__()
    def load_transcription(self,argfilename):
        with open(f"{self.audio_output_folder}/{argfilename}.mp3","rb") as f:
            contents = f.read()
        return contents

    def check_file_exists(self,argfilename):
        folder =  self.audio_output_folder
        if folder in os.listdir():
            if f"{argfilename}.mp3" in os.listdir(folder):
                return True
            else:
                return False
        else:
            return True
    def clean_up_tts(self,argfilename):
        try:
            folder = self.audio_output_folder
            os.remove(f"{folder}/{argfilename}.mp3")
            return True
        except Exception as ex:
            return False
                
    def run_tts(self,argfilename,text,language):
        try:
            myobj = gTTS(text=text, lang=language, slow=False)
            # Saving the converted audio in a mp3 file named
            # welcome
            if self.audio_output_folder not in os.listdir():
                os.mkdir(self.audio_output_folder)
            myobj.save(f"{self.audio_output_folder}/{argfilename}.mp3")
            # Playing the converted file
            #os.system("mpg321 welcome.mp3")
            worked = True
            error = None
        except Exception as ex:
            worked = False
            error = f"{type(ex)} - {ex}"
        return worked,error