C++ Sample

Two samples are provided in C++ version of Cochl.Sense SDK: sense-file and sense-stream.

sense-flie makes prediction out of the input file, and sense-stream makes prediction out of the input stream such as a microphone.

This guide walks through how to run the samples.

1. Check Requirements

Follow Getting started and set up environments.

2. Prepare Sample

Prepare sample

$ git clone https://github.com/cochlearai/sense-sdk-cpp-tutorials.git

Unzip SDK

$ unzip path/to/sdk/sense-sdk-1.0-cpp.zip -d path/to/sample/sense-sdk-cpp-tutorials/

The prepared sample directory

└── sense-sdk-cpp-tutorials
    ├── audio_files          # sample audio files
    ├── examples
    |   ├── AudioFile.h
    |   ├── sense-file.cc    # example for file input
    │   └── sense-stream.cc  # example for stream input
    └── sense                # SDK
        ├── include
        └── lib

3. Set Up Parameters

Set up parameters with values of your choice before you build and run the C++ sample.

It can be set in the SDK file sense/include/sense.hpp. Check possible options and set your parameters.

For example, change the value of service to Service::Home_Context if you want to detect baby crying or dog barking.


// sense.hpp

struct Parameters {
  AudioFormat audio_format = AudioFormat::AF_FLOAT32;
  ModelDelegate model_delegate = ModelDelegate::Default;
  Service service = Service::Emergency;
  int num_threads = 0;


Possible Service options

// sense.hpp
enum Service {
  Emergency = 0,
  Human_Interaction = 1,
  Human_Status = 2,
  Home_Context = 3

Possible AudioFormat options

// sense.hpp
enum AudioFormat {

4. Build

Set your Project Key into the sample code.

// examples/sense-file.cc

if (sense::Init("YOUR_PROJECT_KEY_HERE", sense_params) < 0) {
  return -1;

Then it’s ready to build sense-file.

$ g++ -fopenmp examples/sense-file.cc -I./sense/include/ -lsense-core -L./sense/lib -o sense-file -lm -std=c++11 -ldl -lstdc++ -Wl,-rpath -Wl,./sense/lib

Set your Project Key into the sample code.

// examples/sense-stream.cc

if (sense::Init("YOUR_PROJECT_KEY_HERE", sense_params) < 0) {
  return -1;

Then it’s ready to build sense-stream.

$ g++ -fopenmp examples/sense-stream.cc -I./sense/include/ -lsense-core -L./sense/lib -o sense-stream -lm -std=c++11 -ldl -lstdc++ -lpulse -lpulse-simple -Wl,-rpath -Wl,./sense/lib

5. Run


Sample audio files in the SDK directory can be used as well.

$ LD_LIBRARY_PATH=. ./sense-file audio_files/babycry.wav
$ LD_LIBRARY_PATH=. ./sense-file audio_files/carhorn.wav
$ LD_LIBRARY_PATH=. ./sense-file audio_files/cough.wav
$ LD_LIBRARY_PATH=. ./sense-file audio_files/dogbark.wav
$ LD_LIBRARY_PATH=. ./sense-file audio_files/glassbreak.wav
$ LD_LIBRARY_PATH=. ./sense-file audio_files/siren.wav
$ LD_LIBRARY_PATH=. ./sense-file audio_files/snoring.wav
$ LD_LIBRARY_PATH=. ./sense-file audio_files/toilet_flush.wav
$ LD_LIBRARY_PATH=. ./sense-file audio_files/whistle.wav

Make sure the input device is properly connected before you run sense-stream

$ LD_LIBRARY_PATH=. ./sense-stream

(Note) sense-stream troubleshooting

Try the steps below when you have a trouble running sense-stream

(1) Check connection

  • Check if the input device is connected

(2) Check power

  • The board may lack of power when an external device is connected
  • Check if the board gets stable and enough power

(3) Check pulseaudio

# start pulseaudio
$ pulseaudio -D

# check your device index from the pulseaudio command result
$ pacmd list-sources | grep -e 'index:' -e device.string -e 'name:'

# set default source with the index above
$ pacmd set-default-source <DEVICE_INDEX>