Picovoice Porcupine

repository·master·Indexed 26 days ago

https://github.com/picovoice/porcupine

A high-performance, lightweight wake word engine designed for always-listening voice capabilities in applications, IoT, and embedded devices. It supports custom wake words (.ppn) and non-English language models (.pv), with bindings available for Android, .NET, Flutter, iOS, Java, and Node.js.

Tokens
31.4K
Snippets
89
Records
187
Agent score
86%

What's inside Porcupine

  1. Overview of Porcupine Wake Word Engine

    master

    Porcupine is a highly-accurate, lightweight, and computationally-efficient wake word engine designed for always-listening voice-enabled applications. It is suitable for IoT devices and supports multiple simultaneous voice commands without increasing the runtime footprint.

    Key Features:

    • Deep Neural Networks: Trained in real-world environments for high accuracy.
    • Cross-Platform Support: Works on Arm Cortex-M, STM32, Arduino, Raspberry Pi, Android, iOS, Web browsers (Chrome, Safari, Firefox, Edge), Linux, macOS, and Windows.
    • Customization: Developers can train custom wake word models using the Picovoice Console.
    • Scalability: Detects multiple commands with no added runtime footprint.
  2. Run C File Demo

    master

    To detect keywords within a specific audio file:

    Build

    cmake -S demo/c/. -B demo/c/build && cmake --build demo/c/build --target porcupine_demo_file

    Run (Linux, macOS, Raspberry Pi)

    ./demo/c/build/porcupine_demo_file -l ${LIBRARY_PATH} -m lib/common/porcupine_params.pv \
    -k resources/keyword_files/${PLATFORM}/porcupine_${PLATFORM}.ppn -t 0.5 \
    -w resources/audio_samples/multiple_keywords.wav -a ${ACCESS_KEY}

    Run (Windows)

    .\demo\c\build\porcupine_demo_file.exe ^
    -l lib/windows/amd64/libpv_porcupine.dll ^
    -m lib/common/porcupine_params.pv ^
    -k resources/keyword_files/windows/porcupine_windows.ppn -t 0.5 \
    -w resources/audio_samples/multiple_keywords.wav -a ${ACCESS_KEY}
  3. Install @picovoice/porcupine-react

    master

    To use Porcupine in a React application, install both the Porcupine React binding and the Web Voice Processor package using npm or yarn.

    yarn add @picovoice/porcupine-react @picovoice/web-voice-processor
    
    # or
    
    npm install --save @picovoice/porcupine-react @picovoice/web-voice-processor
  4. Run iOS Demos

    master

    iOS provides two types of demos:

    BackgroundService Demo

    Runs audio recording in the background to detect wake words when the app is not in focus.

    1. Open BackgroundService.xcodeproj in Xcode.
    2. Replace accessKey in ViewModel.swift with your AccessKey.
    3. Run on a simulator or device.

    ForegroundApp Demo

    Runs detection only when the application is in focus.

    1. Open ForegroundApp.xcodeproj in Xcode.
    2. Replace accessKey in ViewModel.swift with your AccessKey.
    3. Select the desired language scheme from Product > Scheme (e.g., esDemo).
    4. Run on a simulator or device.
  5. Run Web Demos (Vanilla JS or React)

    master

    Vanilla JavaScript and HTML

    From the demo/web directory, install dependencies and start the server:

    yarn
    yarn start ${LANGUAGE}
    # OR
    npm install
    npm run start ${LANGUAGE}

    Access the demo at http://localhost:5000.

    React

    From the demo/react directory, install dependencies and start the server:

    yarn
    yarn start ${LANGUAGE}
    # OR
    npm install
    npm run start ${LANGUAGE}

    Access the demo at http://localhost:3000.

  6. Install Porcupine for Android

    master

    To include Porcupine in your Android project, ensure mavenCentral() is included in your top-level build.gradle file. Then, add the dependency to your app's build.gradle file using the following implementation line:

    dependencies {
        // ...
        implementation 'ai.picovoice:porcupine-android:${LATEST_VERSION}'
    }
    dependencies {
        // ...
        implementation 'ai.picovoice:porcupine-android:${LATEST_VERSION}'
    }
  7. Install Porcupine for .NET

    master

    You can install the Porcupine NuGet package using the .NET CLI or via Visual Studio.

    Prerequisites:

    • For .NET Standard 2.0+: macOS (x86_64), Windows (x86_64).
    • For .NET 6.0+: Linux (x86_64), macOS (x86_64, arm64), Windows (x86_64, arm64), and Raspberry Pi (3, 4, or 5).
    dotnet add package Porcupine
  8. Install and use Porcupine Node.js SDK

    master

    Install via yarn add @picovoice/porcupine-node. Create a Porcupine instance with built-in keywords or custom keyword paths. Process audio using the .process() method and call .release() to free WebAssembly resources.

    const { Porcupine, BuiltinKeyword } = require("@picovoice/porcupine-node");
    
    const accessKey = "${ACCESS_KEY}";
    
    // Using built-in keywords
    let handle = new Porcupine(
        accessKey,
        [BuiltinKeyword.GRASSHOPPER, BuiltinKeyword.BUMBLEBEE],
        [0.5, 0.65]
    );
    
    // Using custom keyword path
    // let handle = new Porcupine(accessKey, ["/path/to/custom/keyword/file"], [0.5]);
    
    while (true) {
      let keywordIndex = handle.process(getNextAudioFrame());
      if (keywordIndex !== -1) {
        // detection event
      }
    }
    
    handle.release();
  9. Use the PorcupineManager High-Level API

    master

    The PorcupineManager is the easiest way to get started as it handles audio recording automatically using the flutter_voice_processor plugin.

    Use PorcupineManager.fromBuiltInKeywords for standard keywords or PorcupineManager.fromKeywordPaths for custom .ppn files. You must provide a valid AccessKey from the Picovoice Console.

    Lifecycle Management:

    • Call .start() to begin detection.
    • Call .stop() to pause detection.
    • Call .delete() to release resources. Warning: Avoid calling delete() from a paused state on Android unless you have handled the back button via WillPopScope.
    import 'package:porcupine_flutter/porcupine_manager.dart';
    import 'package:porcupine_flutter/porcupine_error.dart';
    
    final String accessKey = "{ACCESS_KEY}";
    
    // Example: Using built-in keywords
    void createPorcupineManager() async {
        try{
            _porcupineManager = await PorcupineManager.fromBuiltInKeywords(
                accessKey,
                [BuiltInKeyword.PICOVOICE, BuiltInKeyword.PORCUPINE],
                _wakeWordCallback);
        } on PorcupineException catch (err) {
            // handle porcupine init error
        }
    }
    
    // The callback receives the index of the detected keyword
    void _wakeWordCallback(int keywordIndex) {
        if (keywordIndex == 0) {
            // picovoice detected
        }
        else if (keywordIndex == 1) {
            // porcupine detected
        }
    }
    
    // Starting and stopping
    try{
        await _porcupineManager.start();
    } on PorcupineException catch (ex) {
        // handle error
    }
    
    await _porcupineManager.stop();
    await _porcupineManager.delete();