/* record.c * * Written by Tracy Harton (harton@owlnet.rice.edu) * December, 1995 * * matlab mex function for recording audio on SGIs. * * Can specify bps, sampling rate, and duration * * Waits for signal to cross THRESHOLD before beginning sample * */ #define THRESHOLD 10000 /* minimum signal amplitude to "start" recording... */ #include /* audio library header */ #include #include #include #include "mex.h" int record(int bits, long rate, int length, int verbose, short **data); void mexFunction(int nlhs, Matrix *plhs[], int nrhs, Matrix *prhs[]) { int ma, mb, mc, md, na, nb, nc, nd; double *ap, *bp, *cp, *dp; double *ep; int length; int count; short *data; if ((nrhs != 4)||(nlhs != 1)) { mexErrMsgTxt("Usage: data = record(bits, rate, length, verbose)"); } ma = mxGetM(prhs[0]); mb = mxGetM(prhs[1]); mc = mxGetM(prhs[2]); md = mxGetM(prhs[3]); na = mxGetN(prhs[0]); nb = mxGetN(prhs[1]); nc = mxGetN(prhs[2]); nd = mxGetN(prhs[3]); ap = mxGetPr(prhs[0]); bp = mxGetPr(prhs[1]); cp = mxGetPr(prhs[2]); dp = mxGetPr(prhs[3]); length = record((int) *ap, (int) *bp, (int) *cp, (int) *dp, &data); plhs[0] = mxCreateFull(length, 1, REAL); ep = mxGetPr(plhs[0]); for(count = 0; count < length; count++) { ep[count] = (double) data[count]; } } int record(int bits, long rate, int length, int verbose, short **data) { short *buf_pos; short tmp_buff; int start; int time = 0; ALconfig config; /* port configuration structure */ ALport inport; /* audio port structure */ long pvbuf[2]; *data = (short *) malloc(length*rate*sizeof(short)); if(data == NULL) { printf("Unable to allocate temporary buffer.\n"); return(0); } buf_pos = *data; config = ALnewconfig(); /* create a config object */ pvbuf[0] = AL_INPUT_RATE; pvbuf[1] = (long) rate; ALsetparams(AL_DEFAULT_DEVICE, pvbuf, 2); ALgetparams(AL_DEFAULT_DEVICE, pvbuf, 2); rate = pvbuf[1]; ALsetqueuesize(config, rate*2); /* 2 sec of mono */ switch(bits) { case 8: ALsetwidth(config, AL_SAMPLE_8); break; case 16: default: ALsetwidth(config, AL_SAMPLE_16); /* 16-bit signed samples */ break; } ALsetchannels(config, AL_MONO); /* mono port */ inport = ALopenport("inport", "r", config); if(verbose) printf("Waiting for sound..."); start = 0; while(start < 25) { ALreadsamps(inport, &tmp_buff, 1); if((tmp_buff > THRESHOLD) || (tmp_buff < -THRESHOLD)) start++; } if(verbose) printf("\nStarting to record %d seconds at %d/%dbits\n", length, rate, bits); while (time < length) { if(verbose) printf("."); ALreadsamps(inport, buf_pos, rate); /* read 1 sec of sound */ buf_pos += rate; time++; } if(verbose) printf("\nDone sampling.\n"); ALfreeconfig(config); /* free the config object */ ALcloseport(inport); /* shut down the input port */ return(time*rate); }