This demo program shows how to use pyttx3 library to transform a text into audio.
The Output:
The Code:
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 50 51 52 53 54 55 56 | import pyttsx3 from PyQt6.QtGui import * from PyQt6.QtCore import * from PyQt6.QtWidgets import * class SpeechRunnable(QRunnable): def __init__(self): QRunnable.__init__(self) def run(self): self.engine = pyttsx3.init() self.engine.setProperty('rate', 150) self.engine.setProperty('volume', 1) self.engine.say(self.chat_speech) self.engine.runAndWait() def say(self, text): if text != '': self.chat_speech = text QThreadPool.globalInstance().start(self) def stop(self): self.engine.stop() class Window(QWidget): def __init__(self): super(Window, self).__init__() self.runnable = None self.le = QTextEdit(self) self.le.setPlainText(text) self.le.setGeometry(5, 40, 890, 450) self.btnStart = QPushButton("start", self) self.btnStart.setGeometry(5, 5, 100, 30) self.btnStop = QPushButton("stop", self) self.btnStop.setGeometry(110, 5, 100, 30) self.btnStart.clicked.connect(self.onClickedStart) self.runnable = SpeechRunnable() self.btnStop.clicked.connect(self.runnable.stop) self.setGeometry(25, 45, 900, 500) self.setWindowTitle('Speak') def onClickedStart(self): otext=self.le.toPlainText() self.runnable.say(otext) def closeEvent(self, event): if self.runnable is not None: self.runnable.stop() QThread.msleep(100) #delay super(Window, self).closeEvent(event) |
No comments:
Post a Comment