# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'text2sppech.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia
from gtts import gTTS
from PyQt5.QtMultimedia import QSound 
from playsound import playsound




class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.completeButton = QtWidgets.QPushButton(self.centralwidget)
        self.completeButton.setGeometry(QtCore.QRect(280, 520, 75, 23))
        self.completeButton.setObjectName("completeButton")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(20, 40, 751, 461))
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.completeButton.clicked.connect(self.speech)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.completeButton.setText(_translate("MainWindow", "Done"))

    def text2speech(self,text,file_name):
        language = 'en'
        myobj = gTTS(text=text, lang=language, slow=False)
        name = file_name
        print(name)
        print(type(text))
        myobj.save(name)

    def speech(self):
        mytext = self.textEdit.toPlainText()
        print(mytext)
        self.text2speech(mytext,'audio.wav')
        # filename = 'beal.wav'
        #QtMultimedia.QSound.play('audio.wav')
        filename = 'audio.wav'
        # QSound.play(filename)
        fullpath = QtCore.QDir.current().absoluteFilePath(filename)
        print(fullpath) 
        url = QtCore.QUrl.fromLocalFile(fullpath)
        print(url)
        content = QtMultimedia.QMediaContent(url)
        player = QtMultimedia.QMediaPlayer()
        player.setMedia(content)
        player.play()
        os.system("audio.wav")
        #playsound("audio.wav")
    


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
