-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
141 lines (121 loc) · 4.12 KB
/
main.py
File metadata and controls
141 lines (121 loc) · 4.12 KB
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
import time
import pvporcupine
from pvrecorder import PvRecorder
from playsound import playsound
import pvrhino
import subprocess
import random
from datetime import datetime
from suntime import Sun, SunTimeException
import config
import my_spotify
import battery_report
def play_response(sound: str):
if sound == 'evening' or sound == 'morning' or sound == 'afternoon':
playsound(
r'D:\Joi - project\Joi_sounds\Millie\day_greetings\{}{}.wav'.format(sound, random.randrange(0, 1))
)
elif sound == 'greet':
playsound(
r'D:\Joi - project\Joi_sounds\Millie\greetings\{}{}.wav'.format(sound, random.randrange(0, 4))
)
elif sound == 'done':
playsound(
r'D:\Joi - project\Joi_sounds\Millie\done\{}{}.wav'.format(sound, random.randrange(0, 4))
)
elif sound == 'battery_report':
playsound(
r'D:\Joi - project\Joi_sounds\Millie\battery_report\emergency power.wav'
)
elif sound == 'change_voice': # going to come back to this
pass
# testing should change it
elif sound == 'problem':
playsound(
r'D:\Joi - project\Joi_sounds\Millie\oops could not found it.wav'
)
def commands(command_intent: str, command: dict):
if command_intent == 'open_app':
play_response('done')
subprocess.Popen(r'D:\Joi - project\ahk_commands\{}.exe'.format(command['app_name']))
elif command_intent == 'song_control':
if 'status' in command:
if my_spotify.play_and_pause():
play_response('done')
else:
play_response('problem')
elif 'volume_percent' in command:
play_response('done')
my_spotify.set_volume(
int(command['volume_percent'][:-1])
)
elif 'command' in command:
play_response('done')
my_spotify.track_control(command['command'])
# # not sure to use
# def record_call_back(indata, frames, time, status):
# if status:
# print(status, file=sys.stderr)
# q.put(bytes(indata))
porcupine = pvporcupine.create(
access_key=config.ACCESS_KEY,
keyword_paths=[
r'D:\Joi - project\Wake commands\Listen-Joi_en_windows_v2_2_0.ppn',
r'D:\Joi - project\Wake commands\Ok-Joi_en_windows_v2_2_0.ppn'
]
)
recorder = PvRecorder(
device_index=config.MICROPHONE_INDEX,
frame_length=porcupine.frame_length
)
print(PvRecorder.get_audio_devices())
rhino = pvrhino.create(
access_key=config.ACCESS_KEY,
context_path=r'D:\Joi - project\opening_commands\joi_commands_rhino_en_windows_v2_2_0.rhn'
)
time_now = 0
# time vars
current_time = datetime.now().strftime('%H')
latitude = 40.390060
longitude = 71.790321
try:
sun = Sun(latitude, longitude)
today_sunset = sun.get_local_sunset_time().strftime('%H')
today_sunrise = sun.get_local_sunrise_time().strftime('%H')
except SunTimeException:
today_sunset = '19'
today_sunrise = '6'
SunTimeException('Could not get the sun times')
if int(today_sunrise) <= int(current_time) <= 12:
play_response('morning')
elif 12 <= int(current_time) <= int(today_sunset):
play_response('afternoon')
else:
play_response('evening')
try:
recorder.start()
print('Starting test')
while True:
pcm = recorder.read()
keyword_index = porcupine.process(pcm)
if keyword_index >= 0:
print(f'Detected {config.KEYWORDS[keyword_index]}')
play_response('greet')
time_now = time.time()
while time.time() - time_now <= 10:
pcm = recorder.read()
is_finalized = rhino.process(pcm)
if is_finalized:
inference = rhino.get_inference()
# print('inference', inference)
if inference.is_understood:
intent = inference.intent
slots = inference.slots
time_now = time.time()
print('intent', intent, 'slot', slots)
commands(intent, slots)
except KeyboardInterrupt:
recorder.stop()
finally:
porcupine.delete()
recorder.delete()