def test_handler(): def on_speech(context): context.transcript = "event handled" context = SpeechContext() context.add_handler("recognize", on_speech) context.event("recognize") assert context.transcript == "event handled"
def _detect(self, context: SpeechContext) -> None: # read the full contents of the encode window and add the batch dimension # calculate a scalar likelihood that the frame contains a keyword # with the detect model frame = self.encode_window.read_all() frame = np.expand_dims(frame, 0) posterior = self.detect_model(frame)[0][0] class_index = np.argmax(posterior) confidence = posterior[class_index] if confidence >= self._posterior_threshold: context.transcript = self.classes[class_index] context.confidence = confidence context.event("recognize") else: context.event("timeout") self.reset()
def _receive(self, context: SpeechContext) -> None: for response in self._client.streaming_recognize( self._config, self._drain()): for result in response.results[:1]: for alternative in result.alternatives[:1]: context.transcript = alternative.transcript context.confidence = alternative.confidence if context.transcript: context.event("partial_recognize") if result.is_final: if context.transcript: context.event("recognize") _LOG.debug("recognize event") else: context.event("timeout") _LOG.debug("timeout event")
def _receive(self, context: SpeechContext) -> None: self._client.receive() hypotheses = self._client.response.get("hypotheses") if hypotheses: hypothesis = hypotheses[0] context.transcript = hypothesis["transcript"] context.confidence = hypothesis["confidence"] if context.transcript: context.event("partial_recognize") if self._client.is_final: if context.transcript: context.event("recognize") _LOG.debug("recognize event") else: context.event("timeout") _LOG.debug("timeout event")
class SpeechPipeline: """Pipeline for managing speech components. Args: input_source: source of audio input stages: components desired in the pipeline **kwargs: additional keyword arguments """ def __init__(self, input_source: Any, stages: List[Any]) -> None: self._context = SpeechContext() self._input_source = input_source self._stages: list = stages self._is_running = False self._is_paused = False def _dispatch(self) -> None: frame = self._input_source.read() for stage in self._stages: stage(self._context, frame) def close(self) -> None: """ Closes the running pipeline """ self.stop() def activate(self) -> None: """ Activates the pipeline """ self._context.is_active = True def deactivate(self) -> None: """ Deactivates the pipeline """ self._context.is_active = False def start(self) -> None: """ Starts input source of the pipeline """ if not self._is_running: self._input_source.start() self._is_running = True def stop(self) -> None: """ Halts the pipeline """ self._is_running = False self._input_source.stop() def pause(self) -> None: """ Stops audio input until resume is called """ self._is_paused = True self._input_source.stop() def resume(self) -> None: """ Resumes audio input after a pause """ self._is_paused = False self._input_source.start() def run(self) -> None: """ Runs the pipeline to process speech and cleans up after stop is called """ if not self._is_running: self.start() while self._is_running: self.step() self.cleanup() def step(self) -> None: """ Process a single frame with the pipeline """ self._context.event("step") if not self._is_paused: self._dispatch() def cleanup(self) -> None: """ Resets all attributes to default configuration """ for stage in self._stages: stage.close() self._stages.clear() self._input_source.close() self._input_source = None self._context.reset() def event(self, function: Any = None, name: Union[str, None] = None) -> Any: """Registers an event handler Args: function: event handler name: name of event handler Returns: Default event handler if a function not specified """ if function: self._context.add_handler( name or function.__name__.replace("on_", ""), function) else: return lambda function: self.event(function, name) @property def is_running(self) -> bool: """ State of the pipeline """ return self._is_running @property def context(self) -> SpeechContext: """ Current context """ return self._context