Beispiel #1
0
class StateMachine:
    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

    def on_listening(self):
        self.__pixels.wakeup()

    def on_thinking(self):
        self.__pixels.think()

    def on_speaking(self):
        self.__pixels.speak()

    def on_finished(self):
        self.__pixels.off()

    def __on_detected(self, keyword):
        print('detected {}'.format(keyword))
        print('Alexa is listening!')
        self.__alexa.listen()

    def __pushed_button(self, channel):
        print('button pushed')
        print('Alexa is listening!')
        self.__alexa.listen()

    def start(self):
        print('starting voice engine')
        self.__kws.set_callback(self.__on_detected)
        Button.set_callback_on_rising(self.__pushed_button)
        self.__src.recursive_start()

    def stop(self):
        print('stopping voice engine')
        self.__src.recursive_stop()
        Button.clean_up()
        self.__pixels.off()
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()
Beispiel #3
0
def main():
    src = Source('/tmp/ec.output', rate=16000, channels=2)
    ch0 = ChannelPicker(channels=src.channels, pick=1)
    ns = NS()
    kws = KWS(model='alexa', sensitivity=0.7)
    alexa = Alexa()

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

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

    kws.set_callback(on_detected)

    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

    src.pipeline_start()

    is_quit = []

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

    signal.signal(signal.SIGINT, signal_handler)

    while src.is_active() and not is_quit:
        time.sleep(1)

    src.pipeline_stop()
Beispiel #4
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()
Beispiel #5
0
def main():
    import sys

    logging.basicConfig(level=logging.DEBUG)
    configuration_file = DEFAULT_CONFIG_FILE

    if len(sys.argv) < 2:
        if not os.path.isfile(configuration_file):
            print('Usage: {} [configuration.json]'.format(sys.argv[0]))
            print('\nIf configuration file is not provided, {} will be used'.
                  format(configuration_file))
            sys.exit(1)
    else:
        configuration_file = sys.argv[1]

    with open(configuration_file, 'r') as f:
        config = json.load(f)
        require_keys = ['product_id', 'client_id', 'client_secret']
        for key in require_keys:
            if not ((key in config) and config[key]):
                print('{} should include "{}"'.format(configuration_file, key))
                sys.exit(2)

            if not ('refresh_token' in config) and config['refresh_token']:
                print(
                    'Not "refresh_token" available. you should run `alexa-auth {}` first'
                    .format(configuration_file))
                sys.exit(3)

    audio = Audio()
    with Alexa(config, audio) as alexa:
        while True:
            if audio.wakeup():
                alexa.SpeechRecognizer.Recognize(audio).wait(60)
Beispiel #6
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()
Beispiel #7
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()
Beispiel #8
0
    def __init__(self,
                 avs_config_file: str = None,
                 audio_device: str = 'default',
                 audio_player: str = 'default',
                 **kwargs):
        """
        :param avs_config_file: AVS credentials file - default: ~/.avs.json. If the file doesn't exist then
            an instance of the AVS authentication service will be spawned. You can login through an Amazon
            account either in the spawned browser window, if available, or by opening http://your-ip:3000
            in the browser on another machine.

        :param audio_device: Name of the input audio device (default: 'default')
        :param audio_player: Player to be used for audio playback (default: 'default').
            Supported values: 'mpv', 'mpg123', 'gstreamer'
        """
        from avs.alexa import Alexa
        from avs.config import DEFAULT_CONFIG_FILE
        from avs.mic import Audio

        super().__init__(**kwargs)

        if not avs_config_file:
            avs_config_file = DEFAULT_CONFIG_FILE

        if not avs_config_file or not os.path.isfile(avs_config_file):
            from avs.auth import auth
            auth(None, avs_config_file)
            self.logger.warning(
                'Amazon Echo assistant credentials not configured. Open http://localhost:3000 '
                + 'to authenticate this client')

        self.audio_device = audio_device
        self.audio_player = audio_player
        self.audio = Audio(device_name=audio_device)
        self.alexa = Alexa(avs_config_file, audio_player=audio_player)
        self._ready = False

        self.alexa.state_listener.on_ready = self._on_ready()
        self.alexa.state_listener.on_listening = self._on_listening()
        self.alexa.state_listener.on_speaking = self._on_speaking()
        self.alexa.state_listener.on_thinking = self._on_thinking()
        self.alexa.state_listener.on_finished = self._on_finished()
        self.alexa.state_listener.on_disconnected = self._on_disconnected()

        self.audio.link(self.alexa)
        self.alexa.start()
