Пример #1
0
def main():
    src = Source(rate=16000, channels=6, device_name='hw:1')
    route = Route(channels=src.channels)

    def get_kws_callback(channel):
        def on_detected(keyword):
            print('detected @ {}'.format(channel))

        return on_detected

    kws = []
    for channel in range(2):
        k = KWS(model='snowboy', sensitivity=0.6)
        k.on_detected = get_kws_callback(channel)

        kws.append(k)

    sink = FileSink('c6.wav', rate=src.rate, channels=src.channels)

    src.pipeline(route, kws)
    src.link(sink)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #2
0
def main():
    src = Source(rate=16000, channels=4, frames_size=320)
    ch1 = ChannelPicker(channels=4, pick=1)
    kws = KWS()
    doa = DOA(rate=16000)

    src.link(ch1)
    ch1.link(kws)
    src.link(doa)

    def on_detected(keyword):
        position = doa.get_direction()
        pixels.wakeup(position)
        print('detected {} at direction {}'.format(keyword, position))

    kws.set_callback(on_detected)

    src.recursive_start()

    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            print('STOP')
            break

    src.recursive_stop()
Пример #3
0
def main():
    ard = serial.Serial('/dev/ttyACM0', 9600)
    src = Source(rate=16000, channels=8)
    ch1 = ChannelPicker(channels=8, pick=1)
    kws = KWS()
    doa = DOA(rate=16000)
    time.sleep(1)

    src.link(ch1)
    ch1.link(kws)
    src.link(doa)

    while True:

        def on_detected(keyword):
            direction = doa.get_direction()
            print('detected {} at direction {}'.format(keyword, ))
            if (direction >= 0) and (direction <= 180):
                ard.write(struct.pack('>B', direction))

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #4
0
def main():
    src = Source(rate=16000, channels=4, frames_size=320)
    ch0 = ChannelPicker(channels=4, pick=0)
    kws = KWS(model='snowboy', sensitivity=0.6, verbose=True)
    doa = DOA(rate=16000)

    src.link(ch0)
    ch0.link(kws)
    src.link(doa)

    def on_detected(keyword):
        position = doa.get_direction()
        pixels.wakeup(position)
        print('detected {} at direction {}'.format(keyword, position))

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #5
0
def main():
    src = Source(rate=16000, frames_size=320, channels=8)
    ch0 = ChannelPicker(channels=src.channels, pick=0)
    kws = KWS(model='snowboy', sensitivity=0.5)
    doa = DOA(rate=src.rate, chunks=20)

    src.pipeline(ch0, kws)

    src.link(doa)

    def on_detected(keyword):
        direction = doa.get_direction()
        print('detected {} at direction {}'.format(keyword, direction))
        pixel_ring.wakeup(direction)
        time.sleep(2)
        pixel_ring.off()

    kws.on_detected = on_detected

    is_quit = []

    def signal_handler(sig, frame):
        is_quit.append(True)
        print('quit')

    signal.signal(signal.SIGINT, signal_handler)

    src.pipeline_start()
    while not is_quit:
        time.sleep(1)

    src.pipeline_stop()
Пример #6
0
def main():
    logging.basicConfig(level=logging.DEBUG)

    src = Source(rate=16000, channels=8)
    ch1 = ChannelPicker(channels=8, pick=1)
    ns = NS(rate=16000, channels=1)
    kws = KWS(model='alexa')
    doa = DOA(rate=16000)
    alexa = Alexa()

    src.link(ch1)
    ch1.link(ns)
    ns.link(kws)
    kws.link(alexa)

    src.link(doa)

    def on_detected(keyword):
        logging.info('detected {} at direction {}'.format(
            keyword, doa.get_direction()))
        alexa.listen()

    kws.set_callback(on_detected)

    src.recursive_start()

    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #7
