Porcupine Wake Word Engine: Flutter Integration and Background Listening
--
Are you tired of manually initiating voice commands on your Flutter app? Do you wish you could have your app automatically listen for certain phrases or words to trigger a specific action? Look no further than Porcupine Wake Word Engine!
Porcupine Wake Word Engine, developed by Picovoice, is a lightweight and accurate solution for implementing voice activation into any application. With Porcupine, you can set up custom wake words or use inbuilt ones, and seamlessly integrate it into your Flutter app with the added benefit of background listening.
First, you’ll need to get the Permission & Porcupine Flutter plugin by adding it to your pubspec.yaml file:
dependencies:
permission_handler: ^10.2.0
porcupine_flutter: ^2.1.6
We also need to add the necessary permissions for recording audio in the AndroidManifest.xml file.
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Next, you’ll need to request permission to use the microphone. You can do this by adding the following code to your app:
Future<bool> _checkAudioPermission() async {
bool permissionGranted = await Permission.microphone.isGranted;
if (!permissionGranted) {
await Permission.microphone.request();
}
permissionGranted = await Permission.microphone.isGranted;
return permissionGranted;
}
Porcupine comes with a few inbuilt wake words, such as “Alexa,” “Hey Google,” and “Picovoice.” These wake words are pre-trained and optimized for accurate detection in various acoustic environments. To use an inbuilt wake word, you can simply pass an array of keywords to the fromBuiltInKeywords()
method of the PorcupineManager
class, along with a wake word callback function that will be triggered when the wake word is detected.
We also need to provide an access key, which you can obtain from the Porcupine Console.
Here’s an example of how to create a PorcupineManager instance with the inbuilt wake words “Picovoice” and “Porcupine”:
late PorcupineManager _porcupineManager;
void _wakeWordCallback(int keywordIndex) {
if (keywordIndex == 0) {
// "Picovoice" wake word detected
// Do something
} else if (keywordIndex == 1) {
// "Porcupine" wake word detected
// Do something else
}
}
String accessKey = ""; // Get this from https://console.picovoice.ai/
void createPorcupineManager() async {
try{
_porcupineManager = await PorcupineManager.fromBuiltInKeywords(
accessKey,
[BuiltInKeyword.PICOVOICE, BuiltInKeyword.PORCUPINE],
_wakeWordCallback);
} on PorcupineException catch (err) {
// handle porcupine init error
}
}
In addition to the inbuilt wake words, you can also train Porcupine to recognize custom wake words specific to your app. To train a custom wake word, you can use the Porcupine Console, which provides a web-based interface for generating and downloading custom wake word models in the form of a .ppn file.
Once you have your custom wake word file, you can add it to your app’s assets folder and load it using the fromKeywordPaths()
method of the PorcupineManager
class, along with your wake word callback function.
Here’s an example of how to create a PorcupineManager instance with a custom wake word:
late PorcupineManager _porcupineManager;
void _wakeWordCallback(int keywordIndex) {
if (keywordIndex == 0) {
// Custom wake word detected
// Do something
}
}
String keywordAsset = "assets/wakeword.ppn";
String accessKey = ""; // Get this from https://console.picovoice.ai/
void createPorcupineManager() async {
try{
_porcupineManager = await PorcupineManager.fromKeywordPaths(
accessKey, [keywordAsset], _wakeWordCallback);
} on PorcupineException catch (err) {
// handle porcupine init error
}
}
In this example, we’re loading a custom wake word from the assets
folder and passing it to the fromKeywordPaths()
method along with our wake word callback function.
With Porcupine, you can easily add wake word detection to your Flutter app, whether you’re using inbuilt wake words or custom ones. With the ability to detect wake words in real-time, your app can offer hands-free, voice-activated functionality that’s both convenient and engaging for users.