porcupine

package module
v3.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 14 Imported by: 1

README

Porcupine Binding for Go

Porcupine Wake Word Engine

Made in Vancouver, Canada by Picovoice

Porcupine is a highly-accurate and lightweight wake word engine. It enables building always-listening voice-enabled applications. It is

  • using deep neural networks trained in real-world environments.
  • compact and computationally-efficient. It is perfect for IoT.
  • cross-platform:
    • Arm Cortex-M, STM32, Arduino, and i.MX RT
    • Raspberry Pi, NVIDIA Jetson Nano, and BeagleBone
    • Android and iOS
    • Chrome, Safari, Firefox, and Edge
    • Linux (x86_64), macOS (x86_64, arm64), and Windows (x86_64)
  • scalable. It can detect multiple always-listening voice commands with no added runtime footprint.
  • self-service. Developers can train custom wake word models using Picovoice Console.

Compatibility

  • Go 1.16+
  • Runs on Linux (x86_64), macOS (x86_64, arm64), Windows (x86_64), Raspberry Pi, NVIDIA Jetson (Nano), and BeagleBone.
  • Windows: The Go binding requires cgo, which means that you need to install a gcc compiler like Mingw to build it properly.
    • Go versions less than 1.20 requires gcc version 11 or lower.

Installation

go get github.com/Picovoice/porcupine/binding/go/v2

AccessKey

Porcupine requires a valid Picovoice AccessKey at initialization. AccessKey acts as your credentials when using Porcupine SDKs. You can get your AccessKey for free. Make sure to keep your AccessKey secret. Signup or Login to Picovoice Console to get your AccessKey.

Usage

To create an instance of the engine you first create a Porcupine struct with the configuration parameters for the wake word engine and then make a call to .Init():

import . "github.com/Picovoice/porcupine/binding/go/v2"

porcupine := Porcupine{
    AccessKey: "${ACCESS_KEY}", // AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
    BuiltInKeywords: []BuiltInKeyword{PICOVOICE}}
err := porcupine.Init()
if err != nil {
    // handle init fail
}

In the above example, we've initialized the engine to detect the built-in wake word "Picovoice". Built-in keywords are constants in the package with the BuiltInKeyword type.

Porcupine can detect multiple keywords concurrently:

porcupine := Porcupine{
    AccessKey: "${ACCESS_KEY}", // AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
    BuiltInKeywords: []BuiltInKeyword{PICOVOICE, BUMBLEBEE}}
err := porcupine.Init()

To detect non-default keywords, use KeywordPaths parameter instead:

porcupine := Porcupine{
    AccessKey: "${ACCESS_KEY}", // AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
    KeywordPaths: []string{"/path/to/keyword.ppn"}}
err := porcupine.Init()

In addition to custom keywords, you can override the default Porcupine english model file and/or keyword sensitivities.

Sensitivity is the parameter that enables trading miss rate for the false alarm rate. It is a floating-point number within [0, 1]. A higher sensitivity reduces the miss rate at the cost of increased false alarm rate.

The model file contains the parameters for the wake word engine. To change the language that Porcupine understands, you'll pass in a different model file.

porcupine := Porcupine{
    AccessKey: "${ACCESS_KEY}", // AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
    BuiltInKeywords: []BuiltInKeyword{PICOVOICE, BUMBLEBEE},
    Sensitivities: []float32{0.4, 0.9},
    ModelPath: "/path/to/model.pv"}
err := porcupine.Init()

When initialized, the valid sample rate is given by SampleRate. Expected frame length (number of audio samples in an input array) is given by FrameLength. The engine accepts 16-bit linearly-encoded PCM and operates on single-channel audio.

To feed audio into Porcupine, use the Process function in your capture loop. You must call Init() before calling Process.

func getNextFrameAudio() []int16{
    // get audio frame
}

for {
    keywordIndex, err := porcupine.Process(getNextFrameAudio())
    if keywordIndex >= 0 {
        // wake word detected!
    }
}

When done resources have to be released explicitly.

porcupine.Delete()