0
def main():
    src = Source(rate=16000, channels=8)
    ch1 = ChannelPicker(channels=8, pick=1)
    kws = KWS()

    filename = '4.kws.' + datetime.datetime.now().strftime(
        "%Y%m%d.%H:%M:%S") + '.wav'

    sink = FileSink(filename, channels=src.channels, rate=src.rate)

    src.link(sink)
    src.pipeline(ch1, kws)

    def on_detected(keyword):
        print('detected {}'.format(keyword))

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #8
0
def main():
    src = Source(rate=16000, channels=4)
    ch0 = ChannelPicker(channels=src.channels, pick=0)
    kws = KWS(model='snowboy', sensitivity=0.6, verbose=True)
    doa = DOA(rate=16000)

    src.link(ch0)
    ch0.link(kws)
    src.link(doa)

    def on_detected(keyword):
        print('detected {} at direction {}'.format(keyword,
                                                   doa.get_direction()))

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()

    # wait a second to allow other threads to exit
    time.sleep(1)
Пример #9
0
def main():
    logging.basicConfig(level=logging.DEBUG)

    src = Source(rate=16000, channels=8)
    ch1 = ChannelPicker(channels=8, pick=1)
    ns = NS(rate=16000, channels=1)
    kws = KWS(model='alexa')
    doa = DOA(rate=16000)

    src.link(ch1)
    ch1.link(ns)
    ns.link(kws)

    src.link(doa)

    def on_detected(keyword):
        direction = doa.get_direction()
        logging.info('detected {} at direction {}'.format(keyword, direction))
        pixels.wakeup(direction)

    kws.on_detected = on_detected

    src.recursive_start()

    while True:
        try:
            time.sleep(0.05)
            #direction = doa.get_direction()
            #logging.info('direction {}'.format(direction))
            #pixels.wakeup(direction)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #10
0
def main():
    import time
    import datetime
    from voice_engine.source import Source
    from voice_engine.channel_picker import ChannelPicker
    from voice_engine.file_sink import FileSink

    src = Source(channels=2, rate=48000, frames_size=48000)
    chx = ChannelPicker(channels=src.channels, pick=0)
    thd = THD(1000, src.rate)

    filename = '2.94dB1KHz.' + datetime.datetime.now().strftime("%Y%m%d.%H:%M:%S") + '.wav'

    sink = FileSink(filename, channels=src.channels, rate=src.rate)

    src.link(sink)
    src.pipeline(chx, thd)

    src.pipeline_start()

    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.pipeline_stop()
Пример #11
0
def main():
    src = Source(rate=16000, channels=8)
    ch1 = ChannelPicker(channels=8, pick=1)
    ns = NS(rate=16000, channels=1)
    kws = KWS()
    doa = DOA(rate=16000)

    src.link(ch1)
    ch1.link(ns)
    ns.link(kws)
    src.link(doa)

    def on_detected(keyword):
        print('detected {} at direction {}'.format(keyword,
                                                   doa.get_direction()))

    kws.set_callback(on_detected)

    src.recursive_start()

    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #12
0
def main():
    src = Source(rate=16000, frames_size=320, channels=4)
    ds = DelaySum(channels=4,
                  frames_size=src.frames_size,
                  max_offset=max_offset)
    kws = KWS()
    doa = DOA(rate=16000, chunks=20)

    src.link(ds)
    ds.link(kws)

    src.link(doa)

    def on_detected(keyword):
        direction = doa.get_direction()
        pixel_ring.wakeup(direction)
        print('detected {} at direction {}'.format(keyword, direction))

    kws.on_detected = on_detected

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            print('quit')
            break

    src.recursive_stop()
