static double F(){ printf("Inside F\n"); if( strcmp(global_current->val,"(") == 0 ){ next_token(); double hold = E(); if ( strcmp(global_current->val,")") == 0 ){ next_token(); return hold; } else{ printf("Error\n"); } } if ( isNumeric(global_current->val) != 0){ double hold = convDouble(global_current->val); next_token(); return hold; } }
// Entry point from Matlab // Parse arguments, convert buffer to my SAMPLE (float), then call pa_wavplay with parsed args void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { bool recording = true; bool playing = true; // Check for proper number of arguments /* char *usage = "Usage: recordbuffer = pa_wavplay(playbuffer, playdevice, samplerate, recfirstchannel, reclastchannel, recnsamples, recdevice)\n pa_wavplayrecord with no arguments will list all the available devices\n"; if (nrhs != 0 && nrhs != 6) mexErrMsgTxt(usage); */ if (nrhs == 0) { // mexPrintf(usage); pa_printdevinfo(); return; } ////// get deviceids. If deviceid < 0 don't play. if recdeviceid < 0 don't record /////// // get playback device int playdeviceid = 0; double* devptr = mxGetPr(prhs[1]); if (devptr != NULL) playdeviceid = (int)*devptr; if (playdeviceid < 0) playing = false; // get record device int recdeviceid = 0; devptr = mxGetPr(prhs[6]); if (devptr != NULL) recdeviceid = (int)*devptr; if (recdeviceid < 0) recording = false; if (!recording && !playing) { mexPrintf("playdevice < 0 and recdevice < 0. Nothing to be done"); // mexErrMsgTxt(usage); } ////// Next step: convert input buffer to SAMPLE buffer ///////// if (mxIsComplex(prhs[0])) mexErrMsgTxt("audiobuffer must be noncomplex."); int bufrows = mxGetM(prhs[0]); // get number of rows in the buffer int bufcols = mxGetN(prhs[0]); // get number of columns SAMPLE *myBuf = NULL; if (playing) { // Float32 - sweet, same as SAMPLE, no conversion necessary if (mxIsSingle(prhs[0])) { SAMPLE *bufptr = (SAMPLE *)mxGetData(prhs[0]); if (bufptr == NULL) mexErrMsgTxt("audiobuffer is NULL"); myBuf = bufptr; } // double else if (mxIsDouble(prhs[0])) { double *bufptr = mxGetPr(prhs[0]); if (bufptr == NULL) mexErrMsgTxt("audiobuffer is NULL"); myBuf = convDouble(bufptr, bufrows*bufcols); } else { mexErrMsgTxt("audiobuffer is an invalid data type"); } } // get samplerate double* sampleptr = mxGetPr(prhs[2]); if (sampleptr == NULL) mexErrMsgTxt("samplerate is NULL"); double samplerate = sampleptr[0]; int recordsamples = 0, firstrecordchannel = 0, lastrecordchannel = 0, Inputbufrows = 0, Inputbufcols = 0; SAMPLE *myInputBuf = NULL; if (recording) { // get number of record channels double* sptr = mxGetPr(prhs[3]); if (sptr == NULL) mexErrMsgTxt("firstrecordchannel is NULL"); firstrecordchannel = (int)sptr[0]; if (firstrecordchannel <=0) mexErrMsgTxt("invalid firstrecordchannel: <= 0"); sptr = mxGetPr(prhs[4]); if (sptr == NULL) mexErrMsgTxt("lastrecchannel is NULL"); lastrecordchannel = (int)sptr[0]; if (lastrecordchannel <=0) mexErrMsgTxt("invalid lastrecordchannel <= 0"); if (lastrecordchannel < firstrecordchannel) mexErrMsgTxt("invalid lastrecordchannel, < firstrecordchannel"); Inputbufcols = lastrecordchannel - firstrecordchannel + 1; // get number of record samples sptr = mxGetPr(prhs[5]); if (sptr == NULL) mexErrMsgTxt("recsamples is NULL"); recordsamples = (int)sptr[0]; if (recordsamples <= 0) Inputbufrows = bufrows; // get number of rows in the buffer else Inputbufrows = recordsamples; ///// allocate memory for the output matrix plhs[0] = mxCreateNumericMatrix(Inputbufrows,Inputbufcols, mxSINGLE_CLASS, mxREAL); SAMPLE* inbuff = (SAMPLE*)mxGetData(plhs[0]); myInputBuf = inbuff; } if (!playing) { playdeviceid = paNoDevice; bufrows = bufcols = 0; } if (!recording) { recdeviceid = paNoDevice; Inputbufcols = Inputbufrows = 0; } pa_wavplayrec(myBuf, bufrows*bufcols, bufcols, myInputBuf, firstrecordchannel, lastrecordchannel, Inputbufrows*Inputbufcols, playdeviceid, recdeviceid, (int)samplerate); if (!mxIsSingle(prhs[0])) delete myBuf; // delete myBuf if necessary return; }