Esempio n. 1
0
def main():
    parser = configargparse.ArgParser(
        default_config_files=CONFIG_FILES,
        description="Act on voice commands using Google's speech recognition")
    parser.add_argument('-I', '--input-device', default='default',
                        help='Name of the audio input device')
    parser.add_argument('-O', '--output-device', default='default',
                        help='Name of the audio output device')
    parser.add_argument('-T', '--trigger', default='gpio',
                        help='Trigger to use {\'clap\', \'gpio\'}')
    parser.add_argument('--cloud-speech', action='store_true',
                        help='Use the Cloud Speech API instead of the Assistant API')
    parser.add_argument('-L', '--language', default='en-US',
                        help='Language code to use for speech (default: en-US)')
    parser.add_argument('-l', '--led-fifo', default='/tmp/status-led',
                        help='Status led control fifo')
    parser.add_argument('-p', '--pid-file',
                        help='File containing our process id for monitoring')
    parser.add_argument('--audio-logging', action='store_true',
                        help='Log all requests and responses to WAV files in /tmp')
    parser.add_argument('--assistant-secrets',
                        default=os.path.expanduser('~/assistant.json'),
                        help='Path to client secrets for the Assistant API')
    parser.add_argument('--cloud-speech-secrets',
                        default=os.path.expanduser('~/cloud_speech.json'),
                        help='Path to service account credentials for the '
                        'Cloud Speech API')
    parser.add_argument('--trigger-sound', default=None,
                        help='Sound when trigger is activated (WAV format)')

    args = parser.parse_args()

    create_pid_file(args.pid_file)
    i18n.set_language_code(args.language, gettext_install=True)

    player = audio.Player(args.output_device)

    if args.cloud_speech:
        credentials_file = os.path.expanduser(args.cloud_speech_secrets)
        if not os.path.exists(credentials_file) and os.path.exists(OLD_SERVICE_CREDENTIALS):
            credentials_file = OLD_SERVICE_CREDENTIALS
        recognizer = speech.CloudSpeechRequest(credentials_file)
    else:
        credentials = try_to_get_credentials(
            os.path.expanduser(args.assistant_secrets))
        recognizer = speech.AssistantSpeechRequest(credentials)

    recorder = audio.Recorder(
        input_device=args.input_device, channels=1,
        bytes_per_sample=speech.AUDIO_SAMPLE_SIZE,
        sample_rate_hz=speech.AUDIO_SAMPLE_RATE_HZ)
    with recorder:
        do_recognition(args, recorder, recognizer, player)
Esempio n. 2
0
def main():
    parser = configargparse.ArgParser(
        default_config_files=CONFIG_FILES,
        description="Act on voice commands using Google's speech recognition")
    parser.add_argument('-I',
                        '--input-device',
                        default='default',
                        help='Name of the audio input device')
    parser.add_argument('-O',
                        '--output-device',
                        default='default',
                        help='Name of the audio output device')
    parser.add_argument('-T',
                        '--trigger',
                        default='gpio',
                        choices=['clap', 'gpio', 'ok-google'],
                        help='Trigger to use')
    parser.add_argument(
        '--cloud-speech',
        action='store_true',
        help='Use the Cloud Speech API instead of the Assistant API')
    parser.add_argument(
        '-L',
        '--language',
        default='en-US',
        help='Language code to use for speech (default: en-US)')
    parser.add_argument('-l',
                        '--led-fifo',
                        default='/tmp/status-led',
                        help='Status led control fifo')
    parser.add_argument('-p',
                        '--pid-file',
                        help='File containing our process id for monitoring')
    parser.add_argument(
        '--audio-logging',
        action='store_true',
        help='Log all requests and responses to WAV files in /tmp')
    parser.add_argument('--assistant-always-responds',
                        action='store_true',
                        help='Play Assistant responses for local actions.'
                        ' You should make sure that you have IFTTT applets for'
                        ' your actions to get the correct response, and also'
                        ' that your actions do not call say().')
    parser.add_argument('--assistant-secrets',
                        default=os.path.expanduser('~/assistant.json'),
                        help='Path to client secrets for the Assistant API')
    parser.add_argument('--cloud-speech-secrets',
                        default=os.path.expanduser('~/cloud_speech.json'),
                        help='Path to service account credentials for the '
                        'Cloud Speech API')
    parser.add_argument('--trigger-sound',
                        default=None,
                        help='Sound when trigger is activated (WAV format)')

    args = parser.parse_args()

    create_pid_file(args.pid_file)
    i18n.set_language_code(args.language, gettext_install=True)

    player = aiy.audio.get_player()

    if args.cloud_speech:
        credentials_file = os.path.expanduser(args.cloud_speech_secrets)
        if not os.path.exists(credentials_file) and os.path.exists(
                OLD_SERVICE_CREDENTIALS):
            credentials_file = OLD_SERVICE_CREDENTIALS
        recognizer = speech.CloudSpeechRequest(credentials_file)
    else:
        credentials = try_to_get_credentials(
            os.path.expanduser(args.assistant_secrets))
        recognizer = speech.AssistantSpeechRequest(credentials)

    status_ui = StatusUi(player, args.led_fifo, args.trigger_sound)

    # The ok-google trigger is handled with the Assistant Library, so we need
    # to catch this case early.
    if args.trigger == 'ok-google':
        if args.cloud_speech:
            print('trigger=ok-google only works with the Assistant, not with '
                  'the Cloud Speech API.')
            sys.exit(1)
        do_assistant_library(args, credentials, player, status_ui)
    else:
        recorder = aiy.audio.get_recorder()
        with recorder:
            do_recognition(args, recorder, recognizer, player, status_ui)
