Exemplo 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)
Exemplo n.º 2
0
    def record_button(self):
        """
        Records high quality audio files on the artist side.
        :return:
        """
        # Toggle recording
        rec_state = self.app.get_own_state()['recording']
        self.app.get_own_state()['recording'] = not rec_state

        if self.app.get_own_state()['recording']:
            # Update state
            self.app.chat_client.send_sync(constants.SYNC_START_RECORDING)
            # GUI update
            self.ids.record_button.source = SessionScreen.stop_theme
            # Start the progress effect
            progress_thread = threading.Thread(target=self.record_progress)
            progress_thread.start()

            filename = self.app.config.get_file_name(self.app.session_name,
                                                     datetime.now().strftime(constants.DATETIME_HQ))
            head, tail = os.path.split(filename)
            print "Creating: " + tail
            # Make sure folders exist
            if not os.path.exists(head):
                os.makedirs(head)
            # Makes a Recorder with the desired filename
            self.app.recorder = audio.Recorder(filename)
            # Add available audio file to state list
            self.app.add_audio_file(filename)
            # Starts writing to an audio file, including to disk
            self.app.recorder.start()  # Starts recording
            print "Recording..."
        else:
            # Update state
            self.app.chat_client.send_sync(constants.SYNC_STOP_RECORDING)
            # GUI update
            self.ids.record_button.source = SessionScreen.record_red
            # Closes recording threads
            self.app.recorder.stop()

            self.app.phone.stop_start_recording(datetime.now().strftime(constants.DATETIME_LQ))
            self.add_clip()  # adds to gui sidebar

            # Send a sync message for when a clip is available
            available_filename = self.app.get_latest_audio_file()
            _, tail = os.path.split(available_filename)
            self.app.chat_client.send_sync(constants.SYNC_FILE_AVAILABLE,
                                           filename=tail,
                                           length=audio.get_length(available_filename))
Exemplo n.º 3
0
    new_colors = [colorsys.hsv_to_rgb(h, 1.0, 1.0) for h in new_hues]
    old_cols = copy.deepcopy(new_colors)
    for i in xrange(len(new_colors)):
        if i not in [i1, i2]:
            new_colors[i] = (0.0, 0.0, 0.0)
    vis.state = new_colors
    if not disp:
        print new_colors
    vis.write_state()
    return scale, i1, i2, old_cols


if __name__ == '__main__':
    disp = True
    vis = comms.Visualizer(debug=False)
    rec = audio.Recorder()
    fps = 106.0
    spf = 1.0 / fps
    i1 = 0
    i2 = 7
    old_cols = [(0.0, 0.0, 0.0), (0.0, 0.0, 0.0), (0.0, 0.0, 0.0),
                (0.0, 0.0, 0.0), (0.0, 0.0, 0.0), (0.0, 0.0, 0.0),
                (0.0, 0.0, 0.0), (0.0, 0.0, 0.0)]
    if disp:
        display = ui.Display()
    while True:
        buff = ''
        sys.stdin.flush()
        while len(buff) < 100:
            buff += sys.stdin.read(1)
        #if len(buff)>100:
Exemplo n.º 4
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 = 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)

    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 = 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, status_ui)
Exemplo n.º 5
0
    if var_energy == 0.0:
        return False
    #if not disp:
    #    print var_energy,var_e/var_energy,var_prev/var_energy
    if ((var_prev / var_energy) < thresh) and ((var_e / var_energy) > thresh):
        if not disp:
            print True
        return True
    else:
        return False


if __name__ == '__main__':
    disp = True
    vis = comms.Visualizer(debug=False)
    rec = audio.Recorder(sample_size=1000)
    fps = 106.0
    spf = 1.0 / fps
    bar_i = 1
    direc = 1
    n_energies = 30
    energies = [0.0 for i in xrange(n_energies)]
    if disp:
        display = ui.Display()
    while True:
        time_0 = time.time()
        buff = ''
        sys.stdin.flush()
        while len(buff) < 1000:
            buff += sys.stdin.read(1)
        #if len(buff)>100:
Exemplo n.º 6
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)