/
demo.py
98 lines (73 loc) · 2.19 KB
/
demo.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
import os
import signal
import sys
import socket
import pyaudio
from bing_voice import *
from microphone import Microphone
from player import Player
from spi import SPI
try:
from creds import BING_KEY
except ImportError:
print(
'Get a key from https://www.microsoft.com/cognitive-services/en-us/speech-api and create creds.py with the key')
sys.exit(-1)
script_dir = os.path.dirname(os.path.realpath(__file__))
hi = os.path.join(script_dir, 'audio/hi.wav')
spi = SPI()
spi.write('offline\n')
bing = BingVoice(BING_KEY)
mission_completed = False
awake = False
pa = pyaudio.PyAudio()
mic = Microphone(pa)
player = Player(pa)
def check_internet(host="8.8.8.8", port=53, timeout=6):
"""
Host: 8.8.8.8 (google-public-dns-a.google.com)
OpenPort: 53/tcp
Service: domain (DNS/TCP)
"""
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except Exception as ex:
print ex.message
return False
while not check_internet(host="223.6.6.6"):
pass
spi.write('online\n')
def handle_int(sig, frame):
global mission_completed, mic
print('quit')
mission_completed = True
mic.interrupt(stop_listening=True, stop_recording=True)
signal.signal(signal.SIGINT, handle_int)
while not mission_completed:
if not awake:
if mic.recognize(keyword='hey respeaker'):
spi.write('wakeup\n')
awake = True
player.play(hi)
continue
data = b''.join(mic.listen())
if not data:
break
spi.write('wait\n')
# recognize speech using Microsoft Bing Voice Recognition
try:
text = bing.recognize(data, language='en-US')
spi.write('answer\n')
print('Bing:' + text.encode('utf-8'))
tts_data = bing.synthesize('you said ' + text)
player.play_raw(tts_data)
except UnknownValueError:
print("Microsoft Bing Voice Recognition could not understand audio")
except RequestError as e:
print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))
spi.write('sleep\n')
awake = False
mic.close()
spi.write('sleep\n')