Clap detection module for node js
Switch branches/tags
Nothing to show
Clone or download
Latest commit c00ad6a Dec 7, 2016
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore fix readme Jan 18, 2016
README.md Actually make a change with the updateConfig example instead of sendi… Nov 26, 2016
index.js Quoting the filename actually broke it. Nov 28, 2016
package.json publish Dec 7, 2016

README.md

Clap detection module for node js

Synopsis

ClapDetector is a hand clap detection module for nodejs (iojs). It detects a clap or a series of claps and allows you to trigger callbacks whenever these events happen. I created this module for my personal assistant project on a Raspberry Pi (raspbian). The clap detection allows me to activate the assistant whenever I need it (and prevents it from continuously listening out for instructions or interpreting random noises as instructions)

Requirements

This module works on linux based OS (raspbian, Ubuntu, Debian...) using alsa for audio and a working microphone or Mac OS X using coreaudio.

Installation

This module requires sox, "the Swiss Army knife of sound processing programs" (http://sox.sourceforge.net/) to be installed

Linux

sudo apt-get install sox

Mac OS X

brew install sox

npm install

You can simply add this module to your node.js project with

// sudo might be required depending on your system
npm install --save clap-detector

Usage

There are four public methods you can use:

  • clapDetector.start(clapConfig); => this needs to be called to initialize clap detection. The clapConfig object is not mandatory but you can use it to overwrite the default configuration (see next section)
  • clapDetector.onClap(yourcallbackfunctionhere) => register a callback that will be triggered whenever a clap of hand is detected. Your callback can accept an array of ids counting each clap and the corresponding timestamp.
  • clapDetector.onClaps(numberOfClaps, delay, yourcallbackfunctionhere) => register a callback that will be triggered whenever a series of claps (determined by the number of claps) is detected within the period of time you've specified (delay).
  • clapDetector.updateConfig(clapConfig); => updates configuration on-the-fly.
// Require the module
var clapDetector = require('clap-detector');

// Define configuration
var clapConfig = {
   AUDIO_SOURCE: 'alsa hw:1,0'// default for linux
};

// Start clap detection
clapDetector.start(clapConfig);

// Register on clap event
clapDetector.onClap(function(history) {
    //console.log('your callback code here ', history);
});

// Register to a series of 3 claps occuring within 2 seconds
clapDetector.onClaps(3, 2000, function(delay) {
    //console.log('your callback code here ');
});

// Update the configuration
clapDetector.updateConfig({CLAP_ENERGY_THRESHOLD: 0.2});

Configuration

You can pass a configuration object at the initialisation time (clapDetector.init(yourConfObject)). If you don't the following config will be used. You should at least provide the audio input (if different from the default config).

// DEFAULT CONFIG
var CONFIG = {
        AUDIO_SOURCE: 'hw:1,0', // this is your microphone input. If you don't know it you can refer to this thread (http://www.voxforge.org/home/docs/faq/faq/linux-how-to-determine-your-audio-cards-or-usb-mics-maximum-sampling-rate)
        DETECTION_PERCENTAGE_START : '5%', // minimum noise percentage threshold necessary to start recording sound
        DETECTION_PERCENTAGE_END: '5%',  // minimum noise percentage threshold necessary to stop recording sound
        CLAP_AMPLITUDE_THRESHOLD: 0.7, // minimum amplitude threshold to be considered as clap
        CLAP_ENERGY_THRESHOLD: 0.3,  // maximum energy threshold to be considered as clap
        MAX_HISTORY_LENGTH: 10 // all claps are stored in history, this is its max length
    };

If you wish to improve the clap detection you can fiddle with the CLAP_AMPLITUDE_THRESHOLD and CLAP_ENERGY_THRESHOLD values. Depending on your microphone these might need to be modified.

Tests

These will be added soon. Please do not hesitate to add some !

About the Author

I am a full-stack Javascript developer based in Lyon, France.

Check out my website

License

clap-detector is dual licensed under the MIT license and GPL. For more information click here.