Skip to content

Commit 6b2af69

Browse files
committed
add examples
1 parent 08ba1f8 commit 6b2af69

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed

e1.kws.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
A example to show Noise Suppression (NS) and Keyword Spotting (KWS)
3+
4+
The keyword is "computer".
5+
6+
Requirements:
7+
snowboy - https://github.com/Kitt-AI/snowboy
8+
"""
9+
10+
11+
import time
12+
from voice_engine.source import Source
13+
from voice_engine.kws import KWS
14+
15+
16+
def main():
17+
src = Source(rate=16000, channels=1, device_name="default")
18+
kws = KWS(model='computer', sensitivity=0.6, verbose=True)
19+
20+
src.pipeline(kws)
21+
22+
def on_detected(keyword):
23+
print('\ndetected {}'.format(keyword))
24+
25+
kws.set_callback(on_detected)
26+
27+
src.pipeline_start()
28+
29+
while True:
30+
try:
31+
time.sleep(1)
32+
except KeyboardInterrupt:
33+
break
34+
35+
src.pipeline_stop()
36+
37+
38+
if __name__ == '__main__':
39+
main()

e2.ns_kws.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
A example to show Noise Suppression (NS) and Keyword Spotting (KWS)
3+
4+
The keyword is "computer".
5+
6+
Requirements:
7+
snowboy - https://github.com/Kitt-AI/snowboy
8+
python-webrtc-audio-processing - pip3 install webrtc-audio-processing
9+
"""
10+
11+
12+
import time
13+
from voice_engine.source import Source
14+
from voice_engine.kws import KWS
15+
from voice_engine.ns import NS
16+
17+
18+
def main():
19+
src = Source(rate=16000, channels=1, device_name="default")
20+
ns = NS(rate=16000, channels=1)
21+
kws = KWS(model='computer', sensitivity=0.6, verbose=True)
22+
23+
src.pipeline(ns, kws)
24+
25+
def on_detected(keyword):
26+
print('\ndetected {}'.format(keyword))
27+
28+
kws.set_callback(on_detected)
29+
30+
src.pipeline_start()
31+
32+
while True:
33+
try:
34+
time.sleep(1)
35+
except KeyboardInterrupt:
36+
break
37+
38+
src.pipeline_stop()
39+
40+
41+
if __name__ == '__main__':
42+
main()

e3.ds_ns_kws.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
A example to show delay&sum beamforming, Noise Suppression (NS) and Keyword Spotting (KWS)
3+
4+
The keyword is "computer".
5+
6+
Requirements:
7+
snowboy - https://github.com/Kitt-AI/snowboy
8+
python-webrtc-audio-processing - `pip3 install webrtc-audio-processing`
9+
"""
10+
11+
12+
import time
13+
import numpy as np
14+
15+
from voice_engine.element import Element
16+
from voice_engine.source import Source
17+
from voice_engine.kws import KWS
18+
from voice_engine.ns import NS
19+
20+
21+
class DelaySum(Element):
22+
'''a simplest delay&sum beamforming with zero delay'''
23+
24+
def __init__(self, channels=4):
25+
super(DelaySum, self).__init__()
26+
self.channels = channels
27+
28+
def put(self, data):
29+
data = np.fromstring(data, dtype='int16')
30+
bf = data[0::self.channels] / self.channels
31+
for ch in range(1, self.channels):
32+
bf += data[ch::self.channels] / self.channels
33+
super(DelaySum, self).put(bf.astype('int16').tostring())
34+
35+
36+
def main():
37+
src = Source(rate=16000, channels=4, device_name="default")
38+
ds = DelaySum(channels=src.channels)
39+
ns = NS(rate=16000, channels=1)
40+
kws = KWS(model='computer', sensitivity=0.6, verbose=True)
41+
42+
src.pipeline(ds, ns, kws)
43+
44+
def on_detected(keyword):
45+
print('\ndetected {}'.format(keyword))
46+
47+
kws.set_callback(on_detected)
48+
49+
src.pipeline_start()
50+
51+
while True:
52+
try:
53+
time.sleep(1)
54+
except KeyboardInterrupt:
55+
break
56+
57+
src.pipeline_stop()
58+
59+
60+
if __name__ == '__main__':
61+
main()

e4.ns_kws_alexa.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
Hands-free Voice Assistant with Snowboy and Alexa Voice Service. The wake word is "alexa"
3+
4+
Requirements:
5+
snowboy - https://github.com/Kitt-AI/snowboy
6+
python-webrtc-audio-processing - `pip3 install webrtc-audio-processing`
7+
avs - `pip3 install avs`
8+
"""
9+
10+
import os
11+
import signal
12+
import time
13+
import logging
14+
import numpy as np
15+
16+
from voice_engine.element import Element
17+
from voice_engine.source import Source
18+
from voice_engine.kws import KWS
19+
from voice_engine.ns import NS
20+
from avs.alexa import Alexa
21+
22+
23+
class DelaySum(Element):
24+
'''a simplest delay&sum beamforming with zero delay'''
25+
26+
def __init__(self, channels=4):
27+
super(DelaySum, self).__init__()
28+
self.channels = channels
29+
30+
def put(self, data):
31+
data = np.fromstring(data, dtype='int16')
32+
bf = data[0::self.channels] / self.channels
33+
for ch in range(1, self.channels):
34+
bf += data[ch::self.channels] / self.channels
35+
super(DelaySum, self).put(bf.astype('int16').tostring())
36+
37+
38+
def leds_on():
39+
os.system("mosquitto_pub -t '/voicen/leds/value' -m '0xf'")
40+
41+
def leds_off():
42+
os.system("mosquitto_pub -t '/voicen/leds/value' -m '0x0'")
43+
44+
def main():
45+
logging.basicConfig(level=logging.DEBUG)
46+
47+
src = Source(rate=16000, channels=4, device_name='capture')
48+
ds = DelaySum(channels=src.channels)
49+
ns = NS(rate=16000, channels=1)
50+
kws = KWS(model='alexa')
51+
alexa = Alexa()
52+
53+
alexa.state_listener.on_listening = leds_on
54+
# alexa.state_listener.on_thinking = pixel_ring.think
55+
# alexa.state_listener.on_speaking = pixel_ring.speak
56+
alexa.state_listener.on_finished = leds_off
57+
58+
src.link(ds)
59+
ds.link(ns)
60+
ns.link(kws)
61+
kws.link(alexa)
62+
63+
def on_detected(keyword):
64+
logging.info('\ndetected {}'.format(keyword))
65+
alexa.listen()
66+
67+
kws.set_callback(on_detected)
68+
69+
is_quit = []
70+
def signal_handler(sig, frame):
71+
is_quit.append(True)
72+
print('quit')
73+
signal.signal(signal.SIGINT, signal_handler)
74+
75+
src.pipeline_start()
76+
while not is_quit:
77+
time.sleep(1)
78+
79+
src.pipeline_stop()
80+
81+
82+
if __name__ == '__main__':
83+
main()

0 commit comments

Comments
 (0)