-
Notifications
You must be signed in to change notification settings - Fork 0
/
pylator.py
68 lines (56 loc) · 1.69 KB
/
pylator.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
import speech_recognition as sr
from googletrans import Translator
import sys
import os
import pyttsx3
import playsound
import pydub
#gives us easy access to client root computer
sysroot = os.path.expanduser("~")
#easy file operation
def save(filename, content):
with open(filename, "rb")as f:
f.write(content)
def translate(lang,text):
#init the translator class
translator = Translator()
#translate the text with given arguments
translation = translator.translate(text, dest= lang)
translations = f"{translation.origin} --> {translation.text} "
return translations
#here we'll create the speechrecognition fuction
def recognize(audio, lang="en-US"):
#init the recognizer class
r = sr.Recognizer()
#opens input file
file_in = sr.AudioFile(audio)
#records input file as source
with file_in as source:
r.adjust_for_ambient_noise(source, duration=0.5)
audio_record = r.record(source)
#save result to output variable
output = r.recognize_google(audio_record, language = lang)
return output
def read(word):
#initialize the pyttsx3 init class
engine = pyttsx3.init()
engine.say(word)
engine.runAndWait()
engine.save_to_file(word, "output.mp3")
#playing audio files using playsound module
def play(audio_file):
playsound(audio_file)
def convert(audio_input):
src = audio_input
dst = f"{audio_input}.wav"
#convert mp3 files to wav file so speech recognition can work flawlessly
sound = pydub.AudioSegment.from_mp3(src)
sound.export(dst, format="wav")
def mic(lang="en-US"):
r = sr.Recognizer()
mic = sr.Microphone()
with mic as source:
r.adjust_for_ambient_noise(source, duration=0.5)
audio = r.listen(source)
results = r.recognize_google(audio, language = lang)
return results