-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaymusic.py
executable file
·48 lines (41 loc) · 1.88 KB
/
playmusic.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
import logging
import time
import os
from threading import Thread, Event
from respeaker import Microphone
from respeaker.bing_speech_api import BingSpeechAPI
# use madplay to play mp3 file
os.system('madplay')
# get a key from https://www.microsoft.com/cognitive-services/en-us/speech-api
BING_KEY = '3560976f779d4095a7109bc55a3b0c79'
def task(quit_event):
mic = Microphone(quit_event=quit_event)
bing = BingSpeechAPI(key=BING_KEY)
while not quit_event.is_set():
if mic.wakeup('respeaker'):
print('Wake up')
data = mic.listen()
try:
text = bing.recognize(data)
if text:
print('Recognized %s' % text)
if 'play music' in text:
print('I will play music!')
os.system('madplay Tchaikovsky_Concerto_No.1p.mp3')
except Exception as e:
print(e.message)
def main():
logging.basicConfig(level=logging.DEBUG)
quit_event = Event()
thread = Thread(target=task, args=(quit_event,))
thread.start()
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
print('Quit')
quit_event.set()
break
thread.join()
if __name__ == '__main__':
main()