Пример #13
0
def main():
##    RESPEAKER_RATE = 16000
##    RESPEAKER_CHANNELS = 1
##    RESPEAKER_WIDTH = 2
##    CHUNK = 1024
##    RECORD_SECONDS = 2
    src = Source(rate=16000, channels=4, frames_size=320)
    ch1 = ChannelPicker(channels=4, pick=1)
    kws = KWS()
    doa = DOA(rate=16000)

    src.link(ch1)
    ch1.link(kws)
    src.link(doa)
    



    def on_detected(keyword):
        position = doa.get_direction()
        pixels.wakeup(position)
        print('detected {} at direction {}'.format(keyword, position))
        src.stream.start()
        print("* recording")
        frames = []
        for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)):
            data = stream.read(CHUNK)
            frames.append(data)
        print("* done recording")
        src.stream.stop()
        print("start to send to baidu")
        # audio_data should be raw_data
        text = baidu.server_api(generator_list(frames))
        if text:
            try:
                text = json.loads(text)
                for t in text['result']:
                    print(t)
            except KeyError: 
                print("get nothing")
        else:
            print("get nothing")
            
        
        

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #14
0
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle('Audio Measuring Experiment')
        self.resize(800, 500)
        self.cwidget = QWidget()
        self.setCentralWidget(self.cwidget)
        layout = QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        self.cwidget.setLayout(layout)
        self.plotwidget = pg.PlotWidget()
        layout.addWidget(self.plotwidget)

        self.plotwidget.setTitle(title="press 'space' to freeze/resume")

        self.plot = self.plotwidget.plot()
        self.plot.setPen((0, 255, 0))

        self.ff = pg.TextItem('fundamental frequency', anchor=(0, 0))
        self.plotwidget.addItem(self.ff)
        self.ff.setPos(100, 0)

        self.arrow = pg.ArrowItem(pos=(100, 0), angle=-45)
        self.plotwidget.addItem(self.arrow)

        plotitem = self.plotwidget.getPlotItem()
        # plotitem.hideAxis('left')
        # plotitem.hideAxis('bottom')

        self.freeze = False
        self.timer = pg.QtCore.QTimer()
        self.timer.timeout.connect(self.update)
        self.timer.start(50)

        self.src = Source(rate=48000, frames_size=4800)
        chx = ChannelPicker(channels=self.src.channels, pick=0)
        thd = THD(1000, self.src.rate)

        filename = '2.94dB1KHz.' + datetime.datetime.now().strftime(
            "%Y%m%d.%H:%M:%S") + '.wav'

        sink = FileSink(filename,
                        channels=self.src.channels,
                        rate=self.src.rate)

        def on_data(d):
            global data

            data = d

        thd.on_data = on_data

        self.src.link(sink)
        self.src.pipeline(thd)

        self.src.pipeline_start()
Пример #15
0
def main():
    src = Source(rate=16000, channels=4, frames_size=320)
    ch1 = ChannelPicker(channels=4, pick=1)
    kws = KWS(model='dad.pmdl')
    doa = DOA(rate=16000)

    src.link(ch1)
    ch1.link(kws)
    src.link(doa)

    def on_detected(keyword):
        position = doa.get_direction()
        pixels.wakeup(position)
        print('detected {} at direction {}'.format(keyword, position))

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #16
0
def main():
    src = Source(rate=16000, channels=2)
    route = Route(channels=src.channels)
    ns = NS(rate=16000, channels=1)

    def get_kws_callback(channel):
        def on_detected(keyword):
            print('detected @ {}'.format(channel))

        return on_detected

    kws = []
    for channel in range(2):
        k = KWS(model='snowboy', sensitivity=0.6, verbose=False)
        k.on_detected = get_kws_callback(channel)
        
        kws.append(k)

    # data flow between elements
    # ---------------------------
    # src -> route -> ns -> kws[0]
    #           \
    #           kws[1]
    src.link(route)
    route.link((ns, kws[1]))
    ns.link(kws[0])

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #17
0
def main():
    logging.basicConfig(level=logging.DEBUG)

    src = Source(rate=16000)
    ns = NS(rate=16000, channels=1)
    kws = KWS(model='alexa')
    alexa = Alexa()

    alexa.state_listener.on_listening = pixel_ring.listen
    alexa.state_listener.on_thinking = pixel_ring.think
    alexa.state_listener.on_speaking = pixel_ring.speak
    alexa.state_listener.on_finished = pixel_ring.off

    src.pipeline(ns, kws, alexa)

    def on_detected(keyword):
        logging.info('detected {}'.format(keyword))
        alexa.listen()

    kws.set_callback(on_detected)

    is_quit = []
    def signal_handler(signal, frame):
        is_quit.append(True)
        print('Quit')
    signal.signal(signal.SIGINT, signal_handler)

    src.pipeline_start()
    while not is_quit:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break
    src.pipeline_stop()
