def alexa_recognize(timestamp):

    dialog_request_id = helpers.generate_unique_id()

    transcription = ""

    try:
        filename = "audio_%s.wav" % timestamp
        fpath = "guided_data/alexa_wav/" + filename
        audio = open(fpath, 'rb')
        directives = client.send_audio_file(
            audio, dialog_request_id=dialog_request_id)

        success = False
        text = ""
        if directives:
            for j, directive in enumerate(directives):
                if directive.name == 'RenderTemplate':
                    payload = directive.payload
                    if ('textField' in payload.keys()):
                        text = payload['textField']
                        success = True
        else:
            print("Audio " + filename + " - Can't get response")

        if (success):
            transcription = text
        else:
            transcription = ""

    except Exception as e:
        pass

    return transcription
Example #2
0
def test_avs:
    '''Tests that a prerecorded sample produces a result.'''
    input_file = "sample_audio/alexa_what_time_is_it.wav"
    dialog_req_id = helpers.generate_unique_id()
    client = avs.connect_to_avs()

    outfiles = avs.send_rec_to_avs(input_file, client, dialog_req_id)

    for of in outfiles:
        print("playing:" + of)
        os.system('mpg321 '+ of)
Example #3
0
def test_incorrect:
    '''Tests that "bad input" produces an error message.'''
    input_file = "sample_audio/bad_recording.wav"
    dialog_req_id = helpers.generate_unique_id()
    client = avs.connect_to_avs()  

    convert_16bit(input_file)

    outfiles = avs.send_rec_to_avs(input_file, client, dialog_req_id)

    for of in outfiles:
        print("playing:" + of)
        os.system('mpg321 '+ of)
 def send_audio_file(self,
                     audio_file,
                     dialog_request_id=None,
                     distance_profile=constants.CLOSE_TALK,
                     audio_format=constants.PCM):
     dialog_request_id = dialog_request_id or helpers.generate_unique_id()
     with self.ping_manager.update_ping_deadline():
         headers = self.authentication_manager.get_headers()
         return self.connection_manager.send_audio_file(
             authentication_headers=headers,
             device_state=self.device_manager.get_device_state(),
             audio_file=audio_file,
             dialog_request_id=dialog_request_id,
             distance_profile=distance_profile,
             audio_format=audio_format,
         )
Example #5
0
def test_brad:
    '''Tests that a recording from Brad produces a result.'''
    input_file = "sample_audio/flip_a_coin.wav"
    dialog_req_id = helpers.generate_unique_id()
    client = avs.connect_to_avs()   
    
    temp_fname = "temp.wav"
    apply_low_pass_filter(input_file, temp_fname)
    stretch(temp_fname, f, speed)
    volume_adjust(temp_fname, 15)
    convert_16bit(temp_fname)

    outfiles = avs.send_rec_to_avs(temp_fname, client, dialog_req_id)

    for of in outfiles:
        print("playing:" + of)
        os.system('mpg321 '+ of)
 def generate_message_id():
     return helpers.generate_unique_id()
Example #7
0
client_id = os.getenv("CLIENT_ID")
client_secret = os.getenv("CLIENT_SECRET")
refresh_token = os.getenv("REFRESH_TOKEN")

BASE_URL_NORTH_AMERICA = 'alexa.na.gateway.devices.a2z.com'

if __name__ == '__main__':

    client = AlexaClient(client_id=client_id,
                         secret=client_secret,
                         refresh_token=refresh_token,
                         base_url=BASE_URL_NORTH_AMERICA)

    client.connect()  # authenticate and other handshaking steps

    dialog_request_id = helpers.generate_unique_id()

    translation_writer = open(
        "output/alexa_translation " + str(datetime.now()) + ".txt", "w+")

    data = "sr_alexa/alexa_data/"

    # for (dirpath, _, filenames) in os.walk(data):
    #     if (len(filenames) > 0):
    #         if not os.path.exists(dirpath):
    #             os.makedirs(dirpath)

    dirpath = "sr_alexa/alexa_data/tts_google/generated_speech/"
    for i in range(1, 20001):
        filename = "audio_" + str(i) + ".wav"
        fpath = os.path.join(dirpath, filename)
Example #8
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()