Esempio n. 3
0
def main():
    global mqttclient, Dplayer
    parser = configargparse.ArgParser(
        default_config_files=CONFIG_FILES,
        description="Act on voice commands using Google's speech recognition")
    parser.add_argument('-I',
                        '--input-device',
                        default='default',
                        help='Name of the audio input device')
    parser.add_argument('-O',
                        '--output-device',
                        default='default',
                        help='Name of the audio output device')
    parser.add_argument('-T',
                        '--trigger',
                        default='gpio',
                        help='Trigger to use {\'clap\', \'gpio\'}')
    parser.add_argument(
        '--cloud-speech',
        action='store_true',
        help='Use the Cloud Speech API instead of the Assistant API')
    parser.add_argument(
        '-L',
        '--language',
        default='en-US',
        help='Language code to use for speech (default: en-US)')
    parser.add_argument('-l',
                        '--led-fifo',
                        default='/tmp/status-led',
                        help='Status led control fifo')
    parser.add_argument('-p',
                        '--pid-file',
                        default=PID_FILE,
                        help='File containing our process id for monitoring')
    parser.add_argument(
        '--audio-logging',
        action='store_true',
        help='Log all requests and responses to WAV files in /tmp')
    parser.add_argument('--assistant-secrets',
                        help='Path to client secrets for the Assistant API')
    parser.add_argument('--cloud-speech-secrets',
                        help='Path to service account credentials for the '
                        'Cloud Speech API')

    args = parser.parse_args()

    create_pid_file(args.pid_file)
    i18n.set_language_code(args.language, gettext_install=True)

    player = audio.Player(args.output_device)
    Dplayer = player
    action.VolumeControl(None, 100).run(
        "max volume")  # start loud at the start (which is not very loud)
    blipsay(piblips + "Booting up 1 2 3 4 5")

    if args.cloud_speech:
        credentials_file = os.path.expanduser(args.cloud_speech_secrets)
        if not os.path.exists(credentials_file) and os.path.exists(
                OLD_SERVICE_CREDENTIALS):
            credentials_file = OLD_SERVICE_CREDENTIALS
        recognizer = speech.CloudSpeechRequest(credentials_file)
    else:
        credentials = try_to_get_credentials(
            os.path.expanduser(args.assistant_secrets))
        recognizer = speech.AssistantSpeechRequest(credentials)

    recorder = audio.Recorder(input_device=args.input_device,
                              channels=1,
                              bytes_per_sample=speech.AUDIO_SAMPLE_SIZE,
                              sample_rate_hz=speech.AUDIO_SAMPLE_RATE_HZ)

    # most is encapsulated here (then look for mqttclient.loop())
    if mqttbroker:
        mqttclient = paho.mqtt.client.Client(mqttbroker)  # global object

        def on_connect(client, userdata, flags, rc):
            print("on_connect", client, userdata, flags, rc)
            blipsay(piblips + "M Q T T Connected")

        def on_disconnect(client, userdata, rc):
            print("on_disconnect", client, userdata, rc)
            blipsay(piblips + "M Q T T Disconnected")

        def on_publish(client, userdata, mid):
            print("on_publish", client, userdata, mid)

        def on_message(client, userdata, message):
            print("on_message", client, message, message.topic,
                  message.payload)
            heardhistory.append(message.payload.decode())
            blipsay(message.payload)

        mqttclient.on_connect = on_connect
        mqttclient.on_publish = on_publish
        mqttclient.on_disconnect = on_disconnect
        mqttclient.on_message = on_message
        print("made and attempting to connect", mqttclient)
        mqttclient.connect(mqttbroker)
        mqttclient.subscribe(mqtttohear)

    # this function enters an infinite while loop
    with recorder:
        do_recognition(args, recorder, recognizer, player)