def main():
    src = Source(rate=16000, frames_size=320, channels=8)
    ec = EC(channels=src.channels, capture=0, playback=7)
    ns = NS(rate=src.rate, channels=1)
    kws = KWS()

    # data flow between elements
    # ---------------------------
    # src -> ec -> ns -> kws
    src.pipeline(ec, ns, kws)

    def on_detected(keyword):
        led.toggle()
        print('detected {}'.format(keyword))

    kws.on_detected = on_detected

    is_quit = []

    def signal_handler(sig, frame):
        is_quit.append(True)
        print('quit')

    signal.signal(signal.SIGINT, signal_handler)

    src.pipeline_start()
    while not is_quit:
        time.sleep(1)

    src.pipeline_stop()

    # wait a second to allow other threads to exit
    time.sleep(1)
    led.off()
Пример #19
0
def main():
    logging.basicConfig(level=logging.DEBUG)

    src = Source(rate=16000)
    ns = NS(rate=16000, channels=1)
    kws = KWS()
    alexa = Alexa(model='alexa')

    src.link(ns)
    ns.link(kws)
    kws.link(alexa)

    def on_detected(keyword):
        logging.info('detected {}'.format(keyword))
        alexa.listen()

    kws.set_callback(on_detected)

    src.recursive_start()

    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #20
0
Файл: bing.py Проект: wbaiyu/avs
def main():
    import time
    from voice_engine.channel_picker import ChannelPicker
    from voice_engine.kws import KWS
    from voice_engine.source import Source

    src = Source(channels=2)
    ch0 = ChannelPicker(channels=src.channels, pick=0)
    kws = KWS(model='snowboy', sensitivity=0.7)
    bing = Bing(BING_KEY)

    src.pipeline(ch0, kws, bing)

    def on_detected(keyword):
        print('detected {}'.format(keyword))
        bing.listen()

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #21
0
def main():
    import time
    from voice_engine.source import Source
    from voice_engine.kws import KWS

    src = Source(rate=16000, channels=2, frames_size=1600)
    route = Route(channels=src.channels)
    kws = []

    def gen(channel):
        def on_detected(keyword):
            print('detected at channel {}'.format(channel))

        return on_detected

    for ch in range(src.channels):
        k = KWS(sensitivity=0.5)
        k.set_callback(gen(ch))
        kws.append(k)

    src.pipeline(route, kws)

    src.pipeline_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.pipeline_stop()
Пример #22
0
def main():
    logging.basicConfig(level=logging.DEBUG)

    src = Source(rate=16000)
    kws = KWS(model='alexa')
    alexa = Alexa()

    alexa.state_listener.on_listening = pixels.listen
    alexa.state_listener.on_thinking = pixels.think
    alexa.state_listener.on_speaking = pixels.speak
    alexa.state_listener.on_finished = pixels.off

    def on_detected(keyword):
        logging.info('detected {}'.format(keyword))
        alexa.listen()

    kws.on_detected = on_detected

    src.link(kws)
    kws.link(alexa)

    src.recursive_start()

    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #23
0
def main():
    src = Source(rate=16000, channels=2)
    ch1 = ChannelPicker(channels=2, pick=1)
    ns = NS(rate=16000, channels=1)
    kws = KWS(model='snowboy', sensitivity=0.6, verbose=True)

    src.pipeline(ch1, ns, kws)

    def on_detected(keyword):
        print('detected {}'.format(keyword))

    kws.set_callback(on_detected)

    src.pipeline_start()

    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.pipeline_stop()

    # wait a second to allow other threads to exit
    time.sleep(1)
Пример #24
0
def main():
    import time
    from voice_engine.source import Source

    src = Source()
    kws = KWS()

    src.link(kws)

    def on_detected(keyword):
        print('found {}'.format(keyword))

    kws.on_detected = on_detected

    kws.start()
    src.start()

    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    kws.stop()
    src.stop()
