forked from alexa-pi/AlexaPiDEPRECATED
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
executable file
·675 lines (602 loc) · 25.4 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#!/usr/bin/env python
import logging
import os
import random
import time
import RPi.GPIO as GPIO
import pyaudio
import wave
from creds import *
import requests
import json
import re
from memcache import Client
import vlc
import threading
import cgi
import email
import optparse
import getch
import sys
import fileinput
import datetime
import snowboydecoder
import snowboydetect
import sys
import signal
import tunein
import webrtcvad
# from pocketsphinx.pocketsphinx import *
# from sphinxbase.sphinxbase import *
# record format
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 800
RECORD_SECONDS = 6
WAVE_OUTPUT_FILENAME = "recording.wav"
audio = pyaudio.PyAudio()
# Settings
button = 18 # GPIO Pin with button connected
plb_light = 24 # GPIO Pin for the playback/activity light
rec_light = 25 # GPIO Pin for the recording light
lights = [plb_light, rec_light] # GPIO Pins with LED's connected
device = "plughw:1" # Name of your microphone/sound card in arecord -L
# Get arguments
parser = optparse.OptionParser()
parser.add_option('-s', '--silent',
dest="silent",
action="store_true",
default=False,
help="start without saying hello"
)
parser.add_option('-d', '--debug',
dest="debug",
action="store_true",
default=False,
help="display debug messages"
)
cmdopts, cmdargs = parser.parse_args()
silent = cmdopts.silent
debug = cmdopts.debug
# Setup
recorded = False
servers = ["127.0.0.1:11211"]
mc = Client(servers, debug=1)
path = os.path.realpath(__file__).rstrip(os.path.basename(__file__))
interrupted = False
#################################pocketsphnix processs###########################
# # Sphinx setup
# trigger_phrase = "alexa"
#
# sphinx_data_path = "/root/pocketsphinx/"
# modeldir = sphinx_data_path + "/model/"
# datadir = sphinx_data_path + "/test/data"
#
# # PocketSphinx configuration
# config = Decoder.default_config()
#
# # Set recognition model to US
# config.set_string('-hmm', os.path.join(modeldir, 'en-us/en-us'))
# config.set_string('-dict', os.path.join(modeldir, 'en-us/cmudict-en-us.dict'))
#
# # Specify recognition key phrase
# config.set_string('-keyphrase', trigger_phrase)
# config.set_float('-kws_threshold', 1e-5)
#
# # Hide the VERY verbose logging information
# config.set_string('-logfn', '/dev/null')
#
# # Process audio chunk by chunk. On keyword detected perform action and restart search
# decoder = Decoder(config)
# decoder.start_utt()
#####################################################################################
# Variables
p = ""
nav_token = ""
streamurl = ""
streamid = ""
position = 0
audioplaying = False
button_pressed = False
start = time.time()
tunein_parser = tunein.TuneIn(5000)
vad = webrtcvad.Vad(2)
currVolume = 100
# constants
VAD_SAMPLERATE = 16000
VAD_FRAME_MS = 30
VAD_PERIOD = (VAD_SAMPLERATE / 1000) * VAD_FRAME_MS
VAD_SILENCE_TIMEOUT = 1000
VAD_THROWAWAY_FRAMES = 10
MAX_RECORDING_LENGTH = 6
MAX_VOLUME = 100
MIN_VOLUME = 30
#Snowboy setup
logging.basicConfig()
logger = logging.getLogger("snowboy")
logger.setLevel(logging.INFO)
TOP_DIR = os.path.dirname(os.path.abspath(__file__))
model = sys.argv[1]
RESOURCE_FILE = os.path.join(TOP_DIR, "resources/common.res")
DETECT_DING = os.path.join(TOP_DIR, "resources/ding.wav")
DETECT_DONG = os.path.join(TOP_DIR, "resources/dong.wav")
# Function for snowboy
def interrupt_callback():
global interrupted
return interrupted
if len(sys.argv) == 1:
print("Error: need to specify model name")
print("Usage: python demo.py your.model")
sys.exit(-1)
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def internet_on():
print("Checking Internet Connection...")
try:
r = requests.get('https://api.amazon.com/auth/o2/token')
print("Connection {}OK{}".format(bcolors.OKGREEN, bcolors.ENDC))
return True
except:
print("Connection {}Failed{}".format(bcolors.WARNING, bcolors.ENDC))
return False
def gettoken():
token = mc.get("access_token")
refresh = refresh_token
if token:
return token
elif refresh:
payload = {"client_id": Client_ID, "client_secret": Client_Secret, "refresh_token": refresh,
"grant_type": "refresh_token",}
url = "https://api.amazon.com/auth/o2/token"
r = requests.post(url, data=payload)
resp = json.loads(r.text)
mc.set("access_token", resp['access_token'], 3570)
return resp['access_token']
else:
return False
def alexa_speech_recognizer():
# https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/rest/speechrecognizer-requests
if debug: print("{}Sending Speech Request...{}".format(bcolors.OKBLUE, bcolors.ENDC))
# GPIO.output(plb_light, GPIO.HIGH)
url = 'https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize'
headers = {'Authorization': 'Bearer %s' % gettoken()}
d = {
"messageHeader": {
"deviceContext": [
{
"name": "playbackState",
"namespace": "AudioPlayer",
"payload": {
"streamId": "",
"offsetInMilliseconds": "0",
"playerActivity": "IDLE"
}
}
]
},
"messageBody": {
"profile": "alexa-close-talk",
"locale": "en-us",
"format": "audio/L16; rate=16000; channels=1"
}
}
with open(path + 'recording.wav') as inf:
files = [
('file', ('request', json.dumps(d), 'application/json; charset=UTF-8')),
('file', ('audio', inf, 'audio/L16; rate=16000; channels=1'))
]
r = requests.post(url, headers=headers, files=files)
process_response(r)
def alexa_getnextitem(nav_token):
# https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/rest/audioplayer-getnextitem-request
time.sleep(0.5)
if audioplaying == False:
if debug: print("{}Sending GetNextItem Request...{}".format(bcolors.OKBLUE, bcolors.ENDC))
# GPIO.output(plb_light, GPIO.HIGH)
url = 'https://access-alexa-na.amazon.com/v1/avs/audioplayer/getNextItem'
headers = {'Authorization': 'Bearer %s' % gettoken(), 'content-type': 'application/json; charset=UTF-8'}
d = {
"messageHeader": {},
"messageBody": {
"navigationToken": nav_token
}
}
r = requests.post(url, headers=headers, data=json.dumps(d))
process_response(r)
def alexa_playback_progress_report_request(requestType, playerActivity, streamid):
# https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/rest/audioplayer-events-requests
# streamId Specifies the identifier for the current stream.
# offsetInMilliseconds Specifies the current position in the track, in milliseconds.
# playerActivity IDLE, PAUSED, or PLAYING
if debug: print("{}Sending Playback Progress Report Request...{}".format(bcolors.OKBLUE, bcolors.ENDC))
headers = {'Authorization': 'Bearer %s' % gettoken()}
d = {
"messageHeader": {},
"messageBody": {
"playbackState": {
"streamId": streamid,
"offsetInMilliseconds": 0,
"playerActivity": playerActivity.upper()
}
}
}
if requestType.upper() == "ERROR":
# The Playback Error method sends a notification to AVS that the audio player has experienced an issue during playback.
url = "https://access-alexa-na.amazon.com/v1/avs/audioplayer/playbackError"
elif requestType.upper() == "FINISHED":
# The Playback Finished method sends a notification to AVS that the audio player has completed playback.
url = "https://access-alexa-na.amazon.com/v1/avs/audioplayer/playbackFinished"
elif requestType.upper() == "IDLE":
# The Playback Idle method sends a notification to AVS that the audio player has reached the end of the playlist.
url = "https://access-alexa-na.amazon.com/v1/avs/audioplayer/playbackIdle"
elif requestType.upper() == "INTERRUPTED":
# The Playback Interrupted method sends a notification to AVS that the audio player has been interrupted.
# Note: The audio player may have been interrupted by a previous stop Directive.
url = "https://access-alexa-na.amazon.com/v1/avs/audioplayer/playbackInterrupted"
elif requestType.upper() == "PROGRESS_REPORT":
# The Playback Progress Report method sends a notification to AVS with the current state of the audio player.
url = "https://access-alexa-na.amazon.com/v1/avs/audioplayer/playbackProgressReport"
elif requestType.upper() == "STARTED":
# The Playback Started method sends a notification to AVS that the audio player has started playing.
url = "https://access-alexa-na.amazon.com/v1/avs/audioplayer/playbackStarted"
r = requests.post(url, headers=headers, data=json.dumps(d))
if r.status_code != 204:
print("{}(alexa_playback_progress_report_request Response){} {}".format(bcolors.WARNING, bcolors.ENDC, r))
else:
if debug: print(
"{}Playback Progress Report was {}Successful!{}".format(bcolors.OKBLUE, bcolors.OKGREEN, bcolors.ENDC))
def process_response(r):
global nav_token, streamurl, streamid, currVolume, isMute
if debug: print("{}Processing Request Response...{}".format(bcolors.OKBLUE, bcolors.ENDC))
nav_token = ""
streamurl = ""
streamid = ""
if r.status_code == 200:
data = "Content-Type: " + r.headers['content-type'] + '\r\n\r\n' + r.content
msg = email.message_from_string(data)
for payload in msg.get_payload():
if payload.get_content_type() == "application/json":
j = json.loads(payload.get_payload())
if debug: print("{}JSON String Returned:{} {}".format(bcolors.OKBLUE, bcolors.ENDC, json.dumps(j)))
elif payload.get_content_type() == "audio/mpeg":
filename = path + "tmpcontent/" + payload.get('Content-ID').strip("<>") + ".mp3"
with open(filename, 'wb') as f:
f.write(payload.get_payload())
else:
if debug: print(
"{}NEW CONTENT TYPE RETURNED: {} {}".format(bcolors.WARNING, bcolors.ENDC,
payload.get_content_type()))
# Now process the response
if 'directives' in j['messageBody']:
if len(j['messageBody']['directives']) == 0:
if debug: print("0 Directives received")
GPIO.output(rec_light, GPIO.LOW)
GPIO.output(plb_light, GPIO.LOW)
for directive in j['messageBody']['directives']:
if directive['namespace'] == 'SpeechSynthesizer':
if directive['name'] == 'speak':
GPIO.output(rec_light, GPIO.LOW)
play_audio(path + "tmpcontent/" + directive['payload']['audioContent'].lstrip("cid:") + ".mp3")
for directive in j['messageBody']['directives']: # if Alexa expects a response
if directive[
'namespace'] == 'SpeechRecognizer': # this is included in the same string as above if a response was expected
if directive['name'] == 'listen':
if debug: print(
"{}Further Input Expected, timeout in: {} {}ms".format(bcolors.OKBLUE, bcolors.ENDC,
directive['payload'][
'timeoutIntervalInMillis']))
play_audio(path + 'beep.wav', 0, 100)
timeout = directive['payload']['timeoutIntervalInMillis'] / 116
# listen until the timeout from Alexa
silence_listener(timeout)
# now process the response
alexa_speech_recognizer()
elif directive['namespace'] == 'AudioPlayer':
# do audio stuff - still need to honor the playBehavior
if directive['name'] == 'play':
nav_token = directive['payload']['navigationToken']
for stream in directive['payload']['audioItem']['streams']:
if stream['progressReportRequired']:
streamid = stream['streamId']
playBehavior = directive['payload']['playBehavior']
if stream['streamUrl'].startswith("cid:"):
content = path + "tmpcontent/" + stream['streamUrl'].lstrip("cid:") + ".mp3"
else:
content = stream['streamUrl']
pThread = threading.Thread(target=play_audio,
args=(content, stream['offsetInMilliseconds']))
pThread.start()
elif directive['namespace'] == "Speaker":
# speaker control such as volume
if directive['name'] == 'SetVolume':
vol_token = directive['payload']['volume']
type_token = directive['payload']['adjustmentType']
if (type_token == 'relative'):
currVolume = currVolume + int(vol_token)
else:
currVolume = int(vol_token)
if (currVolume > MAX_VOLUME):
currVolume = MAX_VOLUME
elif (currVolume < MIN_VOLUME):
currVolume = MIN_VOLUME
if debug: print("new volume = {}".format(currVolume))
elif 'audioItem' in j['messageBody']: # Additional Audio Iten
nav_token = j['messageBody']['navigationToken']
for stream in j['messageBody']['audioItem']['streams']:
if stream['progressReportRequired']:
streamid = stream['streamId']
if stream['streamUrl'].startswith("cid:"):
content = path + "tmpcontent/" + stream['streamUrl'].lstrip("cid:") + ".mp3"
else:
content = stream['streamUrl']
pThread = threading.Thread(target=play_audio, args=(content, stream['offsetInMilliseconds']))
pThread.start()
return
elif r.status_code == 204:
GPIO.output(rec_light, GPIO.LOW)
for x in range(0, 3):
time.sleep(.2)
GPIO.output(plb_light, GPIO.HIGH)
time.sleep(.2)
GPIO.output(plb_light, GPIO.LOW)
if debug: print(
"{}Request Response is null {}(This is OKAY!){}".format(bcolors.OKBLUE, bcolors.OKGREEN, bcolors.ENDC))
else:
print("{}(process_response Error){} Status Code: {}".format(bcolors.WARNING, bcolors.ENDC, r.status_code))
r.connection.close()
GPIO.output(lights, GPIO.LOW)
for x in range(0, 3):
time.sleep(.2)
GPIO.output(rec_light, GPIO.HIGH)
time.sleep(.2)
GPIO.output(lights, GPIO.LOW)
def play_audio(file=DETECT_DING, offset=0, overRideVolume=0):
global currVolume
if (file.find('radiotime.com') != -1):
file = tuneinplaylist(file)
global nav_token, p, audioplaying
if debug: print("{}Play_Audio Request for:{} {}".format(bcolors.OKBLUE, bcolors.ENDC, file))
GPIO.output(plb_light, GPIO.HIGH)
i = vlc.Instance('--aout=alsa') # , '--alsa-audio-device=mono', '--file-logging', '--logfile=vlc-log.txt')
m = i.media_new(file)
p = i.media_player_new()
p.set_media(m)
mm = m.event_manager()
mm.event_attach(vlc.EventType.MediaStateChanged, state_callback, p)
audioplaying = True
if (overRideVolume == 0):
p.audio_set_volume(currVolume)
else:
p.audio_set_volume(overRideVolume)
p.play()
while audioplaying:
continue
GPIO.output(plb_light, GPIO.LOW)
def tuneinplaylist(url):
global tunein_parser
if (debug): print("TUNE IN URL = {}".format(url))
req = requests.get(url)
lines = req.content.split('\n')
nurl = tunein_parser.parse_stream_url(lines[0])
if (len(nurl) != 0):
return nurl[0]
return ""
def state_callback(event, player):
global nav_token, audioplaying, streamurl, streamid
state = player.get_state()
# 0: 'NothingSpecial'
# 1: 'Opening'
# 2: 'Buffering'
# 3: 'Playing'
# 4: 'Paused'
# 5: 'Stopped'
# 6: 'Ended'
# 7: 'Error'
if debug: print("{}Player State:{} {}".format(bcolors.OKGREEN, bcolors.ENDC, state))
if state == 3: # Playing
if streamid != "":
rThread = threading.Thread(target=alexa_playback_progress_report_request,
args=("STARTED", "PLAYING", streamid))
rThread.start()
elif state == 5: # Stopped
audioplaying = False
if streamid != "":
rThread = threading.Thread(target=alexa_playback_progress_report_request,
args=("INTERRUPTED", "IDLE", streamid))
rThread.start()
streamurl = ""
streamid = ""
nav_token = ""
elif state == 6: # Ended
audioplaying = False
if streamid != "":
rThread = threading.Thread(target=alexa_playback_progress_report_request,
args=("FINISHED", "IDLE", streamid))
rThread.start()
streamid = ""
if streamurl != "":
pThread = threading.Thread(target=play_audio, args=(streamurl,))
streamurl = ""
pThread.start()
elif nav_token != "":
gThread = threading.Thread(target=alexa_getnextitem, args=(nav_token,))
gThread.start()
elif state == 7:
audioplaying = False
if streamid != "":
rThread = threading.Thread(target=alexa_playback_progress_report_request, args=("ERROR", "IDLE", streamid))
rThread.start()
streamurl = ""
streamid = ""
nav_token = ""
def detect_button(channel):
global button_pressed
buttonPress = time.time()
button_pressed = True
if debug: print("{}Button Pressed! Recording...{}".format(bcolors.OKBLUE, bcolors.ENDC))
time.sleep(.5) # time for the button input to settle down
while (GPIO.input(button) == 0):
button_pressed = True
time.sleep(.1)
if time.time() - buttonPress > 10: # pressing button for 10 seconds triggers a system halt
play_audio(path + 'alexahalt.mp3')
if debug: print("{} -- 10 second putton press. Shutting down. -- {}".format(bcolors.WARNING, bcolors.ENDC))
os.system("halt")
if debug: print("{}Recording Finished.{}".format(bcolors.OKBLUE, bcolors.ENDC))
button_pressed = False
time.sleep(.5) # more time for the button to settle down
def silence_listener(triggeredbyvoice):
global button_pressed
# Reenable reading microphone raw data
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
framerec = []
# Buffer as long as we haven't heard enough silence or the total size is within max size
# thresholdSilenceMet = False
# frames = 0
# numSilenceRuns = 0
# silenceRun = 0
start = time.time()
play_audio(DETECT_DING)
print ("Start recording")
# #do not count first 10 frames when doing VAD
# while (frames < throwaway_frames): # VAD_THROWAWAY_FRAMES):
# data = stream.read(CHUNK, exception_on_overflow=False)
# frames = frames + 1
# framerec.append(data)
# now do VAD
if triggeredbyvoice:
for i in range(0, int(RATE/CHUNK * MAX_RECORDING_LENGTH)):
data = stream.read(CHUNK, exception_on_overflow=False)
framerec.append(data)
else:
while button_pressed or ((time.time() - start) < MAX_RECORDING_LENGTH):
data = stream.read(CHUNK, exception_on_overflow=False)
framerec.append(data)
# isSpeech = vad.is_speech(data, VAD_SAMPLERATE)
#
# if (isSpeech == False):
# silenceRun = silenceRun + 1
# print "0"
# else:
# silenceRun = 0
# numSilenceRuns = numSilenceRuns + 1
# print "1"
# only count silence runs after the first one
# (allow user to speak for total of max recording length if they haven't said anything yet)
# if (numSilenceRuns != 0) and ((silenceRun * VAD_FRAME_MS) > VAD_SILENCE_TIMEOUT):
# thresholdSilenceMet = True
GPIO.output(rec_light, GPIO.HIGH)
print ("End recording")
play_audio(path+'beep.wav')
if debug: play_audio(path+'beep.wav', 0, 100)
stream.close()
GPIO.output(rec_light, GPIO.LOW)
waveFile = wave.open(path+WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(framerec))
waveFile.close()
def start(triggerword=False):
global audioplaying, p, vad, button_pressed
if not triggerword:
GPIO.add_event_detect(button, GPIO.FALLING, callback=detect_button, bouncetime=100) # threaded detection of button press
while True:
record_audio = False
# Enable reading microphone raw data
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
framerec = []
start = time.time()
while record_audio == False:
time.sleep(.1)
if triggerword:
if audioplaying: p.stop()
start = time.time()
record_audio = True
elif button_pressed:
if audioplaying: p.stop
record_audio = True
#################### THIS IS POCKETSPHNIX PROCESS ################################
# # Process microphone audio via PocketSphinx, listening for trigger word
# while decoder.hyp() == None and button_pressed == False:
# # Read from microphone
# l, buf = inp.read()
# # Detect if keyword/trigger word was said
# decoder.process_raw(buf, False, False)
#
# # if trigger word was said
# if decoder.hyp() != None:
# if audioplaying: p.stop()
# start = time.time()
# record_audio = True
# play_audio(path + 'alexayes.mp3', 0)
# elif button_pressed:
# if audioplaying: p.stop()
# record_audio = True
#
# do the following things if either the button has been pressed or the trigger word has been said
if debug: print ("detected the edge, setting up audio")
#
# To avoid overflows close the microphone connection
stream.close()
# # clean up the temp directory
if debug == False: os.system("rm /root/AlexaPi/tmpcontent/*")
#
if debug: print "Starting to listen..."
if triggerword: silence_listener(True)
else: silence_listener(False)
if debug: print "Debug: Sending audio to be processed"
alexa_speech_recognizer()
# Now that request is handled restart audio decoding
redetect = snowboydecoder.HotwordDetector(model, sensitivity=0.5)
print('Ready to hear your voice')
redetect.start(interrupt_check=interrupt_callback,
sleep_time=0.03)
##############################################################################
def setup():
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(lights, GPIO.OUT)
GPIO.output(lights, GPIO.LOW)
while internet_on() == False:
print(".")
token = gettoken()
if token == False:
while True:
for x in range(0, 5):
time.sleep(.1)
GPIO.output(rec_light, GPIO.HIGH)
time.sleep(.1)
GPIO.output(rec_light, GPIO.LOW)
for x in range(0, 5):
time.sleep(.1)
GPIO.output(plb_light, GPIO.HIGH)
time.sleep(.1)
GPIO.output(plb_light, GPIO.LOW)
if (silent == False): play_audio(path + "hello.mp3")
def signal_handler(signal, frame):
global interrupted
interrupted = True
# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
if __name__ == "__main__":
setup()
start()