예제 #1
0
def start_recording():
    try:
        library_path = './lib/mac/x86_64/libpv_porcupine.dylib'
        model_file_path = './lib/common/porcupine_params.pv'
        keyword_file_paths = ['./keyword_files/mac/alexa_mac.ppn']
        sensitivities = [0.2]
        handle = porcupine.Porcupine(library_path,
                                    model_file_path,
                                    keyword_file_paths=keyword_file_paths,
                                    sensitivities=sensitivities)

        pa = pyaudio.PyAudio()
        audio_stream = pa.open(
                rate=handle.sample_rate,
                channels=1,
                format=pyaudio.paInt16,
                input=True,
                frames_per_buffer=handle.frame_length)

        print('Listening for keyword alexa...')
        silent_frames = 0
        while True:
            data = audio_stream.read(handle.frame_length, exception_on_overflow = False)
            pcm = struct.unpack_from("h" * handle.frame_length, data)
            sample_size = pa.get_sample_size(FORMAT)
            result = handle.process(pcm)
            if result:
                executed = False
                frames = []
                print('Keep Speaking...')
                while(silent_frames < 20):
                    frame = audio_stream.read(CHUNK, exception_on_overflow = False)
                    if(is_silent(frame)):
                        silent_frames += 1
                    frames.append(frame)
                    save_wav(sample_size, b''.join(frames))
                print('[%s] detected keyword' % str(datetime.now()))
                if(not(executed)):
                    speech_to_text.speech_to_text()
                    executed = True
            silent_frames = 0

    except KeyboardInterrupt:
        print('stopping ...')
    finally:
        if handle is not None:
            handle.delete()

        if audio_stream is not None:
            audio_stream.close()

        if pa is not None:
            pa.terminate()
예제 #2
0
import commands
import porcupine as Porcupine
import struct
import time
import pyaudio

library_path = 'lib\\windows\\amd64\\libpv_porcupine.dll'
model_file_path = 'lib\\common\\porcupine_params.pv'
keyword_file_paths = ['flamingo_windows.ppn']
sensitivities = [0.5]
handle = Porcupine.Porcupine(library_path, model_file_path, keyword_file_paths=keyword_file_paths, sensitivities=sensitivities)
input_device_index=None

def run():

    def audio_callback(in_data, frame_count, time_info, status):
        pcm = struct.unpack_from("h" * handle.frame_length, in_data)
        result = handle.process(pcm)
        if result:
            print("Hotword detected!")
            commands.listening()
            pass
        return None, pyaudio.paContinue

    try:
        pa = pyaudio.PyAudio()
        sample_rate = handle.sample_rate
        num_channels = 1
        audio_format = pyaudio.paInt16
        frame_length = handle.frame_length
