forked from Melissa-AI/Melissa-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
142 lines (112 loc) · 3.72 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import sys
import time
import audioop
import tempfile
import wave
import yaml
import pyaudio
import speech_recognition as sr
from brain import brain
from GreyMatter import play_music
from GreyMatter.SenseCells.tts import tts
profile = open('profile.yaml')
profile_data = yaml.safe_load(profile)
profile.close()
# Functioning Variables
name = profile_data['name']
music_path = profile_data['music_path']
city_name = profile_data['city_name']
city_code = profile_data['city_code']
proxy_username = profile_data['proxy_username']
proxy_password = profile_data['proxy_password']
access_token = profile_data['twitter']['access_token']
access_token_secret = profile_data['twitter']['access_token_secret']
consumer_key = profile_data['twitter']['consumer_key']
consumer_secret = profile_data['twitter']['consumer_secret']
tts('Welcome ' + name + ', systems are now ready to run. How can I help you?')
# Thanks to Jasper for passive code snippet.
_audio = pyaudio.PyAudio()
def getScore(data):
rms = audioop.rms(data, 2)
score = rms / 3
return score
def fetchThreshold():
THRESHOLD_MULTIPLIER = 1.8
RATE = 16000
CHUNK = 1024
THRESHOLD_TIME = 1
stream = _audio.open(format=pyaudio.paInt16,channels=1,rate=RATE,input=True,frames_per_buffer=CHUNK)
frames = []
lastN = [i for i in range(20)]
for i in range(0, RATE / CHUNK * THRESHOLD_TIME):
data = stream.read(CHUNK)
frames.append(data)
lastN.pop(0)
lastN.append(getScore(data))
average = sum(lastN) / len(lastN)
stream.stop_stream()
stream.close()
THRESHOLD = average * THRESHOLD_MULTIPLIER
return THRESHOLD
def passiveListen():
THRESHOLD_MULTIPLIER = 1.8
RATE = 16000
CHUNK = 1024
THRESHOLD_TIME = 1
LISTEN_TIME = 300
stream = _audio.open(format=pyaudio.paInt16,
channels=1,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
frames = []
lastN = [i for i in range(30)]
for i in range(0, RATE / CHUNK * THRESHOLD_TIME):
data = stream.read(CHUNK)
frames.append(data)
lastN.pop(0)
lastN.append(getScore(data))
average = sum(lastN) / len(lastN)
THRESHOLD = average * THRESHOLD_MULTIPLIER
frames = []
didDetect = False
for i in range(0, RATE / CHUNK * LISTEN_TIME):
data = stream.read(CHUNK)
frames.append(data)
score = getScore(data)
if score > THRESHOLD:
didDetect = True
stream.stop_stream()
stream.close()
time.sleep(1)
tts('Yes?')
main()
if not didDetect:
print "No disturbance detected"
stream.stop_stream()
stream.close()
def main():
try:
if sys.argv[1] == '--text' or sys.argv[1] == '-t':
text_mode = True
speech_text = raw_input("Write something: ").lower().replace("'", "")
except IndexError:
text_mode = False
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
speech_text = r.recognize_google(audio).lower().replace("'", "")
print("Melissa thinks you said '" + speech_text + "'")
except sr.UnknownValueError:
print("Melissa could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
play_music.mp3gen(music_path)
brain(name, speech_text, music_path, city_name, city_code, proxy_username, proxy_password)
if text_mode:
main()
else:
passiveListen()
main()