Created
March 3, 2019 15:10
-
-
Save Aldiwildan77/9dbba6ffe578cb54d27eaea0db8c4a86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import speech_recognition as sr | |
import pyttsx3 as lst | |
import winsound | |
from colorama import init | |
# Created by "Muhammad Wildan Aldiansyah" | |
# Currently on development | |
# Simple Text to Speech, especially imitate your voice | |
init() # Colorama init.. | |
engine = lst.init() # Declare Text to Speech object as Engine | |
class colors: | |
HEADER = '\033[95m' | |
BLUE = '\033[94m' | |
GREEN = '\033[92m' | |
YELLOW = '\033[93m' | |
RED = '\033[91m' | |
ENDC = '\033[0m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
class languages: | |
lg = ["id-ID", "de-DE", "en-GB", "en-US", "jv-ID"] # List of available languages | |
number = 0 | |
# Indonesia | |
# Germany | |
# English British | |
# English US | |
# Javanese | |
def choose_language(self): # Method for return languages | |
return self.lg[self.number] | |
lang = languages() # Declare languages | |
values = str(lang.choose_language()) # Get string formatted language as values | |
def beeping(): | |
frequency = 2500 # Set Frequency 2500 Hz | |
duration = 500 # Set Duration to 500 ms == 0.5 second | |
winsound.Beep(frequency, duration) # Windows start to beeping as Frequency and Duration | |
def text_to_speech(text): | |
engine.say(text) # Text to speech do say something as text | |
engine.runAndWait() # Runs an event loop till all queued completed | |
def setting_rate_speech(): | |
rate = engine.getProperty('rate') # Get default rate from engine | |
engine.setProperty('rate', rate) # Set property of rate for Text to speech | |
def input_text_from_speech(): | |
recog = sr.Recognizer() # Init recog as Speech Recognition's recognizer | |
with sr.Microphone() as source: | |
audio = recog.listen(source) # Get voice from microphone then save to audio as values | |
try: | |
text = recog.recognize_google(audio, language=values) # Get text from what you have been spoken | |
print('Said : {} ' . format(text)) # Print the formatted result of text by google recognizer | |
setting_rate_speech() | |
text_to_speech(text) | |
except: | |
beeping() | |
text = colors.RED + 'Said : Sorry can\'t recognize your voice' + colors.ENDC | |
print(text) | |
engine.stop() # Stop the engine and clearing all queued event | |
def main(): | |
condition = True | |
while condition: # Infinite loop for main method | |
input_text_from_speech() | |
condition = False | |
return main() | |
if __name__ == "__main__": | |
if(len(sys.argv) == 1): | |
print(colors.RED + 'Please input the argument. ex : python ez_listening.py 1, Exit..' + colors.ENDC) | |
exit | |
else: | |
if(sys.argv[1].isdigit()): | |
if(int(sys.argv[1]) >= 0 and int(sys.argv[1]) <= 4): | |
lang.number = int(sys.argv[1]) | |
txt = colors.HEADER + 'Language chosen is ' + lang.choose_language() | |
txt += "\n" + 'Speech Now ...' + colors.ENDC | |
txt += "\n---------------------------------------------------" | |
print(txt) | |
main() | |
else: | |
print(colors.RED + 'Only values between 0 - 4 are accepted, Exit..' + colors.ENDC) | |
exit | |
else: | |
print(colors.RED + 'Please input the correct argument only number will be accepted, Exit..' + colors.ENDC) | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment