cobra-web
The Picovoice Cobra library for web browsers, powered by WebAssembly. Intended (but not required) to be used with the @picovoice/web-voice-processor package.
This library processes audio for voice detection in-browser, offline. All processing is done via WebAssembly and Workers in a separate thread.
Compatibility
- Chrome / Edge
- Firefox
- Safari
This library requires several modern browser features: WebAssembly, Web Workers, and promises. Internet Explorer will not work.
If you are using this library with the @picovoice/web-voice-processor to access the microphone, that requires some additional browser features like Web Audio API. Its overall browser support is approximately the same.
AccessKey
Cobra requires a valid Picovoice AccessKey
at initialization. AccessKey
acts as your credentials when using Cobra 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
.
Packages
The Cobra SDK for Web is split into separate worker and factory packages; import each as required.
Workers
For typical cases, use the worker package. The worker package creates complete CobraWorker
instances that can be immediately used with @picovoice/web-voice-processor.
Factories
Factory packages allow you to create instances of Cobra
directly. Useful for building your own custom Worker/Worklet, or some other bespoke purpose.
Installation & Usage
Worker
To obtain a CobraWorker
, we can use the static create
factory method from the CobraWorkerFactory. Here is a complete example that:
- Obtains a
CobraWorker
from the CobraWorkerFactory
- Responds to voice activity detection event by defining and passing a callback function
- Creates a
WebVoiceProcessor
to obtain microphone permission and forward microphone audio to the CobraWorker
E.g.:
yarn add @picovoice/web-voice-processor @picovoice/cobra-web-worker
import { WebVoiceProcessor } from "@picovoice/web-voice-processor"
import { CobraWorkerFactory } from "@picovoice/cobra-web-worker";
function cobraCallback(voiceProbability) {
const threshold =
if voiceProbability >= threshold {
}
}
async function startCobra() {
const accessKey =
const cobraWorker = await CobraWorkerFactory.create(
accessKey,
cobraCallback
);
const webVp =
await WebVoiceProcessor.init({
engines: [cobraWorker],
start: true,
});
}
startCobra()
...
if (done) {
webVp.release()
cobraWorker.sendMessage({
command: "release"
})
}
Factory
If you wish to build your own worker, or perhaps not use workers at all, use the factory packages. This will let you instantiate Cobra engine instances directly.
The audio passed to the worker in the process
function must be of the correct format. The WebVoiceProcessor
handles downsampling in the examples above to standard voice recognition format (16-bit, 16kHz linear PCM, single-channel). Use an Int16Array
typed array. If you are not using WebVoiceProcessor
, you must ensure the audio passed to Cobra is of that format. The Cobra instance provides the length of the array required via .frameLength
.
E.g.:
import { Cobra } from "@picovoice/cobra-web-factory";
async function startCobra() {
const accessKey =
const handle = await Cobra.create(accessKey);
return handle;
}
const cobraHandle = startCobra()
const audioFrames = new Int16Array( )
const cobraResult = cobraHandle.process(audioFrames)
...
Build from source (IIFE + ESM outputs)
This library uses Rollup and TypeScript along with Babel and other popular rollup plugins. There are two outputs: an IIFE version intended for script tags / CDN usage, and a JavaScript module version intended for use with modern JavaScript/TypeScript development (e.g. Angular, Create React App, Webpack).
yarn
yarn build
The output will appear in the ./dist/ folder.
For example usage refer to the web demo