start method
Opens audio input stream and sends audio frames to Picovoice.
Throws a PvAudioException
if there was a problem starting the audio engine.
Throws a PvError
if an instance of Picovoice could not be created.
Implementation
Future<void> start() async {
if (_picovoice != null) {
return;
}
_picovoice = await Picovoice.create(
_keywordPath, _wakeWordCallback, _contextPath, _inferenceCallback,
porcupineSensitivity: _porcupineSensitivity,
rhinoSensitivity: _rhinoSensitivity,
porcupineModelPath: _porcupineModelPath,
rhinoModelPath: _rhinoModelPath);
if (_voiceProcessor == null) {
throw new PvError("flutter_voice_processor not available.");
}
_removeVoiceProcessorListener =
_voiceProcessor!.addListener((buffer) async {
// cast from dynamic to int array
List<int> picovoiceFrame;
try {
picovoiceFrame = (buffer as List<dynamic>).cast<int>();
} on Error {
PvError castError = new PvError(
"flutter_voice_processor sent an unexpected data type.");
_errorCallback == null
? print(castError.message)
: _errorCallback!(castError);
return;
}
// process frame with Picovoice
try {
_picovoice?.process(picovoiceFrame);
} on PvError catch (error) {
_errorCallback == null ? print(error.message) : _errorCallback!(error);
}
});
_removeErrorListener = _voiceProcessor!.addErrorListener((errorMsg) {
PvError nativeError = new PvError(errorMsg as String);
_errorCallback == null
? print(nativeError.message)
: _errorCallback!(nativeError);
});
if (await _voiceProcessor?.hasRecordAudioPermission() ?? false) {
try {
// create picovoice
await _voiceProcessor!.start();
} on PlatformException {
throw new PvAudioException(
"Audio engine failed to start. Hardware may not be supported.");
}
} else {
throw new PvAudioException(
"User did not give permission to record audio.");
}
}