Skip to content

Commit

Permalink
Format the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanways committed Jan 17, 2024
1 parent 3b5d6e6 commit 3519174
Showing 1 changed file with 68 additions and 60 deletions.
128 changes: 68 additions & 60 deletions commbase_tts_pyttsx3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
################################################################################
# commbase_tts_pyttsx3.py #
# #
# Conversational AI Assistant and AI Hub for Computers and Droids #
# Command-line application that transforms written text into spoken words #
# #
# Change History #
# 06/10/2023 Esteban Herrera Original code. #
Expand Down Expand Up @@ -36,77 +36,85 @@

# Requirements
import argparse
import fileinput
import os.path
import pyttsx3
import sys


class TextToSpeech:
def __init__(self):
self.engine = None
self.voices = None
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 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.
"""
self.engine.say(text)
self.engine.runAndWait()
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!")
return None
except IOError:
print("An error occurred while reading the file!")
return None
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()
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)
self.set_up_text_to_speech(args.rate, args.voice_index)

file_content = ''
for line in sys.stdin:
file_content += line
file_content = ""
for line in sys.stdin:
file_content += line

if file_content:
self.talk(file_content)
else:
print("No input text provided.")
if file_content:
self.talk(file_content)
else:
print("No input text provided.")


if __name__ == '__main__':
if __name__ == "__main__":
tts = TextToSpeech()
tts.main()

0 comments on commit 3519174

Please sign in to comment.