DEV Community

Cover image for Day 9: Making Cool Raspberry Pi Projects even Cooler with Voice AI (4/4)
Dilek Karasoy for Picovoice

Posted on

Day 9: Making Cool Raspberry Pi Projects even Cooler with Voice AI (4/4)

Last tutorial for adding voice recognition to Raspberry Pi projects is Cobra Voice Activity Detection.

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

Cobra supports

  • Raspberry Pi Zero (W/WH/2W)
  • Raspberry Pi 2
  • Raspberry Pi 3
  • Raspberry Pi 4
  • Raspberry Pi 400

This tutorial showcases how to run Voice Activity Detection on Raspberry Pi using C. For other SDK's visit Cobra docs page on picovoice.ai

Compatibility
C99-compatible compiler
AccessKey
Grab your AccessKey from Picovoice Console, if you haven't. It's free!
Requirements
CMake version 3.4 or higher
Microphone Demo
The Cobra microphone demo opens an audio stream and detects the presence of speech. Commands are run from the root of the repo.
Build
Use CMake to build the Cobra microphone demo target:

cmake -S demo/c/ -B demo/c/build && cmake --build demo/c/build --target cobra_demo_mic
Enter fullscreen mode Exit fullscreen mode

Usage
Run the executable without any command-line arguments prints the usage info to the console:

Usage: ./cobra_demo_mic [-s] [-l LIBRARY_PATH -a ACCESS_KEY -d AUDIO_DEVICE_INDEX]
Enter fullscreen mode Exit fullscreen mode

List the available audio input devices:

./demo/c/build/cobra_demo_mic -s
Enter fullscreen mode Exit fullscreen mode

Run the cobra microphone demo:

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

Replace ${LIBRARY_PATH} with path to appropriate library available under lib, ${ACCESS_KEY} with AccessKey obtained from Picovoice Console, and ${AUDIO_DEVICE_INDEX} with the index of the audio device you wish to capture audio with. An ${AUDIO_DEVICE_INDEX} of -1 will provide you with your system's default recording device.
File Demo
The file demo will analyze an audio file for the presence of voice and print the probability of voice for each frame of audio. This demo expects a single-channel WAV file with a sampling rate of 16000 and 16-bit linear PCM encoding.
Note: the following commands are run from the root of the repo
Build
Use CMake to build the Cobra file demo target:

cmake -S demo/c/ -B demo/c/build && cmake --build demo/c/build --target cobra_demo_file
Enter fullscreen mode Exit fullscreen mode

Usage
Run the demo:

./demo/c/build/cobra_demo_file -l ${LIBRARY_PATH} -a ${ACCESS_KEY} -w ${INPUT_WAV_FILE}
Enter fullscreen mode Exit fullscreen mode

Replace ${LIBRARY_PATH} with the path to the appropriate Cobra library available under lib, ${ACCESS_KEY} with your AccessKey obtained from the Picovoice Console, and ${INPUT_WAV_FILE} with the path to the WAV file you wish to analyze.

Top comments (0)