process method

int process(
  1. List<int>? frame
)

Process a frame of audio with the wake word engine.

frame is a frame of frame of audio samples to be assessed by Porcupine. The required audio format is found by calling .sampleRate to get the required sample rate and .frameLength to get the required frame size. Audio must be single-channel and 16-bit linearly-encoded.

Index of the detected keyword, or -1 if no detection occurred

Implementation

int process(List<int>? frame) {
  if (_handle == null) {
    throw new PvStateError(
        "Attempted to process an audio frame after Porcupine was been deleted.");
  }

  if (frame == null) {
    throw new PvArgumentError(
        "Frame array provided to Porcupine process was null.");
  }

  if (frame.length != frameLength) {
    throw new PvArgumentError(
        "Size of frame array provided to 'process' (${frame.length}) does not match the engine 'frameLength' ($frameLength)");
  }

  // generate arguments for ffi
  _cFrame.asTypedList(frame.length).setAll(0, frame);
  Pointer<Int32> keywordIndexPtr = malloc(1);

  int status = _porcupineProcess(_handle!, _cFrame, keywordIndexPtr);
  PvStatus pvStatus = PvStatus.values[status];
  if (pvStatus != PvStatus.SUCCESS) {
    pvStatusToException(
        pvStatus, "Porcupine failed to process an audio frame.");
  }

  return keywordIndexPtr.value;
}