Пример #25
0
def t3_bot():
    src = Source(rate=16000, channels=4, frames_size=320)
    ch1 = ChannelPicker(channels=4, pick=1)
    kws = KWS()
    doa = DOA(rate=16000)

    src.link(ch1)
    ch1.link(kws)
    src.link(doa)
    pixels.listen()
    pwm.setPWM(0, 0, 370)
    pwm.setPWM(1, 0, 640)

    counter = 0

    def on_detected(keyword):
        position = doa.get_direction()
        pixels.wakeup(position)
        print('detected {} at direction {}'.format(keyword, position))
        if position >= 30 and position <= 180:
            pwm.setPWM(0, 0, 175)
            pwm.setPWM(1, 0, 500)
        elif position > 180 and position <= 330:
            pwm.setPWM(0, 0, 560)
            pwm.setPWM(1, 0, 500)
        elif position > 330 or position < 30:
            pwm.setPWM(0, 0, 370)
            pwm.setPWM(1, 0, 6200)
        else:
            pwm.setPWM(0, 0, 370)
            pwm.setPWM(1, 0, 640)

        #talkassist.os.system("espeak 'may i help you'")
        print("How may I help you?")
        print("call google assistant here, delete this line.")

    kws.set_callback(on_detected)

    src.recursive_start()

    while True:
        try:
            time.sleep(1)
            counter += 1
            print("counter is at " + str(counter))
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Пример #26
0
def main():
    src = Source(rate=16000, frames_size=160, channels=2)
    ch0 = ChannelPicker(channels=src.channels, pick=0)
    ns = NS(rate=src.rate, channels=1)
    kws = KWS()
    doa = DOA(rate=16000, chunks=50)
    alexa = Alexa()

    alexa.state_listener.on_listening = pixel_ring.listen
    alexa.state_listener.on_thinking = pixel_ring.think
    alexa.state_listener.on_speaking = pixel_ring.speak
    alexa.state_listener.on_finished = pixel_ring.off

    # data flow between elements
    # ---------------------------
    # src -> ns -> kws -> alexa
    #    \
    #    doa
    src.pipeline(ch0, ns, kws, alexa)

    src.link(doa)

    def on_detected(keyword):
        direction = doa.get_direction()
        print('detected {} at direction {}'.format(keyword, direction))
        alexa.listen()
        pixel_ring.wakeup(direction)

    kws.on_detected = on_detected

    is_quit = []

    def signal_handler(sig, frame):
        is_quit.append(True)
        print('quit')

    signal.signal(signal.SIGINT, signal_handler)

    src.pipeline_start()
    while not is_quit:
        time.sleep(1)

    src.pipeline_stop()
    pixel_ring.off()

    # wait a second to allow other threads to exit
    time.sleep(1)
Пример #27
0
    def __init__(self, wake_word_model):

        self.__src = Source(rate=16000)
        self.__ns = NS(rate=16000, channels=1)
        self.__kws = KWS(model=wake_word_model)
        self.__alexa = Alexa()

        self.__src.link(self.__ns)
        self.__ns.link(self.__kws)
        self.__kws.link(self.__alexa)

        self.__pixels = ThreePixels()
        self.__button = Button()

        self.__alexa.state_listener.on_listening = self.on_listening
        self.__alexa.state_listener.on_thinking = self.on_thinking
        self.__alexa.state_listener.on_speaking = self.on_speaking
        self.__alexa.state_listener.on_finished = self.on_finished
Пример #28
0
    def __init__(self):
        self.init_pixelring()
        self.source = Source(rate=160000, frames_size=320, channels=8)
        self.ch0 = ChannelPicker(channels=self.source.channels, pick=0)
        self.kws = KWS(sensitivity=0.7)
        self.doa = DOA(rate=self.source.rate, chunks=20)

        self.kws.on_detected = self.kws_on_detected

        self.cmd_pub = rospy.Publisher('/respeaker/cmd', String, queue_size=1)
