Documentation

Listener Module

class piclap.Listener(config=None, calibrate=True)[source]

Describes methods which are called by user for the initialisation of the PyAudio module to stream microphone input.

Parameters
  • config (class: Settings) – An object of Settings which is used for configuring the module, defaults to None

  • calibrate (bool) – If the flag is set, the chunk size is calibrated, defaults to True

Variables
  • config (class: Settings) – Store the Settings object

  • input (class: pyaudio.PyAudio) – Store the PyAudio object

  • stream (class: pyaudio.Stream) – Open a new input audio stream and store the stream object

  • claps (int) – Store current value of claps counted and initially set to 0

  • lock (thread.lock) – Store the thread lock

  • processor (class: SignalProcessor) – Store the Processor object

clapWait(clap)[source]

Start waiting for a small duration of time recursively until no more new claps are detected

Parameters

clap (int) – Number of claps found at the time of wait initialised

claps

default: 0

This class property contains the current count of claps detected

config

If the config parameter is None, an object of Settings is assigned

confirm()[source]
device

Stores an Device object which initialise PyAudio with calibration and manages the audio interface

listenClaps(threadName)[source]

This method runs on a child thread with lock when claps equals 1 and reset the class property claps to 0 when execution is finished

Parameters

threadName (str) – Name of the child thread started

processor

Initialised with an SignalProcessor object using the signal processing method found inside config

start()[source]

When this method is called, the listener start reading binary data from stream and sreach for claps inside the chunks of data using SignalProcessor until Settings.exit flag is True

Raises

KeyboardInterrupt: If Control + C is pressed on keyboard

stop()[source]

When this method is called, the listener stop listening by closing the stream safely and terminating the connection

Settings Module

class piclap.Settings[source]

This class describes all the configurations needed for the Listener to work.

Variables
  • exit (boolean) – Exit flag the determine the exit state of Listener

  • rate (int) – Bitrate at which input audio is streamed

  • channels (int) – Number of audio channels used by Listener

  • chunk_size (int) – Frame count inside the audio buffer

  • wait (float) – Clap wait in seconds

  • method (class: Munch) – The algorithm used for the detection of claps

  • actions (list(str)) – Collection of defined actions

actions

When the class initialised, it collects all the actions defined inside this class as well as any classes where are derived with this class as base class

Condition: The method name defined should start with ‘on’ and end with ‘Claps’ with the clap count inbetween them.

channels

default: 1 (Mono Channel)

Number of audio channel to listen

chunk_size

default: 1024

Number of frames in the input audio buffer

exit

default: False

Exit flag

method

default: {'name': 'threshold','value': 7000}

Detection method used for identifing claps

on2Claps()[source]

Action performed when 2 claps are detected.

on3Claps()[source]

Action performed when 3 claps are detected.

on4Claps()[source]

Action performed when 4 claps are detected. As default, exit flag is set to True if 4 claps are detected

rate

default: 44100

Number of audio samples collected in 1 second

updateMethod(method)[source]

Update the method for detecting clap

Parameters

method (dict) – A dict type parameter which defines a clap detection method

wait

default: 0.5

Time duration to wait for claps to complete in Listener.clapWait()

SignalProcessor Module

class piclap.SignalProcessor(method)[source]

Describes all the signal processing algorithms and selector method.

Parameters

method (class: Munch) – An object that contain the configuration and details of which algorithm to be used for processing the data received.

Variables
  • alg_id (int) – Stores the ID of the algorithm used

  • algorithm (class: Munch) – Stores the configuration of the algorithm used

alg_id

default: 0

Search for the algorithm name in the DETECTION_ALGORITHMS and store the id found

algorithm

The algorithm stored in this variable is used throughout the execution

findClap(data)[source]

Based on the detection algorithm selected, the data is given to the selected algorithm

Parameters

data (bytearray) – Binary data received from microphone

Returns

True if clap is detected, False otherwise

Return type

bool

useFFT(byte_stream)[source]

This algorithm is not implemented yet

Parameters

byte_stream (bytearray) – Binary stream of data received from microphone

useFiter(byte_stream)[source]

This algorithm is not implemented yet

Parameters

byte_stream (bytearray) – Binary stream of data received from microphone

useThreshold(byte_stream)[source]

This algorithm uses a threshold value to detect claps.

Parameters

byte_stream (bytearray) – Binary stream of data received from microphone

Returns

When the maximum value of the integer array is greater than threshold, it returns True and False if otherwise

Return type

bool

useWavelets(byte_stream)[source]

This algorithm is not implemented yet

Parameters

byte_stream (bytearray) – Binary stream of data received from microphone

Device Module

class piclap.Device(config, calibrate)[source]

Describes methods which are called by user for the initialisation of the PyAudio module to stream microphone input.

Parameters
  • config (class: Settings) – An object of Settings which is used for configuring the module, defaults to None

  • calibrate (bool) – If the flag is set, the chunk size is calibrated, defaults to True

Variables
  • config (class: Settings) – Store the Settings object

  • input (class: PyAudio) – Store the PyAudio object

calibrateBufferSize(enabled=True)[source]

Calibrate the chunk size to a size which the user’s system can process without any errors

Parameters

enabled (bool) – If True, calibration is done. Otherwise, it is not calibrated

closeStream()[source]

Close the audio stream

openStream()[source]

Open as binary stream to receive audio signals from microphone

readData()[source]

Reads a single chunk of binary data from stream

setThreshold()[source]

Set the threashold value to the most closest value where a clap is detected. (Not accurate)