This project implements a clap detection system using an a mic or raw audio data as input. It can detect clap patterns including single and double claps.
If you found this repository useful, please give it a ⭐!.
- Clap pattern detection.
- Dynamic threshold adjustment for robust clap detection.
- Bandpass filtering to focus on clap frequencies.
- Audio recording and saving capabilities.

-
If clap-detector fails to install due to pyaudio issues, try to install
portaudio19usingsudo apt install portaudio19-dev, then install clap-detector normally usingpip install clap-detector. -
If pyaudio still fails to install after trying option A, try to install it using
sudo apt install python3-pyaudio, then install clap-detector normallypip install clap-detector.
- If there are issues with audio input, check the inputDevice in the
ClapDetectorconstructor.
- Adjust the bandpass filter parameters for better clap detection in different environments.
- Python3
- PyAudio
- NumPy
- SciPy
- install from the official pypi package:
pip install clap-detector
-
Install the required Python packages:
pip install pyaudio numpy scipy
-
Clone the repository:
git clone https://github.com/TzurSoffer/clapDetection/ cd clapDetection/src/clapDetector -
Run the clap detection script:
python clapDetector.py
-
Adjust parameters in the
ClapDetectorclass constructor to fine-tune the clap detection system.The application initially attempts to use the system's default audio device. If this doesn't work or if you prefer to use a different device, you can change it. First, find your input devices using
clapDetector.printDeviceInfo(), this will print something along the lines of:Available audio devices: Device 0: Microsoft Sound Mapper - Input Device 1: Microphone (Yeti Stereo Microph Device 2: Microsoft Sound Mapper - Output Device 3: YX Display (NVIDIA High Definit Device 4: Speakers (Realtek(R) Audio) Device 5: Speakers (Yeti Stereo Microphon Device 6: ES-G34C5 (NVIDIA High Definitioonce you found your desired input device, copy either it id or name, eg:
1orMicrophone (Yeti Stereo Microph, note that it's almost always better to use the name and not the id as it is more robust.Once you have your input device, simply tell the constructor to use it like so:
clapDetector.ClapDetector(inputDevice="Microphone (Yeti Stereo Microph")
or
clapDetector.ClapDetector(inputDevice=1)
-
clone the repository, if you have not already using
git clone https://github.com/TzurSoffer/clapDetection/ -
go into the examples folder and choose one of the scripts you would like to run.
The
examples/folder contains small runnable scripts demonstrating common flows:findMicrophone.py— list audio devices and indexessingleClap.py— detects single clapsdoubleClap.py— detects double claps and saves audio when detectedliveVisualization.py— live plot of audio levels (may require matplotlib)externalAudioSource.py— shows how to feed audio into the detector without using the module's built-in tools (Note that this is an advanced example showing the tools versatility, but I do not recommend doing this in most cases)
To run an example (PowerShell):
python .\examples\doubleClap.py
-
Create a script that uses this library
import time from clapDetector import ClapDetector, printDeviceInfo print(""" -------------------------------- The application initially attempts to use the system's default audio device. If this doesn't work or if you prefer to use a different device, you can change it. Below are the available audio devices. Find the one you are using and change the 'inputDevice' variable to the name or index of your preferred audio device. Then, restart the program, and it should properly capture audio. -------------------------------- """) printDeviceInfo() thresholdBias = 6000 lowcut=200 #< increase this to make claps detection more strict highcut=3200 #< decrease this to make claps detection more strict clapDetector = ClapDetector(inputDevice=-1, logLevel=10) clapDetector.initAudio() try: while True: audioData = clapDetector.getAudio() result = clapDetector.run(thresholdBias=thresholdBias, lowcut=lowcut, highcut=highcut, audioData=audioData) resultLength = len(result) #< amount of claps detected (1 is single-clap, 2 is double-clap, etc) if resultLength == 2: print(f"Double clap detected! bias {thresholdBias}, lowcut {lowcut}, and highcut {highcut}") clapDetector.saveAudio(folder="./") time.sleep(1/60) except KeyboardInterrupt: print("Exited gracefully") except Exception as e: print(f"error: {e}") finally: clapDetector.stop()
-
The system will continuously monitor audio input and detect claps.
This project is licensed under the MIT License — see the LICENSE.txt file for details.