Beispiel #9
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
Beispiel #10
0
def main():
    from avs.alexa import Alexa
    from avs.mic import Audio

    logging.basicConfig(level=logging.DEBUG)

    config = None if len(sys.argv) < 2 else sys.argv[1]

    audio = Audio()
    kws = KWS()
    alexa = Alexa(config)

    audio.link(kws)
    kws.link(alexa)

    def wakeup(keyword):
        if keyword.find('alexa') >= 0:
            alexa.listen()

    kws.set_callback(wakeup)

    alexa.start()
    kws.start()
    audio.start()

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

    alexa.stop()
    kws.stop()
    audio.stop()
Beispiel #11
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)
Beispiel #12
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)
def main():
    # src = Source(rate=16000, frames_size=320, channels=6)
    src = Source(rate=16000, frames_size=160, channels=6, device_name='hw:1')

    ch0 = ChannelPicker(channels=src.channels, pick=0)
    kws = KWS(model='snowboy', sensitivity=0.5)
    # doa = DOA(rate=src.rate, 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(ch0, kws, alexa)

    # src.link(doa)

    def on_detected(keyword):
        # direction = doa.get_direction()
        # print('detected {} at direction {}'.format(keyword, direction))
        print('detected {} '.format(keyword))
        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()
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()
def main():
    src = Source(rate=16000, frames_size=320, channels=8)
    # ds = DelaySum(channels=8, frames_size=src.frames_size, max_offset=max_offset)
    ch0 = ChannelPicker(channels=src.channels, pick=0)
    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.link(ch0)
    ch0.link(kws)
    kws.link(alexa)

    # src.link(doa)

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

    kws.on_detected = on_detected

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

    src.recursive_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(model='alexa')
    alexa = Alexa()

    alexa.state_listener.on_listening = led.on
    alexa.state_listener.on_finished = led.off

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

    def on_detected(keyword):
        led.on()
        print('detected {}'.format(keyword))
        alexa.listen()

    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()
    led.off()

    # wait a second to allow other threads to exit
    time.sleep(1)
Beispiel #17
0
def main():
    logging.basicConfig(level=logging.DEBUG)

    src = Source(rate=16000, channels=4, device_name='default')
    ds = DelaySum(channels=src.channels)
    ns = NS(rate=16000, channels=1)
    kws = KWS(model='alexa')
    alexa = Alexa()

    alexa.state_listener.on_listening = leds_on
