How to detect a sound in just a few lines of code

The basic detection method is just one short line of code

(while (= (snd-fetch s) 0))

What this does is to fetch consecutive samples from the sound “s” until a sample is returned with a non-zero value.

A word of caution, SND-FETCH is destructive to the audio data. If “s” will be required later in the code, use SND-COPY to copy the audio data or it will be corrupted.

Clearly we can set a non-zero detection threshold if required with something like:

(while (< (abs (snd-fetch s)) 0.1))

To make it a little more useful, let’s create a label at the detected audio position:

(let ((num (do ((j 0 (1+ j)))
((/= 0 (snd-fetch s)) j))))
(list (list (/ (- num 0.5) *sound-srate*) "Start")))


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.