Cobra is a highly-accurate and lightweight voice activity detection (VAD) engine.

Overview

Cobra

GitHub

PyPI Maven Central Cocoapods Crates.io

Made in Vancouver, Canada by Picovoice

Twitter URL YouTube Channel Views

Cobra is a highly-accurate and lightweight voice activity detection (VAD) engine.

Table of Contents

Demos

Python Demos

Install the demo package:

sudo pip3 install pvcobrademo

With a working microphone connected to your device run the following in the terminal:

cobra_demo_mic --access_key ${AccessKey}

Replace ${AccessKey} with your AccessKey obtained from Picovoice Console. Cobra starts processing the audio input from the microphone in realtime and outputs to the terminal when it detects any voice activities.

For more information about the Python demos go to demo/python.

C Demos

Build the demo:

cmake -S demo/c/ -B demo/c/build && cmake --build demo/c/build --target cobra_demo_mic

To list the available audio input devices:

./demo/c/build/cobra_demo_mic -s

To run the demo:

./demo/c/build/cobra_demo_mic -l ${LIBRARY_PATH} -a ${ACCESS_KEY} -d ${AUDIO_DEVICE_INDEX}

Replace ${LIBRARY_PATH} with path to appropriate library available under lib, Replace ${ACCESS_KEY} with AccessKey obtained from Picovoice Console, and ${INPUT_AUDIO_DEVICE} with the index of your microphone device.

For more information about C demos go to demo/c.

Android Demos

Using Android Studio, open demo/android/Activity as an Android project and then run the application. Replace String ACCESS_KEY = "..." inside MainActivity.java with your AccessKey generated by Picovoice Console.

For more information about Android demos go to demo/android.

iOS demos

Run the following from this directory to install the Cobra-iOS CocoaPod:

pod install

Replace let ACCESS_KEY = "..." inside ViewModel.swift with yours obtained from Picovoice Console.

Then, using Xcode, open the generated CobraDemo.xcworkspace and run the application. Press the start button and start talking. The background will change colour while you're talking.

For more information about iOS demos go to demo/ios.

Web Demos

From demo/web run the following in the terminal:

yarn
yarn start

(or)

npm install
npm run start

Open http://localhost:5000 in your browser to try the demo.

SDKs

Python

Install the Python SDK:

pip3 install pvcobra

The SDK exposes a factory method to create instances of the engine:

import pvcobra

handle = pvcobra.create(access_key=${AccessKey})

where ${AccessKey} is an AccessKey which should be obtained from Picovoice Console. When initialized, valid sample rate can be obtained using handle.sample_rate. The required frame length (number of audio samples in an input array) is handle.frame_length. The object can be used to monitor incoming audio as follows:

def get_next_audio_frame():
    pass

while True:
    voice_probability = handle.process(get_next_audio_frame())

Finally, when done be sure to explicitly release the resources using handle.delete().

C

include/pv_cobra.h header file contains relevant information. Build an instance of the object:

    pv_cobra_t *handle = NULL;
    pv_status_t status = pv_cobra_init(${ACCESS_KEY}, &handle);
    if (status != PV_STATUS_SUCCESS) {
        // error handling logic
    }

Replace ${ACCESS_KEY} with the AccessKey obtained from Picovoice Console. Now the handle can be used to monitor incoming audio stream. Cobra accepts single channel, 16-bit linearly-encoded PCM audio. The sample rate can be retrieved using pv_sample_rate(). Finally, Cobra accepts input audio in consecutive chunks (aka frames) the length of each frame can be retrieved using pv_cobra_frame_length().

extern const int16_t *get_next_audio_frame(void);

while (true) {
    const int16_t *pcm = get_next_audio_frame();
    float is_voiced = 0.f;
    const pv_status_t status = pv_cobra_process(handle, pcm, &is_voiced);
    if (status != PV_STATUS_SUCCESS) {
        // error handling logic
    }
}

Finally, when done be sure to release the acquired resources:

pv_cobra_delete(handle);

Android

Create an instance of the engine

import ai.picovoice.cobra.Cobra;
import ai.picovoice.cobra.CobraException;

String accessKey = // .. AccessKey provided by Picovoice Console (https://picovoice.ai/console/)
try {
    handle = new Cobra(accessKey);
} catch (CobraException e) {
    // handle error
}