#     alexa.state_listener.on_thinking = pixel_ring.think
#     alexa.state_listener.on_speaking = pixel_ring.speak
    alexa.state_listener.on_finished = leds_off

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

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

    kws.set_callback(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()
Beispiel #18
0
def main():
    config = None if len(sys.argv) < 2 else sys.argv[1]

    audio = Audio(frames_size=1600)
    kws = KWS()
    alexa = Alexa(config)

    def speak():
        pixel_ring.speak(10, 0)

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

    audio.link(kws)
    kws.link(alexa)

    def wakeup(keyword):
        if keyword.find('alexa') >= 0:
            alexa.listen()

    kws.set_callback(wakeup)

    alexa.start()
    kws.start()
    audio.start()

    is_quit = threading.Event()

    def signal_handler(signal, frame):
        print('Quit')
        is_quit.set()

    signal.signal(signal.SIGINT, signal_handler)

    while not is_quit.is_set():
        time.sleep(1)

    alexa.stop()
    kws.stop()
    audio.stop()
Beispiel #19
0
def main():
    from respeaker.pixel_ring import pixel_ring

    config = None if len(sys.argv) < 2 else sys.argv[1]

    audio = Audio(frames_size=1600)
    kws = KWS()
    alexa = Alexa(config)

    def speak():
        pixel_ring.speak(10, 0)

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

    audio.link(kws)
    kws.link(alexa)

    def wakeup(keyword):
        if keyword.find('alexa') >= 0:
            alexa.listen()

    kws.set_callback(wakeup)

    alexa.start()
    kws.start()
    audio.start()

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

    alexa.stop()
    kws.stop()
    audio.stop()
def main():
    src = Source(rate=16000, frames_size=320, channels=8)
    ch0 = ChannelPicker(channels=src.channels, pick=0)
    kws = KWS(model='alexa', sensitivity=0.8)
    pws = KWS(model='/usr/share/respeaker/snowboy/resources/nivi.pmdl')

    def pwstest():
        print('calling Personal function')
        exit()

    doa = DOA(rate=src.rate, 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(ch0, kws, alexa)

    src.pipeline(ch0, pws, doa)

    src.link(doa)

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

    kws.on_detected = on_detected
    #pws.on_detected = on_detected
    print('afterkws before pws')

    #is_quit = []

    def on_detected(keyword):
        direction = doa.get_direction()
        print('PWS Testing')
        print('detected {} at direction {}'.format(keyword, direction))
        pwstest()
        #alexa.listen()
        pixel_ring.wakeup(direction)
        print('Personal function working')

    pws.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()
def main():
    logging.basicConfig(level=logging.DEBUG)
    #logging.getLogger('avs.alexa').setLevel(logging.INFO)
    logging.getLogger('hpack.hpack').setLevel(logging.INFO)

    en = mraa.Gpio(12)
    if os.geteuid() != 0:
        time.sleep(1)
    en.dir(mraa.DIR_OUT)
    en.write(0)

    src = RespeakerdSource()
    alexa = Alexa()

    src.link(alexa)

    pixel_ring.think()

    def on_ready():
        global state
        print("===== on_ready =====\r\n")
        state = 'off'
        pixel_ring.off()
        src.on_cloud_ready()

    def on_listening():
        global state
        global last_dir
        print("===== on_listening =====\r\n")
        if state != 'detected':
            print('The last dir is {}'.format(last_dir))
            pixel_ring.wakeup(last_dir)
        state = 'listening'
        pixel_ring.listen()

    def on_speaking():
        global state
        print("===== on_speaking =====\r\n")
        state = 'speaking'
        src.on_speak()
        pixel_ring.speak()

    def on_thinking():
        global state
        print("===== on_thinking =====\r\n")
        state = 'thinking'
        src.stop_capture()
        pixel_ring.think()

    def on_off():
        global state
        print("===== on_off =====\r\n")
        state = 'off'
        pixel_ring.off()

    def on_detected(dir, index):
        global state
        logging.info('detected at {}`'.format(dir))
        state = 'detected'
        pixel_ring.wakeup(dir + 300)
        next_dir = (dir + 60) % 360
        # alexa.listen()
        # set the direction to next beam
        src.on_set_direction(next_dir)

    alexa.state_listener.on_listening = on_listening
    alexa.state_listener.on_thinking = on_thinking
    alexa.state_listener.on_speaking = on_speaking
    alexa.state_listener.on_finished = on_off
    alexa.state_listener.on_ready = on_ready

    src.set_callback(on_detected)

    src.recursive_start()

    is_quit = threading.Event()

    def signal_handler(signal, frame):
        print('Quit')
        is_quit.set()

    signal.signal(signal.SIGINT, signal_handler)

    count = 0
    while not is_quit.is_set():
        try:
            time.sleep(1)
            count += 10
        except SyntaxError:
            pass
        except NameError:
            pass

    src.recursive_stop()

    en.write(1)
Beispiel #22
0
# -*- coding: utf-8 -*-

import time
import json
import datetime

from assistant import Assistant
import player

import yeelight
import gpiozero

from avs.alexa import Alexa

alexa = Alexa()

led = gpiozero.LED(5)
led.on()

lamp = yeelight.Bulb('192.168.0.104')

weekday = ('', '1', '2', '3', '4', '6', '天')


class Mirror(Assistant):
    def __init__(self, key=''):
        super(Mirror, self).__init__(key)

        self.set_keywords([
            '开灯', '关灯', '播放音乐', '几点了', '现在几点', '今天几号', '今天星期几', '暂停', '小呆',
            '今天的天气怎么样', '会下雨吗'
Beispiel #23
0
def main():
    logging.basicConfig(level=logging.DEBUG)
    #logging.getLogger('avs.alexa').setLevel(logging.INFO)
    logging.getLogger('hpack.hpack').setLevel(logging.INFO)

    if os.geteuid() != 0:
        time.sleep(1)

    src = RespeakerdSource()
    alexa = Alexa()
    ctl = VolumeCtl()
    power = LED(5)
    power.on()
    src.link(alexa)

    pixel_ring.think()

    state = 'thinking'
    last_dir = 0

    def on_ready():
        global state
        print("===== on_ready =====\r\n")
        state = 'off'
        pixel_ring.off()
        src.on_cloud_ready()

    def on_listening():
        global state
        global last_dir
        print("===== on_listening =====\r\n")
        if state != 'detected':
            print('The last dir is {}'.format(last_dir))
            pixel_ring.wakeup(last_dir)
        state = 'listening'
        pixel_ring.listen()

    def on_speaking():
        global state
        print("===== on_speaking =====\r\n")
        state = 'speaking'
        src.on_speak()
        pixel_ring.speak()

    def on_thinking():
        global state
        print("===== on_thinking =====\r\n")
        state = 'thinking'
        src.stop_capture()
        pixel_ring.think()

    def on_off():
        global state
        print("===== on_off =====\r\n")
        state = 'off'
        pixel_ring.off()

    def on_detected(dir, index):
        global state
        global last_dir
        logging.info('detected hotword:{} at {}`'.format(index, dir))
        state = 'detected'
        last_dir = (dir + 360 - 60) % 360
        alexa.listen()
        pixel_ring.wakeup(last_dir)

    def on_vad():
        # when someone is talking
        # print("."),
        # sys.stdout.flush()
        pass

    def on_silence():
        # when it is silent
        pass

    alexa.state_listener.on_listening = on_listening
    alexa.state_listener.on_thinking = on_thinking
    alexa.state_listener.on_speaking = on_speaking
    alexa.state_listener.on_finished = on_off
    alexa.state_listener.on_ready = on_ready

    alexa.Speaker.CallbackSetVolume(ctl.setVolume)
    alexa.Speaker.CallbackGetVolume(ctl.getVolume)
    alexa.Speaker.CallbackSetMute(ctl.setMute)

    src.set_callback(on_detected)
    src.set_vad_callback(on_vad)
    src.set_silence_callback(on_silence)

    src.recursive_start()

    is_quit = threading.Event()

    def signal_handler(signal, frame):
        print('Quit')
        is_quit.set()

    signal.signal(signal.SIGINT, signal_handler)

    while not is_quit.is_set():
        try:
            time.sleep(1)
        except SyntaxError:
            pass
        except NameError:
            pass

    src.recursive_stop()
    power.off()
Beispiel #24
0
def main():
    logging.basicConfig(level=logging.DEBUG)
    #logging.getLogger('avs.alexa').setLevel(logging.INFO)
    logging.getLogger('hpack.hpack').setLevel(logging.INFO)

    en = mraa.Gpio(12)
    if os.geteuid() != 0:
        time.sleep(1)
    en.dir(mraa.DIR_OUT)
    en.write(0)

    src = RespeakerdSource()
    alexa = Alexa()

    src.link(alexa)

    pixel_ring.think()

    state = 'thinking'
    last_dir = 0

    def on_ready():
        global state
        print("===== on_ready =====\r\n")
        state = 'off'
        pixel_ring.off()
        src.on_cloud_ready()

    def on_listening():
        global state
        global last_dir
        print("===== on_listening =====\r\n")
        if state != 'detected':
            print('The last dir is {}'.format(last_dir))
            pixel_ring.wakeup(last_dir)
        state = 'listening'
        pixel_ring.listen()

    def on_speaking():
        global state
        print("===== on_speaking =====\r\n")
        state = 'speaking'
        src.on_speak()
        pixel_ring.speak()

    def on_thinking():
        global state
        print("===== on_thinking =====\r\n")
        state = 'thinking'
        src.stop_capture()
        pixel_ring.think()

    def on_off():
        global state
        print("===== on_off =====\r\n")
        state = 'off'
        pixel_ring.off()

    def on_detected(dir):
        global state
        global last_dir
        logging.info('detected at {}`'.format(dir))
        state = 'detected'
        last_dir = (dir + 360 - 60) % 360
        pixel_ring.wakeup(last_dir)
        alexa.listen()

    alexa.state_listener.on_listening = on_listening
    alexa.state_listener.on_thinking = on_thinking
    alexa.state_listener.on_speaking = on_speaking
    alexa.state_listener.on_finished = on_off
    alexa.state_listener.on_ready = on_ready

    src.set_callback(on_detected)

    src.recursive_start()

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

    src.recursive_stop()

    en.write(1)
Beispiel #25
0
class AssistantEchoPlugin(AssistantPlugin):
    """
    Amazon Echo/Alexa assistant plugin.

    In order to activate the Echo service on your device follow these steps:

        1. Install avs (``pip install git+https://github.com/BlackLight/avs.git``)
        2. Run the ``alexa-auth`` script. A local webservice will start on port 3000
        3. If a browser instance doesn't open automatically then head to http://localhost:3000
        4. Log in to your Amazon account
        5. The required credentials will be stored to ~/.avs.json

    Triggers:

        * :class:`platypush.message.event.assistant.ConversationStartEvent`
            when a new conversation starts
        * :class:`platypush.message.event.assistant.SpeechRecognizedEvent`
            when a new voice command is recognized
        * :class:`platypush.message.event.assistant.ConversationEndEvent`
            when a new conversation ends

    Requires:

        * **avs** (``pip install avs``)
    """
    def __init__(self,
                 avs_config_file: str = None,
                 audio_device: str = 'default',
                 audio_player: str = 'default',
                 **kwargs):
        """
        :param avs_config_file: AVS credentials file - default: ~/.avs.json. If the file doesn't exist then
            an instance of the AVS authentication service will be spawned. You can login through an Amazon
            account either in the spawned browser window, if available, or by opening http://your-ip:3000
            in the browser on another machine.

        :param audio_device: Name of the input audio device (default: 'default')
        :param audio_player: Player to be used for audio playback (default: 'default').
            Supported values: 'mpv', 'mpg123', 'gstreamer'
        """
        from avs.alexa import Alexa
        from avs.config import DEFAULT_CONFIG_FILE
        from avs.mic import Audio

        super().__init__(**kwargs)

        if not avs_config_file:
            avs_config_file = DEFAULT_CONFIG_FILE

        if not avs_config_file or not os.path.isfile(avs_config_file):
            from avs.auth import auth
            auth(None, avs_config_file)
            self.logger.warning(
                'Amazon Echo assistant credentials not configured. Open http://localhost:3000 '
                + 'to authenticate this client')

        self.audio_device = audio_device
        self.audio_player = audio_player
        self.audio = Audio(device_name=audio_device)
        self.alexa = Alexa(avs_config_file, audio_player=audio_player)
        self._ready = False

        self.alexa.state_listener.on_ready = self._on_ready()
        self.alexa.state_listener.on_listening = self._on_listening()
        self.alexa.state_listener.on_speaking = self._on_speaking()
        self.alexa.state_listener.on_thinking = self._on_thinking()
        self.alexa.state_listener.on_finished = self._on_finished()
        self.alexa.state_listener.on_disconnected = self._on_disconnected()

        self.audio.link(self.alexa)
        self.alexa.start()

    def _on_ready(self):
        def _callback():
            self._ready = True

        return _callback

    def _on_listening(self):
        def _callback():
            get_bus().post(ConversationStartEvent(assistant=self))

        return _callback

    def _on_speaking(self):
        def _callback():
            # AVS doesn't provide a way to access the response text
            get_bus().post(ResponseEvent(assistant=self, response_text=''))

        return _callback

    def _on_finished(self):
        def _callback():
            get_bus().post(ConversationEndEvent(assistant=self))

        return _callback

    def _on_disconnected(self):
        def _callback():
            self._ready = False

        return _callback

    def _on_thinking(self):
        def _callback():
            # AVS doesn't provide a way to access the detected text
            get_bus().post(SpeechRecognizedEvent(assistant=self, phrase=''))

        return _callback

    @action
    def start_conversation(self, **kwargs):
        """
        Programmatically start a conversation with the assistant
        """
        if not self._ready:
            raise RuntimeError('Echo assistant not ready')

        self.audio.start()
        self.alexa.listen()

    @action
    def stop_conversation(self):
        """
        Programmatically stop a running conversation with the assistant
        """
        self.audio.stop()
        self._on_finished()()