Git Product home page Git Product logo

clap-detection's Introduction

clap-detection

Clap sequence/rhythm/pattern detection on Raspberry Pi using Csound and Python. This was made as a quick experiment. Don't use it for anything serious.

Why

Some examples of things you can have your Raspberry Pi do when a matching clap sequence is detected:

  • Dim the lights and start playing smooth jazz music
  • Turn on/off the TV
  • Broadcast a yo
  • Project the weather forecast on the wall

How

Csound takes in audio from a microphone live and checks the audio for transients. Whenever a transient (rapidly ascending amplitude) is detected, Csound will notice ClapAnalyzer, a class implemented in Python. ClapAnalyzer looks for a specific rhytmic clap sequence. ClapAnalyzer will notice all listeners whenever a matching clap sequence is detected.

Setup

  • Install Python 2.7
  • Install Csound

Usage

Python

Let's use the following rhythmic sequence as example:

clap

The note lengths are 1/4, 1/8, 1/8, 1/4, 1/4, so we set the note_lengths parameter to [0.25, 0.125, 0.125, 0.25, 0.25].

from clap import ClapAnalyzer

clap_analyzer = ClapAnalyzer(note_lengths=[0.25, 0.125, 0.125, 0.25, 0.25])

def clap_sequence_detected():
  print 'Matching clap sequence detected!'

clap_analyzer.on_clap_sequence(clap_sequence_detected)

# You can now start calling clap_analyzer.clap(time)

Basically, this is the python code that is used in clap.csd

Csound

Start csound from your command line. By default, the csound instrument will get live audio input:

csound clap.csd

If you want to quickly analyze a wav file, you can use that file instead of live audio input. This is good for testing:

csound clap.csd -i myfile.wav

PS: The file must be mono, not stereo, for this to work. And if your sound file is long, then you should modify the amount of time the Csound instrument stays alive accordingly, in order to analyze the whole file.

Troubleshooting

"no module named clap"

Try adding the directory with the python module dynamically:

pyruni "import sys, os"
pyruni "sys.path.append('/path/to/clap-detection')"

Edit /path/to/clap-detection to the place where clap.py is located.

"Segmentation fault" or "Unable to set number of channels on soundcard"

Check if your input device is mono or stereo. If it is mono (i.e. has only one channel), then you should set nchnls = 1 in your csound file, and you should use the in opcode instead of ins. If your input device is stereo, then you should set nchnls = 2.

ALSA and/or PortAudio warnings

Use the -+rtaudio=alsa option

Stuttering/crackling/noise/"Buffer underrun"

Let Csound use a large buffer in both software and hardware. In other word, use the following options: -b2048 -B2048

Input device error

Run arecord -l and check the list of sound cards and subdevices that are available. If you, for example, want to use card 1, subdevice 0, then you should use the following csound option: -i adc:hw:1,0

clap-detection's People

Contributors

iver56 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

clap-detection's Issues

Which sound card do you use?

I just encountered an input error with my Aureon Dual sound card.
unable to set number of channels...it said.

So I just wonder which sound card you use. And do you have any recommendation on sound card that works fine on raspberry pi?

Look for multiple clap rhythms at the same time

With this feature, one can have different clap rhythms that can trigger different actions. For example, one specific clap rhythm will turn on the lights while a different clap rhythm will turn on the TV.

boost input level possible?

Hi

I have a cheap microfone usb-stick but the input level is quite low.
In alsamixer I can go up to 23.81dB but its still to silent to recognize
my claps. Is it possible to somehow preprocess the input stream somehow
that it gets amplified? Is it correct to set 0dbfs = 23 in clap.csd?

Define pattern by musical notation beat lengths instead of pauses

As a musician, one might be more used to 'length of notes', instead of 'length of pauses'.

So in the example notes given

clap

Instead of defining the pattern to be [2, 1, 1, 2] as the relative pauses between 'notes',
you could define it in musical beats as [1./4, 1./8, 1./8, 1./4, 1./4].

Now, a 'converter' could find the smallest of these fractions, and do multiplications to convert it to the proper pattern format.

Improve clap detection using machine learning

Just looking for transients is not good enough because it results in a lot of false positives. A better approach would be machine learning

  • Discard background noise. Sample the noise while the spectrum is not in flux.
  • Train a neural network to recognize clap sounds but reject other types of sounds with a transient.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.