picovoice

package module
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: Apache-2.0 Imports: 3 Imported by: 2

README

Picovoice SDK for Go

Picovoice

Made in Vancouver, Canada by Picovoice

Picovoice is an end-to-end platform for building voice products on your terms. It enables creating voice experiences similar to Alexa and Google. But it entirely runs 100% on-device. Picovoice is

  • Private: Everything is processed offline. Intrinsically HIPAA and GDPR-compliant.
  • Reliable: Runs without needing constant connectivity.
  • Zero Latency: Edge-first architecture eliminates unpredictable network delay.
  • Accurate: Resilient to noise and reverberation. It outperforms cloud-based alternatives by wide margins *.
  • Cross-Platform: Design once, deploy anywhere. Build using familiar languages and frameworks.

Compatibility

  • Go 1.16+
  • Runs on Linux (x86_64), macOS (x86_64, arm64), Windows (x86_64), Raspberry Pi, NVIDIA Jetson (Nano) and BeagleBone

AccessKey

Picovoice requires a valid Picovoice AccessKey at initialization. AccessKey acts as your credentials when using Picovoice 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.

Installation

go get github.com/Picovoice/picovoice/sdk/go/v2

Depending on your setup you also may need to run go mod tidy after in order to download transitive dependencies.

Usage

To create an instance of the engine with default parameters, use the NewPicovoice function. You must provide a Porcupine keyword file, a wake word detection callback function, a Rhino context file and an inference callback function. You must then make a call to Init().

. "github.com/Picovoice/picovoice/sdk/go/v2"
rhn "github.com/Picovoice/rhino/binding/go/v2"

const accessKey string = "${ACCESS_KEY}" // obtained from Picovoice Console (https://console.picovoice.ai/)

keywordPath := "/path/to/keyword/file.ppn"
wakeWordCallback := func(){
    // let user know wake word detected
}

contextPath := "/path/to/keyword/file.rhn"
inferenceCallback := func(inference rhn.RhinoInference){
    if inference.IsUnderstood {
            intent := inference.Intent
            slots := inference.Slots
        // add code to take action based on inferred intent and slot values
    } else {
        // add code to handle unsupported commands
    }
}

picovoice := NewPicovoice(
    accessKey,
    keywordPath,
    wakeWordCallback,
    contextPath,
    inferenceCallback)

err := picovoice.Init()
if err != nil {
    // handle error
}

Upon detection of wake word defined by keywordPath it starts inferring user's intent from the follow-on voice command within the context defined by the file located at contextPath. accessKey is your Picovoice AccessKey. keywordPath is the absolute path to Porcupine wake word engine keyword file (with .ppn suffix). contextPath is the absolute path to Rhino Speech-to-Intent engine context file (with .rhn suffix). wakeWordCallback is invoked upon the detection of wake phrase and inferenceCallback is invoked upon completion of follow-on voice command inference.

When instantiated, valid sample rate can be obtained via SampleRate. Expected number of audio samples per frame is FrameLength. The engine accepts 16-bit linearly-encoded PCM and operates on single-channel audio.

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

for {
    err := picovoice.Process(getNextFrameAudio())
}

When done resources have to be released explicitly

picovoice.Delete()

Non-English Models

In order to detect wake words and run inference in other languages you need to use the corresponding model file. The model files for all supported languages are available here and here.

Demos

Check out the Picovoice Go demos here

Documentation

Index

Constants

This section is empty.

Variables

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

	// Required sample rate of input audio
	SampleRate int

	// Version of Porcupine being used
	PorcupineVersion string

	// Version of Rhino being used
	RhinoVersion string

	// Picovoice version
	Version string
)

Functions

This section is empty.

Types

type InferenceCallbackType

type InferenceCallbackType func(rhn.RhinoInference)

Callback for when Rhino has made an inference

type Picovoice

type Picovoice struct {

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

	// Path to Porcupine keyword file (.ppn)
	KeywordPath string

	// Function to be called once the wake word has been detected
	WakeWordCallback WakeWordCallbackType

	// Path to Rhino context file (.rhn)
	ContextPath string

	// Function to be called once Rhino has an inference ready
	InferenceCallback InferenceCallbackType

	// Path to Porcupine dynamic library file (.so/.dylib/.dll)
	PorcupineLibraryPath string

	// Path to Porcupine model file (.pv)
	PorcupineModelPath string

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

	// Path to Rhino dynamic library file (.so/.dylib/.dll)
	RhinoLibraryPath string

	// Path to Rhino model file (.pv)
	RhinoModelPath string

	// Inference sensitivity. A higher sensitivity value results in
	// fewer misses at the cost of (potentially) increasing the erroneous inference rate.
	// Sensitivity should be a floating-point number within 0 and 1.
	RhinoSensitivity float32

	// Endpoint duration in seconds. An endpoint is a chunk of silence at the end of an
	// utterance that marks the end of spoken command. It should be a positive number within [0.5, 5]. A lower endpoint
	// duration reduces delay and improves responsiveness. A higher endpoint duration assures Rhino doesn't return inference
	// pre-emptively in case the user pauses before finishing the request.
	EndpointDurationSec float32

	// If set to `true`, Rhino requires an endpoint (chunk of silence) before finishing inference.
	RequireEndpoint bool

	// Once initialized, stores the source of the Rhino context in YAML format. Shows the list of intents,
	// which expressions map to those intents, as well as slots and their possible values.
	ContextInfo string
	// contains filtered or unexported fields
}

Picovoice struct

func NewPicovoice

func NewPicovoice(
	accessKey string,
	keywordPath string,
	wakewordCallback WakeWordCallbackType,
	contextPath string,
	inferenceCallback InferenceCallbackType) Picovoice

Returns a Picovoice struct with default parameters

func (*Picovoice) Delete

func (picovoice *Picovoice) Delete() error

Releases resources acquired by Picovoice

func (*Picovoice) Init

func (picovoice *Picovoice) Init() error

Init function for Picovoice. Must be called before attempting process.

func (*Picovoice) Process

func (picovoice *Picovoice) Process(pcm []int16) error

Process a frame of pcm audio with the Picovoice platform. Invokes user-defined callbacks upon detection of wake word and completion of follow-on command inference

type PicovoiceError

type PicovoiceError struct {
	StatusCode PvStatus
	Message    string
	InnerError error
}

func (*PicovoiceError) Error

func (e *PicovoiceError) Error() string

type PvStatus

type PvStatus int

PvStatus describes error codes returned from native code

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
)

type WakeWordCallbackType

type WakeWordCallbackType func()

Callback for when a wake word has been detected

Jump to

Keyboard shortcuts

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