/* * SPDsoft 1996 * Sun Sep 29 01:58:29 DST 1996 * Wed Nov 30 11:19:32 CET 2005 * * IRIX 6.2 audio level monitor * Popup a dialog when audio input level reachs a given level */ #include #include #include #include #include #include #include #include #include #include #define BUFFER 510 #define IDLE 250 #define THR 60.0 #define MIN(a,b) ((a)>(b)?(b):(a)) typedef struct { char *app; char hostname[64]; ALport audioport; Widget dialog; XtAppContext app_context; unsigned long idle_t; long buff_s; float thr; float *samples; int x11; char *cmd; } tstatus; Boolean quit=FALSE; Boolean popup=FALSE; ALport audioinit(tstatus status); void Ok(Widget w, XtPointer client_data, XtPointer call_data); void Quit(Widget w, XtPointer client_data, XtPointer call_data); void Help(Widget w, XtPointer client_data, XtPointer call_data); void idle( XtPointer pstatus, XtIntervalId *id); int main(int argc, char **argv); void usage(tstatus st); void usage(tstatus st) { fprintf(stderr,"Use: %s [-htbfXc] [-text string]\n", st.app); fprintf(stderr,"%s: -X : disable X11. Must be first option\n", st.app); fprintf(stderr,"%s: -t : idle timer (ms) (%ld)\n", st.app, st.idle_t); fprintf(stderr,"%s: -b : buffer (%ld)\n", st.app, st.buff_s); fprintf(stderr,"%s: -f : level (%f)\n", st.app, st.thr); fprintf(stderr,"%s: -c cmd: command\n", st.app); exit(0); } ALport audioinit(tstatus status) /* Configure an audio port */ { ALport audioport; ALconfig audioconfig; int err; ALseterrorhandler(0); audioconfig = ALnewconfig(); ALsetsampfmt(audioconfig, AL_SAMPFMT_FLOAT); ALsetfloatmax(audioconfig, 100.0); ALsetqueuesize(audioconfig, status.buff_s); ALsetchannels(audioconfig,AL_MONO); audioport = ALopenport("micro","r",audioconfig); if (audioport == (ALport) 0) { err = oserror(); if (err == AL_BAD_NO_PORTS) { fprintf(stderr, " System is out of audio ports\n"); } else if (err == AL_BAD_DEVICE_ACCESS) { fprintf(stderr, " Couldn't access audio device\n"); } else if (err == AL_BAD_OUT_OF_MEM) { fprintf(stderr, " Out of memory\n"); } exit(1); } ALfreeconfig ( audioconfig ); return( audioport ); } void Ok(Widget w, XtPointer client_data, XtPointer call_data) { XtUnmanageChild(w); popup=FALSE; } void Quit(Widget w, XtPointer client_data, XtPointer call_data) { XtUnmanageChild(w); quit=TRUE; } void Help(Widget w, XtPointer client_data, XtPointer call_data) { fprintf(stderr,"Sorry, no help by now\n"); } void idle( XtPointer call_data, XtIntervalId *id) { float *sample; long nsamples, ngf; tstatus *pstatus; pstatus=(tstatus *)call_data; XtAppAddTimeOut( pstatus->app_context, pstatus->idle_t, idle, call_data); if(!popup) { ngf = ALgetfilled(pstatus->audioport); nsamples=MIN( pstatus->buff_s, ngf ); if ( nsamples > 0 ) { ALreadsamps(pstatus->audioport, pstatus->samples, nsamples); for( sample=pstatus->samples; sample < pstatus->samples + nsamples; sample++ ) if ( *sample > pstatus->thr ) { popup = TRUE; XtManageChild(pstatus->dialog); break; } } } } int main(int argc, char **argv) { Widget app_shell; Widget dialog; XtAppContext app_context; XEvent event_return; tstatus status; time_t t; struct tm *now; int opt; extern char *optarg; extern int optind,opterr; static XrmOptionDescRec options[] = { {"-text", "*messageString", XrmoptionSepArg, NULL}, }; status.idle_t = IDLE; status.buff_s = BUFFER; status.thr = THR; status.x11 = 1; status.cmd = NULL; if ( argv[1]!=NULL && (argv[1][0] == '-' && argv[1][1] == 'X') ) status.x11 = 0; if (status.x11) app_shell = XtAppInitialize(&app_context, "Dphone", (XrmOptionDescList) options, XtNumber(options), &argc, argv, (String *) NULL, (ArgList) NULL, 0); if ((status.app = strrchr(argv[0], '/')) != NULL) status.app = status.app+1; else status.app = argv[0]; if (gethostname(status.hostname,sizeof(status.hostname)) < 0) { perror("gethostname: who am I ?"); exit(1); } while ( (opt=getopt(argc,argv,"hXc:t:b:f:")) != EOF ) { switch(opt) { case 'X': status.x11=0; break; case 't': status.idle_t = (unsigned long) atol(optarg); if (status.idle_t<0) usage(status); break; case 'c': status.cmd = optarg; break; case 'b': status.buff_s = atol(optarg); if (status.buff_s<510) usage(status); break; case 'f': status.thr = atof( optarg ); if ( (status.thr<0.0) || (status.thr>100.0)) usage(status); break; case 'h': usage(status); break; default: break; } } if ( NULL == (status.samples = malloc(sizeof(float)*status.buff_s))) { fprintf(stderr, "%s: out of memory\n", status.app); exit(2); } status.audioport=audioinit(status); if (status.x11) { dialog = XmCreateMessageDialog( app_shell, argv[0], NULL, 0); /* XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));*/ XtAddCallback(dialog, XmNcancelCallback, Quit, NULL); XtAddCallback(dialog, XmNokCallback, Ok, NULL); XtAddCallback(dialog, XmNhelpCallback, Help, NULL); status.app_context=app_context; status.dialog=dialog; XtAppAddTimeOut(app_context, status.idle_t, idle, (XtPointer)&status); while(!quit) { XtAppNextEvent(app_context, &event_return); XtDispatchEvent(&event_return); } } else { float *sample; long nsamples, ngf; tstatus *pstatus=&status; while(!quit) { int zero=1; ngf = ALgetfilled(pstatus->audioport); nsamples=MIN( pstatus->buff_s, ngf ); if ( nsamples > 0 ) { ALreadsamps(pstatus->audioport, pstatus->samples, nsamples); for( sample=pstatus->samples; sample < pstatus->samples + nsamples; sample++ ) if ( *sample > pstatus->thr ) { t = time(NULL); now = localtime(&t); if (zero) { printf("%s|%0.2d/%0.2d/%0.2d %0.2d:%0.2d:%0.2d|%f\n", status.hostname, now->tm_mon + 1, now->tm_mday, now->tm_year + 1900, now->tm_hour, now->tm_min, now->tm_sec, pstatus->thr); zero=0; } printf("%s|%0.2d/%0.2d/%0.2d %0.2d:%0.2d:%0.2d|%f\n", status.hostname, now->tm_mon + 1, now->tm_mday, now->tm_year + 1900, now->tm_hour, now->tm_min, now->tm_sec, *sample); fflush(stdout); if (pstatus->cmd != NULL) { if (0!=system(pstatus->cmd)) perror(pstatus->cmd); } break; } } else { if (!zero) { t = time(NULL); now = localtime(&t); printf("%s|%0.2d/%0.2d/%0.2d %0.2d:%0.2d:%0.2d|%f\n", status.hostname, now->tm_mon + 1, now->tm_mday, now->tm_year + 1900, now->tm_hour, now->tm_min, now->tm_sec, pstatus->thr); zero=1; } } usleep( status.idle_t * 1000); } } puts("Hola"); ALcloseport(status.audioport); free(status.samples); exit(0); }