예제 #3
0
def main(api_endpoint=ASSISTANT_API_ENDPOINT,
         credentials=os.path.join(click.get_app_dir('google-oauthlib-tool'),
                                  'credentials.json'),
         project_id=None,
         device_model_id=None,
         device_id=None,
         device_config=os.path.join(
             click.get_app_dir('googlesamples-assistant'),
             'device_config.json'),
         lang='en-US',
         display=False,
         verbose=False,
         input_audio_file=None,
         output_audio_file=None,
         audio_sample_rate=audio_helpers.DEFAULT_AUDIO_SAMPLE_RATE,
         audio_sample_width=audio_helpers.DEFAULT_AUDIO_SAMPLE_WIDTH,
         audio_iter_size=audio_helpers.DEFAULT_AUDIO_ITER_SIZE,
         audio_block_size=audio_helpers.DEFAULT_AUDIO_DEVICE_BLOCK_SIZE,
         audio_flush_size=audio_helpers.DEFAULT_AUDIO_DEVICE_FLUSH_SIZE,
         grpc_deadline=DEFAULT_GRPC_DEADLINE,
         once=False,
         *args,
         **kwargs):
    # Setup logging.
    #logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)
    logging.basicConfig(level=logging.INFO)

    # Load OAuth 2.0 credentials.
    try:
        with open(credentials, 'r') as f:
            credentials = google.oauth2.credentials.Credentials(token=None,
                                                                **json.load(f))
            http_request = google.auth.transport.requests.Request()
            credentials.refresh(http_request)
    except Exception as e:
        logging.error('Error loading credentials: %s', e)
        logging.error('Run google-oauthlib-tool to initialize '
                      'new OAuth 2.0 credentials.')
        sys.exit(-1)

    # Create an authorized gRPC channel.
    grpc_channel = google.auth.transport.grpc.secure_authorized_channel(
        credentials, http_request, api_endpoint)
    logging.info('Connecting to %s', api_endpoint)

    # Configure audio source and sink.
    audio_device = None
    if input_audio_file:
        audio_source = audio_helpers.WaveSource(
            open(input_audio_file, 'rb'),
            sample_rate=audio_sample_rate,
            sample_width=audio_sample_width)
    else:
        audio_source = audio_device = (audio_device
                                       or audio_helpers.SoundDeviceStream(
                                           sample_rate=audio_sample_rate,
                                           sample_width=audio_sample_width,
                                           block_size=audio_block_size,
                                           flush_size=audio_flush_size))
    if output_audio_file:
        audio_sink = audio_helpers.WaveSink(open(output_audio_file, 'wb'),
                                            sample_rate=audio_sample_rate,
                                            sample_width=audio_sample_width)
    else:
        audio_sink = audio_device = (audio_device
                                     or audio_helpers.SoundDeviceStream(
                                         sample_rate=audio_sample_rate,
                                         sample_width=audio_sample_width,
                                         block_size=audio_block_size,
                                         flush_size=audio_flush_size))
    # Create conversation stream with the given audio source and sink.
    conversation_stream = audio_helpers.ConversationStream(
        source=audio_source,
        sink=audio_sink,
        iter_size=audio_iter_size,
        sample_width=audio_sample_width,
    )

    if not device_id or not device_model_id:
        try:
            with open(device_config) as f:
                device = json.load(f)
                device_id = device['id']
                device_model_id = device['model_id']
                logging.info("Using device model %s and device id %s",
                             device_model_id, device_id)
        except Exception as e:
            logging.warning('Device config not found: %s' % e)
            logging.info('Registering device')
            if not device_model_id:
                logging.error('Option --device-model-id required '
                              'when registering a device instance.')
                sys.exit(-1)
            if not project_id:
                logging.error('Option --project-id required '
                              'when registering a device instance.')
                sys.exit(-1)
            device_base_url = ('https://%s/v1alpha2/projects/%s/devices' %
                               (api_endpoint, project_id))
            device_id = str(uuid.uuid1())
            payload = {
                'id': device_id,
                'model_id': device_model_id,
                'client_type': 'SDK_SERVICE'
            }
            session = google.auth.transport.requests.AuthorizedSession(
                credentials)
            r = session.post(device_base_url, data=json.dumps(payload))
            if r.status_code != 200:
                logging.error('Failed to register device: %s', r.text)
                sys.exit(-1)
            logging.info('Device registered: %s', device_id)
            pathlib.Path(os.path.dirname(device_config)).mkdir(exist_ok=True)
            with open(device_config, 'w') as f:
                json.dump(payload, f)

    device_handler = device_helpers.DeviceRequestHandler(device_id)

    @device_handler.command('action.devices.commands.OnOff')
    def onoff(on):
        if on:
            logging.info('Turning device on')
        else:
            logging.info('Turning device off')

    @device_handler.command('com.example.commands.BlinkLight')
    def blink(speed, number):
        logging.info('Blinking device %s times.' % number)
        delay = 1
        if speed == "SLOWLY":
            delay = 2
        elif speed == "QUICKLY":
            delay = 0.5
        for i in range(int(number)):
            logging.info('Device is blinking.')
            time.sleep(delay)

    audio_stream = None
    handle = None
    pa = None
    library_path = '../../lib/windows/amd64/libpv_porcupine.dll'
    model_file_path = '../../lib/common/porcupine_params.pv'
    keyword_file_paths = ['bumblebee_windows.ppn']
    num_keywords = len(keyword_file_paths)
    sensitivities = [0.7]  #0.2
    handle = porcupine.Porcupine(library_path,
                                 model_file_path,
                                 keyword_file_paths=keyword_file_paths,
                                 sensitivities=sensitivities)

    pa = pyaudio.PyAudio()
    audio_stream = pa.open(rate=handle.sample_rate,
                           channels=1,
                           format=pyaudio.paInt16,
                           input=True,
                           frames_per_buffer=handle.frame_length)

    conversation_stream.volume_percentage = 100
    print('Listening for keyword bumblebee...')
    with SampleAssistant(lang, device_model_id, device_id, conversation_stream,
                         display, grpc_channel, grpc_deadline,
                         device_handler) as assistant:
        while True:

            pcm = audio_stream.read(handle.frame_length)
            pcm = struct.unpack_from("h" * handle.frame_length, pcm)
            result = handle.process(pcm)
            if num_keywords > 0 and result:
                play_audio_file('ding.wav')
                print('[%s] detected keyword!!!!' % str(datetime.now()))
                assistant.assist()
                play_audio_file('dong.wav')
                print('Listening for keyword bumblebee...')

        print('stopping ...')
        if handle is not None:
            handle.delete()

        if audio_stream is not None:
            audio_stream.close()

        if pa is not None:
            pa.terminate()