Exemplo n.º 1
0
def main(input_file, output_file, speed, debug=False):
    """
    Main control flow for Voice Assistant device.
    """
    GPIO.setmode(GPIO.BOARD)
    button = Button(17)
    button.hold_time = 2
    button.when_held = play_tutorial
    light = led.LED()
    # pull last saved speed from json
    with open('save_state.json', 'r') as saveFile:
        response = json.load(saveFile)
    speed = float(response['savedSpeed'])

    client = avs.connect_to_avs()
    dialog_req_id = [helpers.generate_unique_id()]
    audio_process = Processing(input_file, output_file, speed, 15)
    os.system("mpg321 audio_instrs/startup.mp3")

    # check if should play tutorial, requires holding for 2 sec
    time.sleep(5)

    if IN_TUTORIAL:
        print("hello in tutorial")
        time.sleep(78)

    if speed == 1:
        os.system("mpg321 " + menu_filenames[int(speed) - 1])
        light.flash(led.RED)
    elif speed == 2:
        os.system("mpg321 " + menu_filenames[int(speed) - 1])
        light.flash(led.GRN)
    else:
        os.system("mpg321 " + menu_filenames[int(speed) - 1])
        light.flash(led.BLUE)

    # reset hold time/when_held func to go to menu
    button.hold_time = 5
    button.when_held = partial(launch_menu, button, light, audio_process)

    try:
        while True:
            print("ready for input")
            light.change_color(led.GRN)

            # record from mic
            if input_file == "in.wav":
                button.wait_for_press()

                if button.is_pressed:
                    button.wait_for_release()

                if IN_MENU:
                    while IN_MENU:
                        pass
                    continue

                rec = Recording(input_file)
                light.change_color(led.BLU)
                rec.record(button)

            light.change_color(led.ALL)
            if debug:
                output_file = input_file
            else:
                audio_process.apply()

            # send to avs
            # outfiles = avs.send_rec_to_avs(output_file, client)
            outfiles = avs.send_rec_to_avs(output_file, client, dialog_req_id)

            # play back avs response
            light.change_color(led.PUR)
            if not outfiles:
                light.change_color(led.RED)
                os.system("mpg321 audio_instrs/alexa-noresponse.mp3")
                print("Error, no outfiles")
                time.sleep(1)

            for of in outfiles:
                print("playing: " + of)
                os.system("mpg321 " + of)

            if input_file == 'in.wav':
                print("Command completed! Waiting for new input!")
            else:
                light.interrupt()
                break

    except KeyboardInterrupt:
        light.interrupt()
Exemplo n.º 2
0
import pyaudio
import wave
import time
from recording import Recording
from gpiozero import Button

button = Button(17)
button.wait_for_press()

rec = Recording("out.wav")
rec.record(button)