/* clap.c /* Simple demo to detect claps from the sound card and execute a given command */ /* 1/27/05 eric buehl; eric@thebuehls.com */ #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 */ #define BUF_SIZE 2048 /* amount of audio 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 5 /* the minumum number of peaks that make a clap */ #define MAX_GAP 1500 /* 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.90 /* the maximum freq that we consider to be a clap */ int audio_fd; unsigned char recBuffer[BUF_SIZE]; int peaks[1024]; int numPeaks=0; int claps[5][2]; /* [clap#][start,end] */ int main(int argc, char *argv[]) { int i,j; int len; float freq; int vol; int max=0,min=256; int avg; 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; } strcpy(DEVICE_NAME,argv[1]); for(i=2;i=PEAK)) peaks[numPeaks++]=i; } for(i=0;i (peaks[++i]))); finish=peaks[i]; 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; #ifdef DEBUG printf("start: %d\n",start); printf("finish: %d\n",finish); printf("diff: %d\n",finish-start); printf("vol: %f\n",vol); printf("freq: %f\n",freq); printf("COMMAND: %s\n\n",COMMAND); #else system(COMMAND); #endif } numPeaks=0; #ifdef DEBUG printf("--new buffer--\n"); #endif } close(audio_fd); return 0; }