-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (53 loc) · 1.77 KB
/
main.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
import speech_recognition as sr
import webbrowser
import time
import playsound
import os
import random
from gtts import gTTS
from time import ctime
r = sr.Recognizer()
def record_audio(ask = False):
with sr.Microphone as source:
if ask:
atcwl_speak(ask)
audio = r.Listen(source)
try:
voice_data = r.recognize_google(audio)
except sr.UnknownValueError:
atcwl_speak('sorry, I did not get that')
except sr.RequestError:
atcwl_speak('sorry, My speach service is down')
return voice_data
def atcwl_speak(audio_string):
tts = gTTS(text=audio_string, lang='en')
r =random.randint(1, 10000000)
audio_file = 'audio-' + str(r) + '.mp3'
tts.save(audio_file)
playsound.playsound(audio_file)
print(audio_string)
os.remove(audio_file)
def respond(voice_data):
if 'what is your name' in voice_data:
atcwl_speak('My name is atcwl')
if 'what language were you coded in' in voice_data:
atcwl_speak('I was coded in Python and c plus plus for my body')
if 'what is the time' in voice_data:
atcwl_speak(ctime())
if 'search' in voice_data:
search = record_audio('what do you want to search for?')
url = 'https://google.com/search?q=' + search
webbrowser.get().open(url)
atcwl_speak('Here is what i found for ' + search)
if 'find location' in voice_data:
location = record_audio('what is the location?')
url = 'https://google.nl/maps/place/' + '&'
webbrowser.get().open(url)
atcwl_speak('Here is the location of ' + location)
if 'exit' in voice_data:
exit()
time.sleep(1)
atcwl_speak('How can i help you')
while 1:
voice_data = record_audio()
respond(voice_data)