67 Python binding for Porcupine wake word engine. It detects utterances of given keywords within an incoming stream of
68 audio in real-time. It processes incoming audio in consecutive frames and for each frame emits the detection result.
69 The number of samples per frame can be attained by calling `.frame_length`. The incoming audio needs to have a
70 sample rate equal to `.sample_rate` and be 16-bit linearly-encoded. Porcupine operates on single-channel audio.
83 ACTIVATION_LIMIT_REACHED = 9
84 ACTIVATION_THROTTLED = 10
85 ACTIVATION_REFUSED = 11
87 _PICOVOICE_STATUS_TO_EXCEPTION = {
88 PicovoiceStatuses.OUT_OF_MEMORY: PorcupineMemoryError,
89 PicovoiceStatuses.IO_ERROR: PorcupineIOError,
90 PicovoiceStatuses.INVALID_ARGUMENT: PorcupineInvalidArgumentError,
91 PicovoiceStatuses.STOP_ITERATION: PorcupineStopIterationError,
92 PicovoiceStatuses.KEY_ERROR: PorcupineKeyError,
93 PicovoiceStatuses.INVALID_STATE: PorcupineInvalidStateError,
94 PicovoiceStatuses.RUNTIME_ERROR: PorcupineRuntimeError,
95 PicovoiceStatuses.ACTIVATION_ERROR: PorcupineActivationError,
96 PicovoiceStatuses.ACTIVATION_LIMIT_REACHED: PorcupineActivationLimitError,
97 PicovoiceStatuses.ACTIVATION_THROTTLED: PorcupineActivationThrottledError,
98 PicovoiceStatuses.ACTIVATION_REFUSED: PorcupineActivationRefusedError
104 def __init__(self, access_key, library_path, model_path, keyword_paths, sensitivities):
108 :param access_key: AccessKey obtained from Picovoice Console.
109 :param library_path: Absolute path to Porcupine's dynamic library.
110 :param model_path: Absolute path to the file containing model parameters.
111 :param keyword_paths: Absolute paths to keyword model files.
112 :param sensitivities: Sensitivities for detecting keywords. Each value should be a number within [0, 1]. A
113 higher sensitivity results in fewer misses at the cost of increasing the false alarm rate.
117 raise ValueError(
"access_key should be a non-empty string.")
119 if not os.path.exists(library_path):
120 raise IOError(
"Couldn't find Porcupine's dynamic library at '%s'." % library_path)
122 library = cdll.LoadLibrary(library_path)
124 if not os.path.exists(model_path):
125 raise IOError(
"Couldn't find model file at '%s'." % model_path)
127 if len(keyword_paths) != len(sensitivities):
128 raise ValueError(
"Number of keywords does not match the number of sensitivities.")
130 for x
in keyword_paths:
131 if not os.path.exists(os.path.expanduser(x)):
132 raise IOError(
"Couldn't find keyword file at '%s'." % x)
134 for x
in sensitivities:
135 if not (0 <= x <= 1):
136 raise ValueError(
'A sensitivity value should be within [0, 1].')
138 init_func = library.pv_porcupine_init
139 init_func.argtypes = [
145 POINTER(POINTER(self.CPorcupine))]
146 init_func.restype = self.PicovoiceStatuses
151 access_key.encode(
'utf-8'),
152 model_path.encode(
'utf-8'),
154 (c_char_p * len(keyword_paths))(*[os.path.expanduser(x).encode(
'utf-8')
for x
in keyword_paths]),
155 (c_float * len(keyword_paths))(*sensitivities),
168 version_func = library.pv_porcupine_version
169 version_func.argtypes = []
170 version_func.restype = c_char_p
178 """Releases resources acquired by Porcupine."""
184 Processes a frame of the incoming audio stream and emits the detection result.
186 :param pcm: A frame of audio samples. The number of samples per frame can be attained by calling
187 `.frame_length`. The incoming audio needs to have a sample rate equal to `.sample_rate` and be 16-bit
188 linearly-encoded. Porcupine operates on single-channel audio.
189 :return: Index of observed keyword at the end of the current frame. Indexing is 0-based and matches the ordering
190 of keyword models provided to the constructor. If no keyword is detected then it returns -1.
194 raise ValueError(
"Invalid frame length. expected %d but received %d" % (self.
frame_length, len(pcm)))
211 """Number of audio samples per frame."""
217 """Audio sample rate accepted by Picovoice."""