When initialized, valid sample rate can be obtained using handle.getSampleRate(). The required frame length (number of audio samples in an input array) is handle.getFrameLength(). The object can be used to monitor incoming audio as follows:

short[] getNextAudioFrame(){

while(true) {
    try {
        final float voiceProbability = handle.process(getNextAudioFrame());
    } catch (CobraException e) { }
}

Finally, when done be sure to explicitly release the resources using handle.delete().

iOS

To import the Cobra iOS binding into your project, add the following line to your Podfile and run pod install:

pod 'Cobra-iOS'

Create an instance of the engine

import Cobra

let accessKey : String = // .. AccessKey provided by Picovoice Console (https://picovoice.ai/console/)
do {
    handle = try Cobra(accessKey: accessKey)
} catch { }

func getNextAudioFrame() -> [Int16] {
    // .. get audioFrame
    return audioFrame;
}

while true {
    do {
        let voiceProbability = try handle.process(getNextAudioFrame())
    } catch { }
}

Finally, when done be sure to explicitly release the resources using handle.delete().

Web

Cobra is available on modern web browsers (i.e., not Internet Explorer) via WebAssembly. Cobra is provided pre-packaged as a Web Worker to allow it to perform processing off the main thread.

The Cobra package @picovoice/cobra-web-worker can be used with the @picovoice/web-voice-processor. Microphone audio is handled via the Web Audio API and is abstracted by the WebVoiceProcessor, which also handles down-sampling to the correct format.

Vanilla JavaScript and HTML (CDN Script Tag)

">
>
<html lang="en">
  <head>
    <script src="https://unpkg.com/@picovoice/cobra-web-worker/dist/iife/index.js">script>
    <script src="https://unpkg.com/@picovoice/web-voice-processor/dist/iife/index.js">script>
    <script type="application/javascript">
      function cobraCallback(voiceProbability) {
        const threshold = 0.8;
        if voiceProbability >= threshold {
          const timestamp = new Date();
          console.log("Voice detected with probability of " +
            voiceProbability.toFixed(2) +
            " at " +
            timestamp.toString()
          );
        }
      }

      async function startCobra() {
        const accessKey = // AccessKey string provided by Picovoice Console (picovoice.ai/console/)
        const cobraWorker = await CobraWorkerFactory.create(
          accessKey,
          cobraCallback
        );

        console.log("Cobra worker ready!");

        console.log("WebVoiceProcessor initializing. Microphone permissions requested ...");

        try {
          let webVp = await window.WebVoiceProcessor.WebVoiceProcessor.init({
            engines: [cobraWorker],
          });
          console.log("WebVoiceProcessor ready and listening!");
        } catch (e) {
          console.log("WebVoiceProcessor failed to initialize: " + e);
        }
      }

      document.addEventListener("DOMContentLoaded", function () {
        startCobra();
      });
    script>
  head>
  <body>body>
html>

Vanilla JavaScript and HTML (ES Modules)

yarn add @picovoice/cobra-web-worker @picovoice/web-voice-processor

(or)

npm install @picovoice/cobra-web-worker @picovoice/web-voice-processor
= threshold { const timestamp = new Date(); console.log("Voice detected with probability of " + voiceProbability.toFixed(2) + " at " + timestamp.toString() ); } } async function startCobra() { const accessKey = //AccessKey string provided by Picovoice Console (picovoice.ai/console/) const cobraWorker = await CobraWorkerFactory.create( accessKey, cobraCallback ); const webVp = await WebVoiceProcessor.init({ engines: [cobraWorker], start: true, }); } startCobra() ">
import { WebVoiceProcessor } from "@picovoice/web-voice-processor"
import { CobraWorkerFactory } from "@picovoice/cobra-web-worker";

function cobraCallback(voiceProbability) {
  const threshold = 0.8;
  if voiceProbability >= threshold {
    const timestamp = new Date();
    console.log("Voice detected with probability of " +
      voiceProbability.toFixed(2) +
      " at " +
      timestamp.toString()
    );
  }
}

async function startCobra() {
  const accessKey = //AccessKey string provided by Picovoice Console (picovoice.ai/console/)
  const cobraWorker = await CobraWorkerFactory.create(
      accessKey,
      cobraCallback
  );

  const webVp =
      await WebVoiceProcessor.init({
        engines: [cobraWorker],
        start: true,
      });
}

startCobra()

Releases

v1.0.0 Oct 8th, 2021

  • Initial release.
Comments
  • Cobra Issue: file lib/raspberry-pi/cortex-a53-aarch64/libpv_cobra.so corrupted

    Cobra Issue: file lib/raspberry-pi/cortex-a53-aarch64/libpv_cobra.so corrupted

    Expected an ELF format file of approximately the same size as other shared libraries.

    This file shrank by about half since the previous version, and does not appear to be an ELF file at all.

    bug 
    opened by randy-lcs 11
  • Cobra Issue: running alongside Porcupine Angular creates `IO_ERROR`

    Cobra Issue: running alongside Porcupine Angular creates `IO_ERROR`

    Cobra stops working when porcupine service in installed and used in angular

    getting this error

    core.mjs:6494 ERROR Error: Uncaught (in promise): Error: 'pv_cobra_init' failed with status IO_ERROR at resolvePromise (zone.js:1211:1) at zone.js:1118:1 at asyncGeneratorStep (asyncToGenerator.js:6:1) at _throw (asyncToGenerator.js:29:1) at _ZoneDelegate.invoke (zone.js:372:1) at Object.onInvoke (core.mjs:25608:1) at _ZoneDelegate.invoke (zone.js:371:1) at Zone.run (zone.js:134:1) at zone.js:1275:1 at _ZoneDelegate.invokeTask (zone.js:406:1)

    when working with only cobra VAD it works fine, but once i installed porcupine service it stops working

    help wanted 
    opened by sandeepsn1997 6
  • Cobra Issue: error when starting the cobra_mic_demo on a raspberry pi 4

    Cobra Issue: error when starting the cobra_mic_demo on a raspberry pi 4

    When trying so lunch the cobra_mic_demo using the python sdk and after well installing the package. an error occurs

    Expected behaviour

    The normal start of the cobra_demo_mic (probability of speech)

    Actual behaviour

    Traceback (most recent call last): File "/home/pi/.local/lib/python3.9/site-packages/pvcobrademo/cobra_demo_mic.py", line 61, in run cobra = pvcobra.create( File "/home/pi/.local/lib/python3.9/site-packages/pvcobra/init.py", line 30, in create return Cobra(library_path=library_path, access_key=access_key) File "/home/pi/.local/lib/python3.9/site-packages/pvcobra/cobra.py", line 130, in init raise self._PICOVOICE_STATUS_TO_EXCEPTIONstatus pvcobra.cobra.CobraActivationError

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "/home/pi/.local/bin/cobra_demo_mic", line 8, in sys.exit(main()) File "/home/pi/.local/lib/python3.9/site-packages/pvcobrademo/cobra_demo_mic.py", line 130, in main CobraDemo( File "/home/pi/.local/lib/python3.9/site-packages/pvcobrademo/cobra_demo_mic.py", line 95, in run recorder.delete() AttributeError: 'NoneType' object has no attribute 'delete'

    Steps to reproduce the behaviour

    Launch the commande cobra_demo_mic --access_key TOKEN

    on raspbian bullseye 64 system (raspberry pi 4) 2 GB

    question 
    opened by AbdouBou 2
  • More fixes for rust

    More fixes for rust

    I guess this silences dependabot but it doesn't actually fix the underlying issue. The chrono crate which depends on the older version of time will still build and use the older version. Unfortunately chrono hasn't been updated yet.

    opened by ErisMik 0
  • access key update

    access key update

    • add access key docs to readmes

    NOTE: I will release packages after approve, update demos (python and node), so please check even if actions are failing.

    opened by ksyeo1010 0
Releases(v1.1)
Owner
Picovoice
Edge Voice AI Platform
Picovoice
🎵 A repository for manually annotating files to create labeled acoustic datasets for machine learning.

🎵 A repository for manually annotating files to create labeled acoustic datasets for machine learning.

Jim Schwoebel 28 Dec 22, 2022
Frescobaldi LilyPond Editor

README for Frescobaldi Homepage: http://www.frescobaldi.org/ Main author: Wilbert Berendsen Frescobaldi is a LilyPond sheet music text editor. It aims

Frescobaldi 600 Dec 29, 2022
Marsyas - Music Analysis, Retrieval and Synthesis for Audio Signals

Welcome to MARSYAS. MARSYAS is a software framework for rapid prototyping of audio applications, with flexibility and extensibility as primary concer

Marsyas Developers Group 364 Oct 31, 2022
GiantMIDI-Piano is a classical piano MIDI dataset contains 10,854 MIDI files of 2,786 composers

GiantMIDI-Piano is a classical piano MIDI dataset contains 10,854 MIDI files of 2,786 composers

Bytedance Inc. 1.3k Jan 04, 2023
:notes: Cross-platform music player

Exaile Exaile is a music player with a simple interface and powerful music management capabilities. Features include automatic fetching of album art,

Exaile 327 Dec 19, 2022
An audio digital processing toolbox based on a workflow/pipeline principle

AudioTK Audio ToolKit is a set of audio filters. It helps assembling workflows for specific audio processing workloads. The audio workflow is split in

Matthieu Brucher 238 Oct 18, 2022
[Singing Log] Let your program learn to sing!

[Singing Log] Let your program learn to sing! You must have thought this was changelog when you saw the English title, but it's not, it's chànggēlog. What it does is allow your program to print logs

黄巍 22 Sep 03, 2022
OpenClubhouse - A third-part web application based on flask to play Clubhouse audio.

OpenClubhouse - A third-part web application based on flask to play Clubhouse audio.

1.1k Jan 05, 2023
IDing the songs played on the do you radio show

IDing the songs played on the do you radio show

Rasmus Jones 36 Nov 15, 2022
Audio Retrieval with Natural Language Queries: A Benchmark Study

Audio Retrieval with Natural Language Queries: A Benchmark Study Paper | Project page | Text-to-audio search demo This repository is the implementatio

21 Oct 31, 2022
Open-Source bot to play songs in your Telegram's Group Voice Chat. Powered by @Akki_ThePro

VcPlayer Telegram Voice-Chat Bot [PyTGCalls] ⇝ Requirements ⇜ Account requirements A Telegram account to use as the music bot, You cannot use regular

Akki ThePro 2 Dec 25, 2021
Audio book player for senior visually impaired.

PI Zero W Audio Book Motivation and requirements My dad is practically blind and at 80 years has trouble hearing and operating tiny or more complicate

Andrej Hosna 29 Dec 25, 2022
A Python wrapper for the high-quality vocoder "World"

PyWORLD - A Python wrapper of WORLD Vocoder Linux Windows WORLD Vocoder is a fast and high-quality vocoder which parameterizes speech into three compo

Jeremy Hsu 583 Dec 15, 2022
Pyrogram bot to automate streaming music in voice chats

Pyrogram bot to automate streaming music in voice chats Help If you face an error, want to discuss this project or get support for it, join it's group

Roj 124 Oct 21, 2022
Royal Music You can play music and video at a time in vc

Royals-Music Royal Music You can play music and video at a time in vc Commands SOON String STRING_SESSION Deployment 🎖 Credits • 🇸ᴏᴍʏᴀ⃝🇯ᴇᴇᴛ • 🇴ғғɪ

2 Nov 23, 2021
MusicBrainz Picard

MusicBrainz Picard MusicBrainz Picard is a cross-platform (Linux/Mac OS X/Windows) application written in Python and is the official MusicBrainz tagge

MetaBrainz Foundation 3k Dec 31, 2022
Audio augmentations library for PyTorch for audio in the time-domain

Audio augmentations library for PyTorch for audio in the time-domain, with support for stochastic data augmentations as used often in self-supervised / contrastive learning.

Janne 166 Jan 08, 2023
The official repository for Audio ALBERT

AALBERT Here is also the official repository of AALBERT, which is Pytorch lightning reimplementation of the paper, Audio ALBERT: A Lite Bert for Self-

pohan 55 Dec 11, 2022
Voice to Text using Raspberry Pi

This module will help to convert your voice (speech) into text using Speech Recognition Library. You can control the devices or you can perform the desired tasks by the word recognition

Raspberry_Pi Pakistan 2 Dec 15, 2021
Python tools for the corpus analysis of popular music.

CATCHY Corpus Analysis Tools for Computational Hook discovery Python tools for the corpus analysis of popular music recordings. The tools can be used

Jan VB 20 Aug 20, 2022