/* clap.c a Simple demo to detect claps from the sound card turned in to a PC clapper switch that executes a given command 1/27/05 eric buehl; eric@thebuehls.com 10/8/08 FLD@r00t3d.com */ #include #include #include #include #include #include #include /*#define DEBUG /* uncomment to get some info about claps received */ /* in order to configure, uncomment the debug line. commands will not be executed in debug mode */ /* these values will very greatly depending on your setup, microphone used, etc */ /* Values for detecting claps in the buffer */ #define BUF_SIZE 2048 /* amount of audio samples to record at a time */ #define PEAK 160 /* what values are considered a peak */ #define BOTTOM 96 /* what values are considered a min */ #define FREQ_GAP 30 /* the maximum space between a peak */ #define MIN_GAP 150 /* the minumum number of peaks that make a clap */ #define MAX_GAP 800 /* the max length of a clap */ #define MIN_FREQ 0.35 /* the minimum freq that we consider to be a clap */ #define MAX_FREQ 0.50 /* the maximum freq that we consider to be a clap */ /* How many buffers between the two claps ( 1 buffer is about 47ms ) */ #define MIN_CLAP_GAP 5 /* the minumum amount of silent samples between claps */ #define MAX_CLAP_GAP 10 /* the maximum amount of silent samples between claps */ /* Sound Settings */ int format = AFMT_U8; /* Standard Unsigned 8 bit */ int sample_rate = 44100; /* 44.1kHz */ int chn = 1; /* Mono */ int audio_fd; /* The audio device */ unsigned char recBuffer[BUF_SIZE]; /* Samples */ int peaks[BUF_SIZE]; /* Samples that are above Peak value */ int numPeaks=0; /* How many Peaked */ int capSilent = 0; /* Do we need to count silent buffers */ int silence = 0; /* How many silent buffers */ int bufnum = 0; /* Used in Debug mode to count Buffers */ int main(int argc, char *argv[]) { int i,j; int len; float freq; int vol; char DEVICE_NAME[50]; char COMMAND[200]={0}; int start,finish; if(argc<3){ printf("Usage: %s device_name command\nEXAMPLE: %s /dev/dsp echo clap received\\n\n\n",argv[0],argv[0]); return -1; } /* Parse Commandline Arguments */ strcpy(DEVICE_NAME,argv[1]); for(i=2;i=PEAK)) { peaks[numPeaks++]=i; /*printf("recBuffer: %d\n",recBuffer[i]); */ } } /* Stop counting silent Buffers if there are too many */ if (silence > MAX_CLAP_GAP) { silence = 0; capSilent = 0; } /* Go through every Sample that is over the Peak Limit */ for(i=0;i (peaks[++i]))); finish=peaks[i]; /* Last Peak Sample */ /* Check that there are enough of Peaks */ if(finish-start < MIN_GAP || finish-start > MAX_GAP) continue; /* compute avergae frequency of clap */ /* if we find the number of samples over a certain value per constant time, we get an abstract frequency */ j=start; freq=0; vol=0; while(j<=finish) { if(recBuffer[j]>128) vol+=recBuffer[j]; if(recBuffer[j++] > 128 /* PEAK */) freq++; } freq /= (finish-start); vol /= j; if(freqMAX_FREQ) { continue; } /* Not enough Silence after the first clap so we zero the Silence counter */ if (silence < MIN_CLAP_GAP) silence = 0; capSilent = 1; #ifdef DEBUG printf("start: %d\n",start); printf("finish: %d\n",finish); printf("diff: %d\n",finish-start); printf("vol: %d\n",vol); printf("freq: %f\n",freq); printf("silence: %d\n",silence); if (silence >= MIN_CLAP_GAP && silence <= MAX_CLAP_GAP) printf("run: Yes\n\n"); else printf("run: No\n\n"); #else if (silence >= MIN_CLAP_GAP && silence <= MAX_CLAP_GAP) { system(COMMAND); capSilent = 0; silence = 0; } #endif } /* Start counting Silent Buffers after the first Clap is detected */ if (capSilent == 1) silence++; numPeaks = 0; #ifdef DEBUG if (bufnum > 127) bufnum = 0; bufnum++; printf("--new buffer #%d--\n",bufnum); #endif } /* End of the Main Loop */ close(audio_fd); /* <-- This will happen when (1 != 1)? */ return 0; }