def test_process_multiple(self): keyword_file_names = \ ['americano', 'blueberry', 'bumblebee', 'grapefruit', 'grasshopper', 'picovoice', 'porcupine', 'terminator'] keyword_file_paths = list() for name in keyword_file_names: keyword_file_paths.append(KEYWORD_FILE_PATHS[name]) porcupine = Porcupine(library_path=LIBRARY_PATH, model_file_path=MODEL_FILE_PATH, keyword_file_paths=keyword_file_paths, sensitivities=[0.5] * len(keyword_file_paths)) audio, sample_rate = soundfile.read(os.path.join( os.path.dirname(__file__), '../../resources/audio_samples/multiple_keywords.wav'), dtype='int16') assert sample_rate == porcupine.sample_rate num_frames = len(audio) // porcupine.frame_length results = [] for i in range(num_frames): frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length] result = porcupine.process(frame) if result >= 0: results.append(result) self.assertEqual(results, [6, 0, 1, 2, 3, 4, 5, 6, 7]) porcupine.delete()
def keywordMonitoring(): model_file_path = join(dirname(__file__), 'Porcupine/lib/common/porcupine_params.pv') keyword_file_paths = ["Porcupine/Hey mate_mac.ppn"] sensitivities = [0.8] handle = Porcupine(library_path, model_file_path, keyword_file_paths=keyword_file_paths, sensitivities=sensitivities) num_keywords = 1 pa = pyaudio.PyAudio() audio_stream = pa.open(rate=handle.sample_rate, channels=1, format=pyaudio.paInt16, input=True, frames_per_buffer=handle.frame_length, input_device_index=None) while True: pcm = audio_stream.read(handle.frame_length) pcm = struct.unpack_from("h" * handle.frame_length, pcm) # if self._output_path is not None: # self._recorded_frames.append(pcm) result = handle.process(pcm) if num_keywords == 1 and result: print('[%s] detected keyword' % str(datetime.now())) # Send keyword trigger # This will block if the serial port hits at the same time serial_lock.acquire() ser.write(bytes("g", 'utf-8')) serial_lock.release()
def test_process(self): porcupine = Porcupine( library_path=pv_library_path('../..'), model_path=pv_model_path('../../'), keyword_paths=[pv_keyword_paths('../..')['porcupine']], sensitivities=[0.5]) audio, sample_rate = soundfile.read(os.path.join( os.path.dirname(__file__), '../../resources/audio_samples/porcupine.wav'), dtype='int16') assert sample_rate == porcupine.sample_rate num_frames = len(audio) // porcupine.frame_length results = [] for i in range(num_frames): frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length] keyword_index = porcupine.process(frame) if keyword_index >= 0: results.append(keyword_index) porcupine.delete() self.assertEqual(results, [0])
def listenmic(): num_keywords = 1 print('Waiting hotword:') porcupine = None pa = None audio_stream = None try: porcupine = Porcupine( library_path='lib/libpv_porcupine.dll', model_file_path='lib/porcupine_params.pv', keyword_file_paths=['keywords/jarvis_windows.ppn'], sensitivities=[0.9]) pa = pyaudio.PyAudio() audio_stream = pa.open(rate=porcupine.sample_rate, channels=1, format=pyaudio.paInt16, input=True, frames_per_buffer=porcupine.frame_length, input_device_index=None) while True: pcm = audio_stream.read(porcupine.frame_length) pcm = struct.unpack_from("h" * porcupine.frame_length, pcm) result = porcupine.process(pcm) if num_keywords == 1 and result: print('Listening') playmp3("mp3/activation.mp3") with sr.Microphone() as source: audio = r.listen(source) try: print(r.recognize_google(audio, language="ru-RU")) phrase = r.recognize_google(audio, language="ru-RU") for value in assistant_meme_words.values(): if phrase.lower() == value: speakme('куда') where_to_send() # mem_post = get_random_meme(VK_PUBLIC_IDS) # atts = mem_post.formatted_photos # text = mem_post.text # write_msg_vk_chat(vk_api, vk, VK_CHAT_ID, text, atts) # write_msg_vk_chat(vk_api, vk, VK_CHAT_ID, text, atts) print(value) print('Waiting hotword:') except sr.UnknownValueError: print("Voice assistant cannot hear phrase") speakme('Не услышала') print('Waiting hotword:') except sr.RequestError as e: print("Service error; {0}".format(e)) finally: if porcupine is not None: porcupine.delete() if audio_stream is not None: audio_stream.close() if pa is not None: pa.terminate() _AUDIO_DEVICE_INFO_KEYS = [ 'index', 'name', 'defaultSampleRate', 'maxInputChannels' ]
def test_process_multiple(self): keyword_file_names = ['alexa', 'americano', 'avocado', 'blueberry', 'bumblebee', 'caterpillar', 'christina', 'dragonfly', 'flamingo', 'francesca', 'grapefruit', 'grasshopper', 'iguana', 'picovoice', 'pineapple', 'porcupine', 'raspberry', 'terminator', 'vancouver'] keyword_file_paths = [ self._abs_path('../../resources/keyword_files/%s_%s.ppn' % (name, self._keyword_file_extension())) for name in keyword_file_names] porcupine = Porcupine( library_path=self._library_path(), model_file_path=self._abs_path('../../lib/common/porcupine_params.pv'), keyword_file_paths=keyword_file_paths, sensitivities=[0.5] * len(keyword_file_paths)) audio, sample_rate = soundfile.read( self._abs_path('../../resources/audio_samples/multiple_keywords.wav'), dtype='int16') assert sample_rate == porcupine.sample_rate num_frames = len(audio) // porcupine.frame_length results = [] for i in range(num_frames): frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length] result = porcupine.process(frame) if result >= 0: results.append(result) self.assertEqual(results, [15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]) porcupine.delete()
def test_process(self): extension = self._keyword_file_extension() keyword_file_path = self._abs_path( '../../resources/keyword_files/%s/porcupine_%s.ppn' % (extension, extension)) porcupine = Porcupine(library_path=self._library_path(), model_file_path=self._abs_path( '../../lib/common/porcupine_params.pv'), keyword_file_path=keyword_file_path, sensitivity=0.5) audio, sample_rate = soundfile.read( self._abs_path('../../resources/audio_samples/porcupine.wav'), dtype='int16') assert sample_rate == porcupine.sample_rate num_frames = len(audio) // porcupine.frame_length results = [] for i in range(num_frames): frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length] results.append(porcupine.process(frame)) porcupine.delete() self.assertEqual(sum(results), 1)
def test_process_multiple(self): keyword_file_names = ['alexa', 'americano', 'avocado', 'blueberry', 'bumblebee', 'caterpillar', 'christina', 'dragonfly', 'flamingo', 'francesca', 'grapefruit', 'grasshopper', 'iguana', 'picovoice', 'pineapple', 'porcupine', 'raspberry', 'terminator', 'vancouver'] keyword_file_paths = [ self._abs_path('../../resources/keyword_files/%s_%s.ppn' % (name, self._keyword_file_extension())) for name in keyword_file_names] porcupine = Porcupine( library_path=self._library_path(), model_file_path=self._abs_path('../../lib/common/porcupine_params.pv'), keyword_file_paths=keyword_file_paths, sensitivities=[0.5] * len(keyword_file_paths)) audio, sample_rate = soundfile.read( self._abs_path('../../resources/audio_samples/multiple_keywords.wav'), dtype='int16') assert sample_rate == porcupine.sample_rate num_frames = len(audio) // porcupine.frame_length results = [] for i in range(num_frames): frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length] result = porcupine.process(frame) if result >= 0: results.append(result) self.assertEqual(results, [15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]) porcupine.delete()
def get_baymax_porcupine(self): # New instance of Porcupine for Baymax self.baymaxPorcupine = Porcupine( self.library_path, self.model_file_path, keyword_file_paths=self.keyword_file_paths, sensitivities=self.sensitivities)
class DetectHotWord(): def __init__(self, library_path, model_path, keyword_path, sensitivity): self.handle = Porcupine(library_path, model_path, keyword_file_path=keyword_path, sensitivity=sensitivity) self.mic = None def hear(self): self.pa = pyaudio.PyAudio() audio_stream = self.pa.open( rate=self.handle.sample_rate, channels=1, format=pyaudio.paInt16, input=True, frames_per_buffer=self.handle.frame_length) self.mic = audio_stream def process(self): if self.mic is not None: pcm = self.mic.read(self.handle.frame_length) pcm = struct.unpack_from("h" * self.handle.frame_length, pcm) recorded_frames = [] recorded_frames.append(pcm) result = self.handle.process(pcm) if result: return True return False def __del__(self): self.mic.close() self.pa.terminate() self.handle.delete()
def __init__(self) -> None: """Initialize Hotword processing.""" self.porcupine = Porcupine( library_path=str(self._library_path), model_file_path=str(self._model_file_path), keyword_file_path=str(self._keyword_file_path), sensitivity=0.5, )
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 pa = None audio_stream = 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) pa = pyaudio.PyAudio() audio_stream = pa.open( rate=porcupine.sample_rate, channels=1, format=pyaudio.paInt16, input=True, frames_per_buffer=porcupine.frame_length, input_device_index=self._input_device_index) while True: pcm = audio_stream.read(porcupine.frame_length) pcm = struct.unpack_from("h" * porcupine.frame_length, pcm) if self._output_path is not None: self._recorded_frames.append(pcm) result = porcupine.process(pcm) if num_keywords == 1 and result: print('[%s] detected keyword' % str(datetime.now())) break elif num_keywords > 1 and result >= 0: print('[%s] detected keyword #%d' % (str(datetime.now()), result)) break except KeyboardInterrupt: print('stopping ...') finally: if porcupine is not None: porcupine.delete() if audio_stream is not None: audio_stream.close() if pa is not None: pa.terminate() 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')
def setUp(self): self._porcupine = Porcupine( library_path=self._library_path(), model_file_path=self._abs_path( '../../lib/common/porcupine_params.pv'), keyword_file_path=self._abs_path( '../../resources/keyword_files/porcupine_%s.ppn' % self._keyword_file_extension()), sensitivity=0.5)
class Baymax: def __init__(self): # Get paths and handle sensitivity of wake-word Baymax self.library_path = "porcupineResources/libpv_porcupine.dll" self.model_file_path = "porcupineResources/porcupine_params.pv" self.keyword_file_paths = ["ppn/bae_max_windows_2020-01-28.ppn"] self.sensitivities = [0.5] # Init them as None self.baymaxPorcupine = self.audio_stream = self.audio_instance = None # Only Baymax is there for now self.num_keywords = len(self.keyword_file_paths) def get_baymax_porcupine(self): # New instance of Porcupine for Baymax self.baymaxPorcupine = Porcupine( self.library_path, self.model_file_path, keyword_file_paths=self.keyword_file_paths, sensitivities=self.sensitivities) def get_audio_stream(self): self.audio_instance = pyaudio.PyAudio() return self.audio_instance.open( rate=self.baymaxPorcupine.sample_rate, channels=1, input=True, format=pyaudio.paInt16, frames_per_buffer=self.baymaxPorcupine.frame_length, input_device_index=None) def start_listening_for_baymax(self, callback_functions): try: self.get_baymax_porcupine() print("Listening....") while True: self.audio_stream = self.get_audio_stream() pcm = self.audio_stream.read(self.baymaxPorcupine.frame_length) pcm = struct.unpack_from( "h" * self.baymaxPorcupine.frame_length, pcm) result = self.baymaxPorcupine.process(pcm) if self.num_keywords == 1 and result: print(f'detected keyword Baymax {str(datetime.now())}') for func in callback_functions: func() finally: if self.baymaxPorcupine is not None: self.baymaxPorcupine.delete() if self.audio_stream is not None: self.audio_stream.close() if self.audio_instance is not None: self.audio_instance.terminate()
def setUp(self): self._porcupine = Porcupine( library_path=self._abs_path( '../../lib/%s/%s/libpv_porcupine.%s' % (self._system(), self._machine(), self._dylib_extension())), model_file_path=self._abs_path( '../../lib/common/porcupine_params.pv'), keyword_file_path=self._abs_path( '../../resources/keyword_files/porcupine_%s.ppn' % (self._system())), sensitivity=0.5)
def trigger_detect(): porcupine = None pa = None audio_stream = None record = [] done = 0 try: porcupine = Porcupine(library_path, model_file_path, keyword_file_path, sensitivity) pa = pyaudio.PyAudio() global sample_rate sample_rate = porcupine.sample_rate audio_stream = pa.open(rate=porcupine.sample_rate, channels=1, format=pyaudio.paInt16, input=True, frames_per_buffer=porcupine.frame_length, input_device_index=None) start = time.time() while True: #print("While True") end = time.time() if (end - start) < 2: pcm = audio_stream.read(porcupine.frame_length) pcm = struct.unpack_from("h" * porcupine.frame_length, pcm) result = porcupine.process(pcm) record.append(pcm) if result: print('Starting BARC') done = 1 else: print("Entering") if done == 1: recorded_audio = np.concatenate(record, axis=0).astype(np.int16) soundfile.write(output_path, recorded_audio, samplerate=sample_rate, subtype='PCM_16') done = 0 ## record = [] start = time.time() except KeyboardInterrupt: print('stopping ...') finally: if porcupine is not None: porcupine.delete() if audio_stream is not None: audio_stream.close() if pa is not None: pa.terminate()
def load_handle(self): """Load porcupine library.""" if self.handle is None: for kw_path in self.keyword_paths: if not kw_path.is_file(): self._logger.error( "Missing porcupine keyword at {kw_path}") from porcupine import Porcupine self.handle = Porcupine( self.library_path, self.model_path, keyword_file_paths=[str(p) for p in self.keyword_paths], sensitivities=self.sensitivities, ) # 16-bit self.chunk_size = self.handle.frame_length * 2 self.chunk_format = "h" * self.handle.frame_length self._logger.debug( "Loaded porcupine (keyword=%s). Expecting sample rate=%s, frame length=%s", self.keyword_paths, self.handle.sample_rate, self.handle.frame_length, )
def run(self, keyword_name='bumblebee', sensitivity=0.5): """ 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. """ # print('- %s (sensitivity: %f)' % (keyword_name, sensitivity)) def sdcallback(indata, frames, time, status): if status: logging.info(status) if (frames) >= porcupine.frame_length: pcm = struct.unpack_from("h" * porcupine.frame_length, indata) result = porcupine.process(pcm) if result: send_native_message({'ns': 'hotword', 'state': 'on'}) asr_result = self.asr.recognize() send_native_message(asr_result) # with sd.InputStream(samplerate=args.samplerate, device=args.device, channels=args.channels, callback=sdcallback) as asr_stream: # logging.info('Delegating to ASR...') # pass porcupine = None audio_stream = None sample_rate = None porcupine = Porcupine(library_path=self._library_path, model_file_path=self._model_file_path, keyword_file_path=self.keywords.get('bumblebee'), sensitivity=sensitivity) # Make sure the file is opened before recording anything: with sd.RawInputStream(channels=1, dtype='int16', samplerate=porcupine.sample_rate, blocksize=porcupine.frame_length, callback=sdcallback) as stream: # print(' Pinned / Top repo Pinned / Top repositories sitories #' * Pinned / Top repositories 80) # print('press Ctrl+C to stop the recording') # print('#' * 80) while True: 1 == 1 # delete Porcupine last to avoid segfault in callback. if porcupine is not None: porcupine.delete()
def listenmic(): num_keywords = 1 print('Ждем ключевого слова roxy:') porcupine = None pa = None audio_stream = None try: porcupine = Porcupine( library_path='lib/libpv_porcupine.dll', model_file_path='lib/porcupine_params.pv', keyword_file_paths=['keywords/americano_windows.ppn'], sensitivities=[0.5]) pa = pyaudio.PyAudio() audio_stream = pa.open(rate=porcupine.sample_rate, channels=1, format=pyaudio.paInt16, input=True, frames_per_buffer=porcupine.frame_length, input_device_index=None) while True: pcm = audio_stream.read(porcupine.frame_length) pcm = struct.unpack_from("h" * porcupine.frame_length, pcm) result = porcupine.process(pcm) if num_keywords == 1 and result: print('Слушаю') with sr.Microphone() as source: audio = r.listen(source) try: print(r.recognize_google(audio, language="ru-RU")) print('Ждем ключевого слова roxy:') except sr.UnknownValueError: print("Робот не расслышал фразу") print('Ждем ключевого слова roxy:') except sr.RequestError as e: print("Ошибка сервиса; {0}".format(e)) finally: if porcupine is not None: porcupine.delete() if audio_stream is not None: audio_stream.close() if pa is not None: pa.terminate() _AUDIO_DEVICE_INFO_KEYS = [ 'index', 'name', 'defaultSampleRate', 'maxInputChannels' ]
def __init__(self, key_phrase="hey mycroft", config=None, lang="en-us"): super(PorcupineHotWord, self).__init__(key_phrase, config, lang) porcupine_path = expanduser( self.config.get("porcupine_path", join('~', '.mycroft', 'Porcupine'))) keyword_file_paths = [ expanduser(x.strip()) for x in self.config.get( "keyword_file_path", "hey_mycroft.ppn").split(',') ] sensitivities = self.config.get("sensitivities", 0.5) bindings_path = join(porcupine_path, 'binding/python') LOG.info('Adding %s to Python path' % bindings_path) sys.path.append(bindings_path) try: from porcupine import Porcupine except ImportError: raise Exception( "Python bindings for Porcupine not found. " "Please use --porcupine-path to set Porcupine base path") system = platform.system() machine = platform.machine() library_path = join(porcupine_path, 'lib/linux/%s/libpv_porcupine.so' % machine) model_file_path = join(porcupine_path, 'lib/common/porcupine_params.pv') if isinstance(sensitivities, float): sensitivities = [sensitivities] * len(keyword_file_paths) else: sensitivities = [float(x) for x in sensitivities.split(',')] self.audio_buffer = [] self.has_found = False self.num_keywords = len(keyword_file_paths) LOG.info( 'Loading Porcupine using library path {} and keyword paths {}'. format(library_path, keyword_file_paths)) self.porcupine = Porcupine(library_path=library_path, model_file_path=model_file_path, keyword_file_paths=keyword_file_paths, sensitivities=sensitivities) LOG.info('Loaded Porcupine')
def wakeWord(library_path, keyword_file_paths, model_file_path): try: if not keyword_file_paths: raise ValueError('keyword file paths are missing') num_keywords = len(keyword_file_paths) sensitivities = 0.5 keyword_file_paths = [x.strip() for x in keyword_file_paths.split(',')] if isinstance(sensitivities, float): sensitivities = [sensitivities] * len(keyword_file_paths) else: sensitivities = [float(x) for x in sensitivities.split(',')] porcupine = Porcupine(library_path=library_path, model_file_path=model_file_path, keyword_file_paths=keyword_file_paths, sensitivities=sensitivities) pa = pyaudio.PyAudio() audio_stream = pa.open(rate=porcupine.sample_rate, channels=1, format=pyaudio.paInt16, input=True, frames_per_buffer=porcupine.frame_length, input_device_index=None) while True: pcm = audio_stream.read(porcupine.frame_length) pcm = struct.unpack_from("h" * porcupine.frame_length, pcm) result = porcupine.process(pcm) if num_keywords == 1 and result: print('[%s] detected keyword' % str(datetime.now())) #audio_stream.close() #value = recognizeSpeech() #response = requestWit(value) #parseResponse(response, cam) elif num_keywords > 1 and result >= 0: pass except KeyboardInterrupt: print('stopping ...')
def _run(input_audio_file_path, library_path, model_file_path, keyword_file_paths, sensitivity): """ Monitors an input audio file for occurances of keywords for which keyword files are provided and prints their occurrence time (in seconds from start of file). :param input_audio_file_path: Absolute path to input audio file. The file should have a sample rate of 16000 and be single-channel. :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. :return: """ num_keywords = len(keyword_file_paths) porcupine = Porcupine(library_path=library_path, model_file_path=model_file_path, keyword_file_paths=keyword_file_paths, sensitivities=[sensitivity] * num_keywords) def _frame_index_to_sec(frame_index): return float(frame_index * porcupine.frame_length) / float( porcupine.sample_rate) audio, sample_rate = soundfile.read(input_audio_file_path, dtype='int16') assert sample_rate == porcupine.sample_rate num_frames = len(audio) // porcupine.frame_length for i in range(num_frames): frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length] result = porcupine.process(frame) if num_keywords == 1 and result: print('detected keyword at time %f' % _frame_index_to_sec(i)) elif num_keywords > 1 and result >= 0: print('detected keyword index %d at time %f' % (result, _frame_index_to_sec(i))) porcupine.delete()
def trigger_detect(keyword_paths): if len(keyword_paths) == 1: keyword_path = keyword_paths[0] else: sensitivities = [0.5 for i in range(len(keyword_paths))] porcupine = None pa = None audio_stream = None record = [] done = 0 try: if len(keyword_paths) == 1: porcupine = Porcupine( library_path = library_path, model_file_path = model_file_path, keyword_file_path = keyword_path, sensitivity = sensitivity) else: porcupine = Porcupine( library_path = library_path, model_file_path = model_file_path, keyword_file_paths = keyword_paths, sensitivities = sensitivities) pa = pyaudio.PyAudio() global sample_rate sample_rate = porcupine.sample_rate audio_stream = pa.open( rate = porcupine.sample_rate, channels = 1, format = pyaudio.paInt16, input = True, frames_per_buffer = porcupine.frame_length, input_device_index = None) while True: pcm = audio_stream.read(porcupine.frame_length) pcm = struct.unpack_from("h" * porcupine.frame_length, pcm) result = porcupine.process(pcm) record.append(pcm) if len(keyword_paths) == 1: if result: print("Keyword Detected") return result else: if result >= 0: print("Keyword Detected") return result except KeyboardInterrupt: print('stopping ...') finally: if porcupine is not None: porcupine.delete() if audio_stream is not None: audio_stream.close() if pa is not None: pa.terminate()
def test_process(self): porcupine = Porcupine( library_path=self._library_path(), model_file_path=self._abs_path('../../lib/common/porcupine_params.pv'), keyword_file_path=self._abs_path('../../resources/keyword_files/porcupine_%s.ppn' % self._keyword_file_extension()), sensitivity=0.5) audio, sample_rate = soundfile.read( self._abs_path('../../resources/audio_samples/porcupine.wav'), dtype='int16') assert sample_rate == porcupine.sample_rate num_frames = len(audio) // porcupine.frame_length results = [] for i in range(num_frames): frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length] results.append(porcupine.process(frame)) porcupine.delete() self.assertEqual(sum(results), 1)
def test_process(self): porcupine = Porcupine( library_path=LIBRARY_PATH, model_file_path=MODEL_FILE_PATH, keyword_file_path=KEYWORD_FILE_PATHS['porcupine'], sensitivity=0.5) audio, sample_rate = soundfile.read( os.path.join(os.path.dirname(__file__), '../../resources/audio_samples/porcupine.wav'), dtype='int16') assert sample_rate == porcupine.sample_rate num_frames = len(audio) // porcupine.frame_length results = [] for i in range(num_frames): frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length] results.append(porcupine.process(frame)) porcupine.delete() self.assertEqual(sum(results), 1)
def __init__(self, library_path, model_file_path, keyword_file_paths, sensitivities): self._library_path = library_path self._model_file_path = model_file_path self._keyword_file_paths = keyword_file_paths self._sensitivities = sensitivities self._input_device_index = None num_keywords = len(self._keyword_file_paths) self.keyword_names =\ [os.path.basename(x).replace('.ppn', '').replace('_tiny', '').split('_')[0] for x in self._keyword_file_paths] print('listening for:') for keyword_name, sensitivity in zip(self.keyword_names, sensitivities): print('- %s (sensitivity: %f)' % (keyword_name, sensitivity)) self.porcupine = None self.pa = None self.audio_stream = None # print(self._library_path, self._model_file_path, self._keyword_file_paths, self._sensitivities) self.porcupine = Porcupine( library_path=self._library_path, model_file_path=self._model_file_path, keyword_file_paths=self._keyword_file_paths, sensitivities=self._sensitivities) pa = pyaudio.PyAudio() self.audio_stream = pa.open( rate=self.porcupine.sample_rate, channels=1, format=pyaudio.paInt16, input=True, frames_per_buffer=self.porcupine.frame_length, input_device_index=self._input_device_index)
def _run(input_audio_file_path, library_path, model_file_path, keyword_file_paths, sensitivity): """ Monitors an input audio file for occurances of keywords for which keyword files are provided and prints their occurrence time (in seconds from start of file). :param input_audio_file_path: Absolute path to input audio file. The file should have a sample rate of 16000 and be single-channel. :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. :return: """ num_keywords = len(keyword_file_paths) porcupine = Porcupine( library_path=library_path, model_file_path=model_file_path, keyword_file_paths=keyword_file_paths, sensitivities=[sensitivity] * num_keywords) def _frame_index_to_sec(frame_index): return float(frame_index * porcupine.frame_length) / float(porcupine.sample_rate) audio, sample_rate = soundfile.read(input_audio_file_path, dtype='int16') assert sample_rate == porcupine.sample_rate num_frames = len(audio) // porcupine.frame_length for i in range(num_frames): frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length] result = porcupine.process(frame) if num_keywords == 1 and result: print('detected keyword at time %f' % _frame_index_to_sec(i)) elif num_keywords > 1 and result >= 0: print('detected keyword index %d at time %f' % (result, _frame_index_to_sec(i))) porcupine.delete()
def run_porcupine(self, language, keywords, ground_truth, audio_file_name=None): if audio_file_name is None: _audio_file_name_prefix = self.__append_language( 'multiple_keywords', language) audio_file_name = '%s.wav' % _audio_file_name_prefix keyword_paths = list() for x in keywords: keyword_paths.append( self.__pv_keyword_paths_by_language('../..', language)[x]) porcupine = Porcupine(access_key=sys.argv[1], library_path=pv_library_path('../..'), model_path=self.__pv_model_path_by_language( '../..', language), keyword_paths=keyword_paths, sensitivities=[0.5] * len(keyword_paths)) audio = self.__read_file( os.path.join(os.path.dirname(__file__), '../../resources/audio_samples/', audio_file_name), porcupine.sample_rate) num_frames = len(audio) // porcupine.frame_length results = [] for i in range(num_frames): frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length] result = porcupine.process(frame) if result >= 0: results.append(result) porcupine.delete() self.assertEqual(results, ground_truth)
def test_process_multiple(self): keyword_file_names = [ 'alexa', 'americano', 'blueberry', 'bumblebee', 'grapefruit', 'grasshopper', 'hey pico', 'picovoice', 'porcupine', 'terminator' ] extension = self._keyword_file_extension() keyword_file_paths = list() for name in keyword_file_names: keyword_file_paths.append( self._abs_path('../../resources/keyword_files/%s/%s_%s.ppn' % (extension, name, extension))) porcupine = Porcupine(library_path=self._library_path(), model_file_path=self._abs_path( '../../lib/common/porcupine_params.pv'), keyword_file_paths=keyword_file_paths, sensitivities=[0.5] * len(keyword_file_paths)) audio, sample_rate = soundfile.read(self._abs_path( '../../resources/audio_samples/multiple_keywords.wav'), dtype='int16') assert sample_rate == porcupine.sample_rate num_frames = len(audio) // porcupine.frame_length results = [] for i in range(num_frames): frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length] result = porcupine.process(frame) if result >= 0: results.append(result) self.assertEqual(results, [8, 0, 1, 2, 3, 4, 5, 7, 8, 9]) porcupine.delete()
def load_handle(self): if self.handle is None: from porcupine import Porcupine self.handle = Porcupine( self.library_path, self.model_path, keyword_file_paths=self.keyword_paths, sensitivities=self.sensitivities, ) # 16-bit self.chunk_size = self.handle.frame_length * 2 self.chunk_format = "h" * self.handle.frame_length self._logger.debug( f"Loaded porcupine (keyword={self.keyword_paths[0]}). Expecting sample rate={self.handle.sample_rate}, frame length={self.handle.frame_length}" )
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) keyword_names =\ [os.path.basename(x).replace('.ppn', '').replace('_tiny', '').split('_')[0] for x in self._keyword_file_paths] print('listening for:') for keyword_name, sensitivity in zip(keyword_names, self._sensitivities): print('- %s (sensitivity: %f)' % (keyword_name, sensitivity)) porcupine = None pa = None audio_stream = 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._sensitivities) pa = pyaudio.PyAudio() audio_stream = pa.open(rate=porcupine.sample_rate, channels=1, format=pyaudio.paInt16, input=True, frames_per_buffer=porcupine.frame_length, input_device_index=self._input_device_index) while True: pcm = audio_stream.read(porcupine.frame_length) pcm = struct.unpack_from("h" * porcupine.frame_length, pcm) if self._output_path is not None: self._recorded_frames.append(pcm) result = porcupine.process(pcm) if num_keywords == 1 and result: play_audio_file(DETECT_DING) print('[%s] detected keyword' % str(datetime.now())) while True: #if self._assistant.assistOrg() == False: if self._assistant.assist() == False: break #print('done') play_audio_file(DETECT_DONG) elif num_keywords > 1 and result >= 0: print('[%s] detected %s' % (str(datetime.now()), keyword_names[result])) except KeyboardInterrupt: print('stopping ...') finally: if porcupine is not None: porcupine.delete() if audio_stream is not None: audio_stream.close() if pa is not None: pa.terminate() 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)
def run(self): """ Creates an input audio stream, initializes wake word detection (Porcupine) and speech to intent (Rhino) engines, and monitors the audio stream for occurrences of the wake word and then infers the intent from speech command that follows. """ porcupine = None rhino = None pa = None audio_stream = None wake_phrase_detected = False intent_extraction_is_finalized = False try: porcupine = Porcupine( library_path=self._porcupine_library_path, model_file_path=self._porcupine_model_file_path, keyword_file_paths=[self._porcupine_keyword_file_path], sensitivities=[self._porcupine_sensitivity]) rhino = Rhino(library_path=self._rhino_library_path, model_file_path=self._rhino_model_file_path, context_file_path=self._rhino_context_file_path) print(rhino.context_expressions) pa = pyaudio.PyAudio() audio_stream = pa.open(rate=porcupine.sample_rate, channels=1, format=pyaudio.paInt16, input=True, frames_per_buffer=porcupine.frame_length, input_device_index=self._input_device_index) # NOTE: This is true now and will be correct possibly forever. If it changes the logic below need to change. assert porcupine.frame_length == rhino.frame_length while True: pcm = audio_stream.read(porcupine.frame_length) pcm = struct.unpack_from("h" * porcupine.frame_length, pcm) if self._output_path is not None: self._recorded_frames.append(pcm) if not wake_phrase_detected: wake_phrase_detected = porcupine.process(pcm) if wake_phrase_detected: print('detected wake phrase') elif not intent_extraction_is_finalized: intent_extraction_is_finalized = rhino.process(pcm) else: if rhino.is_understood(): intent, slot_values = rhino.get_intent() print('intent: %s' % intent) print('---') for slot, value in slot_values.items(): print('%s: %s' % (slot, value)) print() else: print("didn't understand the command") rhino.reset() wake_phrase_detected = False intent_extraction_is_finalized = False except KeyboardInterrupt: print('stopping ...') finally: if porcupine is not None: porcupine.delete() if rhino is not None: rhino.delete() if audio_stream is not None: audio_stream.close() if pa is not None: pa.terminate() 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')
return type : int ''' if len(keyword_paths) == 1: keyword_path = keyword_paths[0] else: sensitivities = [0.5 for i in range(len(keyword_paths))] porcupine = None pa = None audio_stream = None record = [] done = 0 try: if len(keyword_paths) == 1: porcupine = Porcupine( library_path = library_path, model_file_path = model_file_path, keyword_file_path = keyword_path, sensitivity = sensitivity) else: porcupine = Porcupine( library_path = library_path, model_file_path = model_file_path, keyword_file_paths = keyword_paths, sensitivities = sensitivities) pa = pyaudio.PyAudio() global sample_rate sample_rate = porcupine.sample_rate audio_stream = pa.open( rate = porcupine.sample_rate, channels = 1,
input=texttospeech.SynthesisInput(text=string), voice=self.text_to_speech["voice"], audio_config=self.text_to_speech["audio_config"]) with open("temp.mp3", "wb") as out: out.write(response.audio_content) playsound("temp.mp3") os.remove("temp.mp3") self.vlc_player.audio_set_volume(temp_media_volume) def run_most_likely_command(self, command_string): """ Runs command with highest likelyhood of waking from all plugins """ working_plugins = self.plugins.copy() working_plugins.sort( key=lambda x: x.find_highest_probablity(command_string)) working_plugins[-1].most_likely_command(command_string).call_function() carrie = Carrie() carrie.load_plugins_from_folder() carrie_porcupine = Porcupine( "porcupine_wakewords", 0.5, lambda: carrie.run_most_likely_command(carrie.inp())) carrie_porcupine.run()