Using a defer call to Delete() after Init() is also a good way to ensure cleanup.

Non-English Wake Words

In order to detect non-English wake words you need to use the corresponding model file. The model files for all supported languages are available here.

Demos

Check out the Porcupine Go demos here

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Number of audio samples per frame.
	FrameLength int

	// Audio sample rate accepted by Picovoice.
	SampleRate int

	// Porcupine version
	Version string
)

List of available built-in wake words

Functions

This section is empty.

Types

type BuiltInKeyword

type BuiltInKeyword string

BuiltInKeyword Type

const (
	ALEXA       BuiltInKeyword = "alexa"
	AMERICANO   BuiltInKeyword = "americano"
	BLUEBERRY   BuiltInKeyword = "blueberry"
	BUMBLEBEE   BuiltInKeyword = "bumblebee"
	COMPUTER    BuiltInKeyword = "computer"
	GRAPEFRUIT  BuiltInKeyword = "grapefruit"
	GRASSHOPPER BuiltInKeyword = "grasshopper"
	HEY_GOOGLE  BuiltInKeyword = "hey google"
	HEY_SIRI    BuiltInKeyword = "hey siri"
	JARVIS      BuiltInKeyword = "jarvis"
	OK_GOOGLE   BuiltInKeyword = "ok google"
	PICOVOICE   BuiltInKeyword = "picovoice"
	PORCUPINE   BuiltInKeyword = "porcupine"
	TERMINATOR  BuiltInKeyword = "terminator"
)

Available built-in wake words constants

func (BuiltInKeyword) IsValid

func (k BuiltInKeyword) IsValid() bool

Checks if a given BuiltInKeyword is valid

type Porcupine

type Porcupine struct {

	// AccessKey obtained from Picovoice Console (https://console.picovoice.ai/).
	AccessKey string

	// Absolute path to Porcupine's dynamic library.
	LibraryPath string

	// Absolute path to the file containing model parameters.
	ModelPath string

	// Sensitivity values for detecting keywords. Each value should be a number within [0, 1]. A
	// higher sensitivity results in fewer misses at the cost of increasing the false alarm rate.
	Sensitivities []float32

	// List of built-in keywords to use.
	BuiltInKeywords []BuiltInKeyword

	// Absolute paths to keyword model files.
	KeywordPaths []string
	// contains filtered or unexported fields
}

Porcupine struct

func (*Porcupine) Delete

func (porcupine *Porcupine) Delete() error

Releases resources acquired by Porcupine.

func (*Porcupine) Init

func (porcupine *Porcupine) Init() error

Init function for Porcupine. Must be called before attempting process

func (*Porcupine) Process

func (porcupine *Porcupine) Process(pcm []int16) (keywordIndex int, err error)

Processes a frame of the incoming audio stream and emits the detection result. Frame of audio The number of samples per frame can be attained by calling `.FrameLength`. The incoming audio needs to have a sample rate equal to `.Sample` and be 16-bit linearly-encoded. Porcupine operates on single-channel audio. Returns a 0 based index if keyword was detected in frame. Returns -1 if no detection was made.

type PorcupineError

type PorcupineError struct {
	StatusCode   PvStatus
	Message      string
	MessageStack []string
}

func (*PorcupineError) Error

func (e *PorcupineError) Error() string

type PvStatus

type PvStatus int

PvStatus type

const (
	SUCCESS                  PvStatus = 0
	OUT_OF_MEMORY            PvStatus = 1
	IO_ERROR                 PvStatus = 2
	INVALID_ARGUMENT         PvStatus = 3
	STOP_ITERATION           PvStatus = 4
	KEY_ERROR                PvStatus = 5
	INVALID_STATE            PvStatus = 6
	RUNTIME_ERROR            PvStatus = 7
	ACTIVATION_ERROR         PvStatus = 8
	ACTIVATION_LIMIT_REACHED PvStatus = 9
	ACTIVATION_THROTTLED     PvStatus = 10
	ACTIVATION_REFUSED       PvStatus = 11
)

Possible status return codes from the Porcupine library

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL