-
Notifications
You must be signed in to change notification settings - Fork 10
/
run.py
216 lines (177 loc) · 6.9 KB
/
run.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
# -*- coding: utf-8-*-
import sys, os, time, random
import re
import yaml
import argparse
import logging
from multiprocessing import Process, Queue, Pipe
import psutil
from lib.voice.baiduVoice import BaiduVoice
from lib.voice.snowboyVoice import SnowboyVoice
from lib.mic import RawTextMic,ArecordMic,PyAudioMic
import lib.appPath
import lib.util
from plugin.fm.doubanFM import DoubanFM
import plugin.fm.doubanFM
from lib.conversation import Conversation
from plugin.bootstrap import Bootstrap
from lib.gpio.servo import Servo
from lib.gpio.led import Led
from lib.gpio.manager import Manager as gpioManager
import signal
interrupted = False
def signal_handler(signal, frame):
global interrupted
interrupted = True
def interrupt_callback():
global interrupted
return interrupted
def run(robot_name="ROBOT",logger=None,args=None):
bootstrap_file = os.path.join(lib.appPath.CONFIG_PATH, 'bootstrap.yml')
if os.path.exists(bootstrap_file) is False:
logger.error("bootstrap file is not exists!")
return False
# Read config
logger.debug("Trying to read config file: '%s'", bootstrap_file)
try:
with open(bootstrap_file, "r") as f:
bootstrap_config = yaml.safe_load(f)
except OSError:
logger.error("Can't open config file: '%s'", bootstrap_file)
raise
#语音唤醒实例
try:
passive_stt_engine_tag = bootstrap_config['passive_stt_engine']
except KeyError:
passive_stt_engine_tag = 'snowboy'
logger.warning("passive_stt_engine not specified in profile, defaulting " + "to '%s'", passive_stt_engine_tag)
passive_stt_engine_class = lib.util.get_engine_by_tag(passive_stt_engine_tag)
#语音识别实例
try:
active_stt_engine_tag = bootstrap_config['active_stt_engine']
except KeyError:
active_stt_engine_tag = 'baidu-ai-voice'
logger.warning("active_stt_engine not specified in profile, defaulting " + "to '%s'", active_stt_engine_tag)
active_stt_engine_class = lib.util.get_engine_by_tag(active_stt_engine_tag)
#语音合成实例
try:
speak_engine_tag = bootstrap_config['speak_engine']
except KeyError:
speak_engine_tag = 'baidu-ai-voice'
logger.warning("speak_engine not specified in profile, defaulting " + "to '%s'", speak_engine_tag)
speak_engine_class = lib.util.get_engine_by_tag(speak_engine_tag)
#语音录入实例
passive_mic = RawTextMic() if args.debug else ArecordMic()
active_mic = RawTextMic() if args.debug else PyAudioMic()
#会话(指令)初始
conversation = Conversation(robot_name, passive_mic, active_mic,
speak_engine_class.get_instance(),
active_stt_engine_class.get_instance(),
passive_stt_engine_class.get_instance(),
bootstrap_config)
#开始交互
conversation.handleForever()
#插件引导初始
#bootstrap = Bootstrap(speak_engine_class.get_instance(), bootstrap_config)
'''
queue = Queue()
conversation_process = Process(target=conversation.handleForever,args=(interrupt_callback,queue,))
bootstrap_process = Process(target=bootstrap.query,args=(queue,))
conversation_process.start()
bootstrap_process.start()
conversation_process.join()
bootstrap_process.join()
'''
def debugMic(logger,args):
mic = ArecordMic()
active_mic = PyAudioMic()
passive_stt = SnowboyVoice.get_instance()
active_stt = BaiduVoice.get_instance()
mic.init_recording()
while True:
if interrupt_callback():
logger.debug("debug mic break.")
break
threshold, transcribed = mic.passiveListen("weedge",
transcribe_callback=passive_stt.transcribe)
if not transcribed or not threshold:
#logger.debug("Nothing has been said or transcribed.")
continue
#passive_stt.play(os.path.join(lib.appPath.DATA_PATH,"snowboy/resources/ding.wav"))
input = active_mic.activeListenToAllOptions(THRESHOLD=threshold,
speak_callback=active_stt.play,transcribe_callback=active_stt.transcribe)
logger.debug("input:%s",input)
if input:
active_stt.say(input)
mic.terminate()
def debugLedServo(logger,args):
print("debugLedServo")
gpioManager.shakeshake_blingbling(process_callback=None,
process_args=(logger,args),
shake_num=1,bling_num=100)
def debugServo(logger,args):
print("debugServo")
Servo.get_instance().rotate(2)
def debugDaemon(logger,args):
'''
#daemon servo
servo_son_processor = Process(target=lib.util.create_daemon, args=(Servo.get_instance().rotate,(1,)))
servo_son_processor.start()
servo_son_processor.join()
#daemon led
led_son_processor = Process(target=lib.util.create_daemon, args=(Led.get_instance().bling,(100,)))
led_son_processor.start()
led_son_processor.join()
time.sleep(30)
'''
print("debug daemon servo ")
lib.util.create_daemon(daemon_callback=debugServo,args=(logger,args))
print("debug daemon over")
def debugLed(logger,args):
print("debugLed")
print("-----led bling----")
Led.get_instance().bling(10)
print("-----led breath----")
Led.get_instance().breath(10)
if __name__ == '__main__':
# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
parser = argparse.ArgumentParser(description='doubanFM pi')
parser.add_argument('--debug', action='store_true',
help='Show debug messages')
parser.add_argument('--debugDoubanFmPlugin', action='store_true',
help='Show debug douban fm plugin messages')
parser.add_argument('--debugLed', action='store_true',
help='Show debug gpio led messages')
parser.add_argument('--debugServo', action='store_true',
help='Show debug gpio servo messages')
parser.add_argument('--debugLedServo', action='store_true',
help='Show debug gpio servo messages')
parser.add_argument('--debugDaemon', action='store_true',
help='Show debug create daemon porcessor messages')
parser.add_argument('--debugMic', action='store_true',
help='Show debug mic messages')
args = parser.parse_args()
logging.basicConfig(stream=sys.stdout)
logger = logging.getLogger("")
if args.debug:
logger.setLevel(logging.DEBUG)
if args.debugMic:
debugMic(logger,args)
exit()
if args.debugDaemon:
debugDaemon(logger,args)
exit()
if args.debugLedServo:
debugLedServo(logger,args)
exit()
if args.debugServo:
debugServo(logger,args)
exit()
if args.debugLed:
debugLed(logger,args)
exit()
if args.debugDoubanFmPlugin:
debugDoubanFm(logger,args)
exit()
run('weedge',logger,args)