Skip to content

Instantly share code, notes, and snippets.

@yodakohl
Created June 20, 2018 05:41
Show Gist options
  • Save yodakohl/f40aba7eefddfd88a42920b600b68c24 to your computer and use it in GitHub Desktop.
Save yodakohl/f40aba7eefddfd88a42920b600b68c24 to your computer and use it in GitHub Desktop.
#
# Copyright 2018 Picovoice Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
import os
import platform
import struct
import sys
from datetime import datetime
from threading import Thread
import wave
import collections
import subprocess
import threading
import numpy as np
import time
import soundfile
class RingBuffer(object):
"""Ring buffer to hold audio from audio capturing tool"""
def __init__(self, size = 4096):
self._buf = collections.deque(maxlen=size)
def extend(self, data):
"""Adds data to the end of buffer"""
self._buf.extend(data)
def get(self):
"""Retrieves data from the beginning of buffer and clears it"""
tmp = bytes(bytearray(self._buf))
self._buf.clear()
return tmp
sys.path.append(os.path.join(os.path.dirname(__file__), '../../binding/python'))
from porcupine import Porcupine
class PorcupineDemo(Thread):
"""
Demo class for wake word detection (aka Porcupine) library. It creates an input audio stream from a microphone,
monitors it, and upon detecting the specified wake word(s) prints the detection time and index of wake word on
console. It optionally saves the recorded audio into a file for further review.
"""
def __init__(
self,
library_path,
model_file_path,
keyword_file_paths,
sensitivity=0.8,
input_device_index=None,
output_path=None):
"""
Constructor.
:param library_path: Absolute path to Porcupine's dynamic library.
:param model_file_path: Absolute path to the model parameter file.
:param keyword_file_paths: List of absolute paths to keyword files.
:param sensitivity: Sensitivity parameter. For more information refer to 'include/pv_porcupine.h'. It uses the
same sensitivity value for all keywords.
:param input_device_index: Optional argument. If provided, audio is recorded from this input device. Otherwise,
the default audio input device is used.
:param output_path: If provided recorded audio will be stored in this location at the end of the run.
"""
super(PorcupineDemo, self).__init__()
self._library_path = library_path
self._model_file_path = model_file_path
self._keyword_file_paths = keyword_file_paths
self._sensitivity = float(sensitivity)
self._input_device_index = input_device_index
self._output_path = output_path
if self._output_path is not None:
self._recorded_frames = []
self.ring_buffer = RingBuffer(16000 * 8)
def run(self):
"""
Creates an input audio stream, initializes wake word detection (Porcupine) object, and monitors the audio
stream for occurrences of the wake word(s). It prints the time of detection for each occurrence and index of
wake word.
"""
num_keywords = len(self._keyword_file_paths)
porcupine = None
try:
porcupine = Porcupine(
library_path=self._library_path,
model_file_path=self._model_file_path,
keyword_file_paths=self._keyword_file_paths,
sensitivities=[self._sensitivity] * num_keywords)
self.porcupine = porcupine
self.init_recording()
self.running = True
while self.running:
data = self.ring_buffer.get()
if len(data) == 0:
time.sleep(0.01)
continue
data = struct.unpack_from("h" * porcupine.frame_length, data)
if self._output_path is not None:
self._recorded_frames.append(data)
result = porcupine.process(data)
if num_keywords == 1 and result:
print('[%s] detected keyword' % str(datetime.now()))
elif num_keywords > 1 and result >= 0:
print('[%s] detected keyword #%d' % (str(datetime.now()), result))
except KeyboardInterrupt:
self.running = False
self.recording = False
print('stopping ...')
finally:
if porcupine is not None:
porcupine.delete()
if self._output_path is not None and len(self._recorded_frames) > 0:
recorded_audio = np.concatenate(self._recorded_frames, axis=0).astype(np.int16)
soundfile.write(self._output_path, recorded_audio, samplerate=porcupine.sample_rate, subtype='PCM_16')
_AUDIO_DEVICE_INFO_KEYS = ['index', 'name', 'defaultSampleRate', 'maxInputChannels']
def record_proc(self):
CHUNK = self.porcupine.frame_length
RECORD_RATE = 16000
cmd = 'arecord -q -r %d -c 1 -f S16_LE' % RECORD_RATE
print(cmd)
process = subprocess.Popen(cmd.split(' '),
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
wav = wave.open(process.stdout, 'rb')
while self.recording:
data = wav.readframes(CHUNK)
self.ring_buffer.extend(data)
process.terminate()
def init_recording(self):
"""
Start a thread for spawning arecord process and reading its stdout
"""
self.recording = True
self.record_thread = threading.Thread(target = self.record_proc)
self.record_thread.start()
@classmethod
def show_audio_devices_info(cls):
""" Provides information regarding different audio devices available. """
pa = pyaudio.PyAudio()
for i in range(pa.get_device_count()):
info = pa.get_device_info_by_index(i)
print(', '.join("'%s': '%s'" % (k, str(info[k])) for k in cls._AUDIO_DEVICE_INFO_KEYS))
pa.terminate()
def _default_library_path():
system = platform.system()
machine = platform.machine()
if system == 'Darwin':
return os.path.join(os.path.dirname(__file__), '../../lib/mac/%s/libpv_porcupine.dylib' % machine)
elif system == 'Linux':
if machine == 'x86_64' or machine == 'i386':
return os.path.join(os.path.dirname(__file__), '../../lib/linux/%s/libpv_porcupine.so' % machine)
else:
raise Exception('cannot autodetect the binary type. Please enter the path to the shared object using --library_path command line argument.')
elif system == 'Windows':
if platform.architecture()[0] == '32bit':
return os.path.join(os.path.dirname(__file__), '..\\..\\lib\\windows\\i686\\libpv_porcupine.dll')
else:
return os.path.join(os.path.dirname(__file__), '..\\..\\lib\\windows\\amd64\\libpv_porcupine.dll')
raise NotImplementedError('Porcupine is not supported on %s/%s yet!' % (system, machine))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--keyword_file_paths', help='comma-separated absolute paths to keyword files', type=str, required=True)
parser.add_argument(
'--library_path',
help="absolute path to Porcupine's dynamic library",
type=str)
parser.add_argument(
'--model_file_path',
help='absolute path to model parameter file',
type=str,
default=os.path.join(os.path.dirname(__file__), '../../lib/common/porcupine_params.pv'))
parser.add_argument('--sensitivity', help='detection sensitivity [0, 1]', default=0.5)
parser.add_argument('--input_audio_device_index', help='index of input audio device', type=int, default=None)
parser.add_argument(
'--output_path',
help='absolute path to where recorded audio will be stored. If not set, it will be bypassed.',
type=str,
default=None)
parser.add_argument('--show_audio_devices_info', action='store_true')
args = parser.parse_args()
if args.show_audio_devices_info:
PorcupineDemo.show_audio_devices_info()
else:
PorcupineDemo(
library_path=args.library_path if args.library_path is not None else _default_library_path(),
model_file_path=args.model_file_path,
keyword_file_paths=[x.strip() for x in args.keyword_file_paths.split(',')],
sensitivity=args.sensitivity,
output_path=args.output_path,
input_device_index=args.input_audio_device_index).run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment