-
Notifications
You must be signed in to change notification settings - Fork 15
/
va_simple.py
executable file
·73 lines (61 loc) · 1.97 KB
/
va_simple.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
#!/usr/bin/env python3
import logging
logging.basicConfig(level=logging.INFO)
from enum import Enum
from nltools.asr import ASR
from nltools.pulserecorder import PulseRecorder
from nltools.vad import VAD
from nltools.tts import TTS
MODELDIR = '/opt/kaldi/model/kaldi-generic-en-tdnn_250'
VOLUME = 150
class Intent(Enum):
HELLO = 1
LIGHT = 2
RADIO = 3
print ("Initializing...")
radio_on = False
lights_on = False
asr = ASR(model_dir = MODELDIR)
rec = PulseRecorder (volume=VOLUME)
vad = VAD()
tts = TTS(engine="espeak", voice="en")
utt_map = {}
def add_utt (utterance, intent):
utt_map[utterance] = intent
add_utt("hello computer", Intent.HELLO)
add_utt("switch on the lights", Intent.LIGHT)
add_utt("switch off the lights", Intent.LIGHT)
add_utt("switch on the radio", Intent.RADIO)
add_utt("switch off the radio", Intent.RADIO)
rec.start_recording()
print ("Please speak. (CTRL-C to exit)")
while True:
samples = rec.get_samples()
audio, finalize = vad.process_audio(samples)
if not audio:
continue
user_utt, c = asr.decode(audio, finalize)
print ("\r%s " % user_utt, end='', flush=True)
if finalize:
print ()
intent = utt_map.get(user_utt, None)
if intent == Intent.HELLO:
resp = "Hello there!"
elif intent == Intent.LIGHT:
if lights_on:
resp = "OK, switching off the lights."
else:
resp = "OK, switching on the lights."
lights_on = not lights_on
elif intent == Intent.RADIO:
if radio_on:
resp = "OK, switching off the radio."
else:
resp = "OK, switching on the radio."
radio_on = not radio_on
if not intent:
continue
rec.stop_recording()
print (resp)
tts.say(resp)
rec.start_recording()