Пример #29
0
def main():
    src = Source(rate=16000, frames_size=320, channels=8)
    ec = EC(channels=src.channels, capture=0, playback=7)
    ns = NS(rate=src.rate, channels=1)
    kws = KWS()
    doa = DOA(rate=16000, chunks=20)
    alexa = Alexa()

    alexa.state_listener.on_listening = pixel_ring.listen
    alexa.state_listener.on_thinking = pixel_ring.think
    alexa.state_listener.on_speaking = pixel_ring.speak
    alexa.state_listener.on_finished = pixel_ring.off

    src.pipeline(ec, ns, kws, alexa)

    src.link(doa)

    def on_detected(keyword):
        direction = doa.get_direction()
        print('detected {} at direction {}'.format(keyword, direction))
        alexa.listen()
        pixel_ring.wakeup(direction)

    kws.on_detected = on_detected

    is_quit = []

    def signal_handler(sig, frame):
        is_quit.append(True)
        print('quit')

    signal.signal(signal.SIGINT, signal_handler)

    src.pipeline_start()
    while not is_quit:
        time.sleep(1)

    src.pipeline_stop()
    pixel_ring.off()

    # wait a second to allow other threads to exit
    time.sleep(1)
Пример #30
0
def main():
    doa = None
    # DoA
    #    src = Source(rate=16000, frames_size=320, channels=4, device_index=0)
    src = Source(rate=16000, frames_size=320, channels=4)
    doa = DOA(rate=16000, chunks=20)
    src.link(doa)
    src.recursive_start()

    # GPIO
    GPIO.output(led_gnd_pin, False)
    GPIO.output(led1_pin, True)
    GPIO.output(led2_pin, True)

    # Google Assistant
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--credentials',
                        type=existing_file,
                        metavar='OAUTH2_CREDENTIALS_FILE',
                        default=os.path.join(
                            os.path.expanduser('/home/pi/.config'),
                            'google-oauthlib-tool', 'credentials.json'),
                        help='Path to store and read OAuth2 credentials')
    args = parser.parse_args()
    with open(args.credentials, 'r') as f:
        credentials = google.oauth2.credentials.Credentials(token=None,
                                                            **json.load(f))

    with Assistant(credentials, "RPi3_Assistant") as assistant:
        for event in assistant.start():
            process_event(event, doa)
Пример #31
0
def main():
    ser = serial.Serial("/dev/ttyACM0", 9600)

    src = Source(rate=16000, channels=4, frames_size=320)
    ch1 = ChannelPicker(channels=4, pick=1)
    kws = KWS(model='dad.pmdl')
    doa = DOA(rate=16000)

    src.link(ch1)
    ch1.link(kws)
    src.link(doa)

    def on_detected(keyword):
        position = doa.get_direction()
        pixels.wakeup(position)
        print('detected {} at direction {}'.format(keyword, position))
        if position <= 90:
            ser.write(b"1000\n")
        elif position > 90 and position <= 180:
            ser.write(b"0100\n")
        elif position > 180 and position <= 270:
            ser.write(b"0010\n")
        else:
            ser.write(b"0001\n")

        vibrate_time = 0.5
        time.sleep(vibrate_time)
        ser.write(b"0000\n")

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
    ser.close()
def main():
    logging.basicConfig(level=logging.DEBUG)

    src = Source(rate=16000, channels=4) 
    ch1 = ChannelPicker(channels=4, pick=1)
    ns = NS(rate=16000, channels=1)
    kws = KWS(model='snowboy')
    doa = DOA(rate=16000)
    alexa = Alexa()

    alexa.state_listener.on_listening = pixels.listen
    alexa.state_listener.on_thinking = pixels.think
    alexa.state_listener.on_speaking = pixels.speak
    alexa.state_listener.on_finished = pixels.off

    def on_detected(keyword):
        direction = doa.get_direction()
        logging.info('detected {} at direction {}'.format(keyword, direction))
        pixels.wakeup(direction)
        alexa.listen()

    kws.on_detected = on_detected

    src.link(ch1)
    ch1.link(ns)
    ns.link(kws)
    kws.link(alexa)

    src.link(doa)

    src.recursive_start()

    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()