-
Notifications
You must be signed in to change notification settings - Fork 0
/
commbase_tts_pyttsx3.py
executable file
·120 lines (107 loc) · 5.19 KB
/
commbase_tts_pyttsx3.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
################################################################################
# commbase_tts_pyttsx3.py #
# #
# Command-line application that transforms written text into spoken words #
# #
# Change History #
# 06/10/2023 Esteban Herrera Original code. #
# Add new history entries as needed. #
# #
# #
################################################################################
################################################################################
################################################################################
# #
# Copyright (c) 2022-present Esteban Herrera C. #
# stv.herrera@gmail.com #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
# commbase_tts_pyttsx3.py
# Utilizes text-to-speech capabilities to read out the content of a file or any
# input text provided.
# Requirements
import argparse
import pyttsx3
import sys
class TextToSpeech:
def __init__(self):
self.engine = None
self.voices = None
def set_up_text_to_speech(self, rate, voice_index):
"""
Initializes the text-to-speech engine, retrieves the available voices,
and sets properties for the engine's rate and voice.
"""
self.engine = pyttsx3.init()
self.voices = self.engine.getProperty("voices")
self.engine.setProperty("rate", rate)
self.engine.setProperty("voice", self.voices[voice_index].id)
def talk(self, text):
"""
Sets up the text-to-speech engine, utilizes it to speak out the provided
text, and ensures the speech synthesis is completed before proceeding.
"""
if not text:
print("No input text provided.")
self.engine.say(text)
self.engine.runAndWait()
def read_file(self, file_path):
"""
Attempts to read the contents of the specified file, handles potential
errors such as file not found or IO errors, and returns the file's
content if it is successfully read.
"""
try:
with open(file_path, "r") as file:
content = file.read()
return content
except FileNotFoundError:
print("File not found!")
raise # Raise the FileNotFoundError here
except IOError:
print("An error occurred while reading the file!")
raise # Raise the IOError here
def main(self):
"""
Serves as the entry point of the program.
This method is responsible for reading input text from either a file or
standard input, storing it in the file_content variable, and then
passing it to the talk() method for speech synthesis. If no input text
is provided, it displays an appropriate message.
"""
parser = argparse.ArgumentParser(description="Text-to-Speech with pyttsx3")
parser.add_argument(
"--rate", type=int, default=145, help="Voice speed", metavar="RATE"
)
parser.add_argument(
"--voice-index",
type=int,
default=0,
help="Index of the voice to use",
metavar="INDEX",
)
args = parser.parse_args()
self.set_up_text_to_speech(args.rate, args.voice_index)
file_content = ""
for line in sys.stdin:
file_content += line
if file_content:
self.talk(file_content)
else:
print("No input text provided.")
if __name__ == "__main__":
tts = TextToSpeech()
tts.main()