int init(int argc, const char **argv, struct parameters *p) { FILE *file; if(argc < ARG_NARGS) { printf("insufficient arguments.\n" "usage: mcp infile outfile\n"); return 1; } printf("--------------------------\n"); printf("MCP\n"); p->inputFileName = argv[ARG_INFILE]; // This is probably fairly unsafe p->outputFileName = argv[ARG_OUTFILE]; p->outputAcidName = argv[ARG_OUTACID]; file = fopen(p->inputFileName, "rb"); if(file == NULL) { printf("Input File Error\n"); return 1; } // Seek to end of file and get the size of it fseek(file, 0, SEEK_END); p->fileSize = ftell(file); rewind(file); // Allocate file buffer memory of size EDWORD * file size p->fileBuffer = (EDWORD *) malloc(sizeof(EDWORD) * p->fileSize); fread(p->fileBuffer, p->fileSize, 1, file); printf("Input File: %s\n", p->inputFileName); printf("Output File: %s\n", p->outputFileName); printf("Output Acid Name: %s\n", p->outputAcidName); // Initialize portsf if (psf_init()) { printf("Unable to start portsf.\n"); return 1; } fclose(file); return 0; }
int main(int argc, char* argv[]) { printf("*********************************\n"); printf("Assignment 3: Additive Synthesis.\n"); printf("*********************************\n\n"); PSF_PROPS props; int ofd = -1; int error = 0; PSF_CHPEAK* peaks = NULL; float* frame = NULL; psf_format outformat = PSF_FMT_UNKNOWN; if(argc < 3){ printf("Error: insufficient arguments.\n" "usage:\n\t" "sf2float <inputfile> <outputfile>\n"); return 1; } /* be good, and startup portsf */ if(psf_init()){ printf("unable to start portsf\n"); return 1; } /* Setup props for writing to wav file */ props.srate = 44100; props.chans = 2; props.samptype = PSF_SAMP_IEEE_FLOAT; props.format = PSF_STDWAVE; props.chformat = STDWAVE; ofd = psf_sndCreate(argv[2],&props,0,0,PSF_CREATE_RDWR); if(ofd < 0){ printf("Error: unable to create outfile %s\n",argv[3]); error++; goto exit; } /* allocate space for one sample frame */ frame = (float*) malloc(props.chans * sizeof(float)); if(frame==NULL){ puts("No memory!\n"); error++; goto exit; } /* and allocate space for PEAK info */ peaks = (PSF_CHPEAK*) malloc(props.chans * sizeof(PSF_CHPEAK)); if(peaks == NULL){ puts("No memory!\n"); error++; goto exit; } /* * Where my synthesis code starts. */ /* Open file to read */ FILE *file; file = fopen(argv[1], "r"); float SAMPLE_RATE = 44100; float sineFrequencies[500] = {0}; float sineAmplitudes[500] = {0}; float prevSineAmplitudes[500] = {0}; float intervalLimit = 0; float value; float time; float sineFrequency; float currentSample = 0; int arraySize = 0; int arrayIndex = 0; /* for linear interpolation */ float prevIntervalLimit = 0; float slope = 0; float intercept = 0; float factor = 1; /* Read file and store frequencies in array */ printf("Info: started processing file.\n"); while (!feof(file)) { fscanf(file, "%f", &sineFrequency); if(sineFrequency!=-1) { sineFrequencies[arraySize] = sineFrequency; arraySize++; } else { break; } } float sum = 0; arrayIndex = 0; int samplesWritten = 0; while(!feof(file)){ /* in all cases save previous value */ fscanf(file, "%f", &value); if(value == -999) { /* save lower interval limit */ prevIntervalLimit = intervalLimit; /* save upper interval limit */ fscanf(file, "%f", &intervalLimit); int ss = nearbyint(SAMPLE_RATE * intervalLimit); float sampleLimit = (float)ss; float current = 1; for(currentSample = currentSample; currentSample < sampleLimit; currentSample++) { // printf("%f\n", currentSample); float sum = 0; int frequencyIndex; float two = 2; int y = nearbyint((intervalLimit-prevIntervalLimit)*SAMPLE_RATE); for(frequencyIndex = 0; frequencyIndex < arraySize; frequencyIndex ++) { /* compute slope */ slope = (sineAmplitudes[frequencyIndex]-prevSineAmplitudes[frequencyIndex])/(intervalLimit-prevIntervalLimit); intercept = prevSineAmplitudes[frequencyIndex]; factor = ((int)currentSample%y)*slope/SAMPLE_RATE + intercept; sum += factor * sin(two * M_PI * sineFrequencies[frequencyIndex]*current/SAMPLE_RATE); } //printf("current sample: %f, sample limit: %f, sum: %d %f\n", currentSample, sampleLimit, yyy, sum); frame[0] = sum; frame[1] = sum; if(psf_sndWriteFloatFrames(ofd, frame, 1) !=1 ) { printf("Error writing to outfile\n"); error ++; break; } current++; samplesWritten ++; } // printf("%d samples were written in interval %f\n", samplesWritten, sampleLimit); arrayIndex = 0; sum = 0; } else { // printf("arrayIndex: %d\n", arrayIndex); prevSineAmplitudes[arrayIndex] = sineAmplitudes[arrayIndex]; sineAmplitudes[arrayIndex] = value; arrayIndex ++; } /* Compute the time of the current sample */ } printf("Info: finished processing file.\n"); printf("Info: %d samples written to file\n", samplesWritten); /* * Where my synthesis code ends. */ /* do all cleanup */ exit: if(ofd >= 0) psf_sndClose(ofd); if(frame) free(frame); if(peaks) free(peaks); psf_finish(); return error; }
int main(int argc, char* argv[]) { PSF_PROPS props; long framesread; long totalread; /* init all dynamic resources to default states */ int ifd = -1,ofd = -1; int i,error = 0; double gain = 1.0; PSF_CHPEAK* peaks = NULL; psf_format outformat = PSF_FMT_UNKNOWN; FILE* fin = NULL; float* frame; printf("TEXT2SF: convert text audio data to soundfile\n"); if(argc < ARG_NARGS) { printf("insufficient arguments.\n" "usage:\n\t" "text2sf infile outfile srate chans gain\n"); return 1; } /* be good, and startup portsf */ if(psf_init()) { printf("unable to start portsf\n"); return 1; } fin = fopen(argv[ARG_INFILE],"r"); /* we now have a resource, so we use goto hereafter on hitting any error */ /* tell user if source file is already floats */ if(fin==NULL) { printf("cannot open infile %s\n",argv[ARG_INFILE]); return 1; } props.chans = atoi(argv[ARG_CHANS]); if(props.chans <=0) { printf("chans must be positive!\n"); fclose(fin); return 1; } props.srate = atoi(argv[ARG_SR]); if(props.srate <=0) { printf("srate must be positive!\n"); fclose(fin); return 1; } gain = atof(argv[ARG_GAIN]); if(gain <= 0.0) { printf("gain value must be positive!\n"); fclose(fin); return 1; } frame = malloc(props.chans * sizeof(float)); if(frame==NULL) { puts("No memoery!\n"); fclose(fin); return 1; } /* fixed output to 16bit file; one alternative would be PSF_SAM_IEEE_FLOAT for 32bit float format */ props.samptype = PSF_SAMP_16; props.format = PSF_STDWAVE; props.chformat = STDWAVE; /* check file extension of outfile name, so we use correct output file format*/ outformat = psf_getFormatExt(argv[ARG_OUTFILE]); if(outformat == PSF_FMT_UNKNOWN) { printf("outfile name %s has unknown format.\n" "Use any of .wav, .aiff, .aif, .afc, .aifc\n",argv[ARG_OUTFILE]); error++; goto exit; } props.format = outformat; ofd = psf_sndCreate(argv[ARG_OUTFILE],&props,0,0,PSF_CREATE_RDWR); if(ofd < 0) { printf("Error: unable to create outfile %s\n",argv[ARG_OUTFILE]); error++; goto exit; } /* and allocate space for PEAK info */ peaks = (PSF_CHPEAK*) malloc(props.chans * sizeof(PSF_CHPEAK)); if(peaks == NULL) { puts("No memory!\n"); error++; goto exit; } printf("copying....\n"); /* single-frame loop to do copy: report any read/write errors */ framesread = readframe(fin,frame,props.chans); totalread = 0; /* count sample frames as they are copied */ /* note: readframe retval of -1 signifies EOF */ while (framesread == 1) { totalread++; /* <--- do any processing here! ---------->*/ for(i=0; i < props.chans; i++) frame[i] *= gain; if(psf_sndWriteFloatFrames(ofd,frame,1) != 1) { printf("Error writing to outfile\n"); error++; break; } framesread = readframe(fin,frame,props.chans); } if(framesread == 0) { printf("Error reading infile at line %ld. Outfile may be incomplete.\n",totalread); error++; } else printf("Done. %ld sample frames copied to %s\n",totalread,argv[ARG_OUTFILE]); /* report PEAK values to user */ if(psf_sndReadPeaks(ofd,peaks,NULL) > 0) { long i; double peaktime; printf("PEAK information:\n"); for(i=0; i < props.chans; i++) { peaktime = (double) peaks[i].pos / (double) props.srate; printf("CH %ld:\t%.4f at %.4f secs\n", i+1, peaks[i].val, peaktime); } } /* do all cleanup */ exit: if(fin != NULL) fclose(fin); if(ofd >= 0) psf_sndClose(ofd); if(frame) free(frame); if(peaks) free(peaks); psf_finish(); /* return error status to caller: may be useful in a script etc */ return error; }
int main (int argc, char**argv) { /* declare variables */ PSF_PROPS outprops; // properties of output soundfile psf_format outformat = PSF_FMT_UNKNOWN; double dur, freq, amp, peakdiff; float val; double minval, maxval, maxamp; int srate, nharms; unsigned long i, j; int i_out; int wavetype = -1; int chans; unsigned long width = DEFAULT_WIDTH; unsigned long nbufs, outframes, remainder, nframes, framesread; oscilt_tickfunc tickfunc = tabitick; // default table lookup is interpolating /* init resources */ int ofd = -1; int error = 0; FILE *fpamp = NULL; // amplitude breakpoint file FILE *fpfreq = NULL; // frequency breakpoint file float* buffer = NULL; BRKSTREAM* ampstream = NULL; // breakpoint amplitude values BRKSTREAM* freqstream = NULL; // breakpoint frequency values GTABLE* gtable = NULL; // wave table with guard point OSCILT* p_osc = NULL; // oscillator using table lookup unsigned long brkampSize = 0, // number of breakpoints brkfreqSize = 0; printf("TABGEN: a table lookup oscillator generator.\n"); /* check for command line options */ if (argc > 1) { while (argv[1][0]=='-') { switch(argv[1][1]) { case('\0'): printf("Error: you did not specify an option.\n" "options: -wN set width of lookup table " "to N points (default: 1024 points)\n" " -t use truncating lookup " "(default: interpolating lookup)\n"); return 1; case('t'): if (argv[1][2] == '\0') tickfunc = tabtick; else { printf("Error: %s is not a valid option.\n" "options: -wN set width of lookup table " "to N points (default: 1024 points)\n" " -t use truncating lookup " "(default: interpolating lookup)\n", argv[1]); return 1; } break; case('w'): if (argv[1][2] == '\0') { printf("Error: you did not specify a table lookup width.\n"); return 1; } if (argv[1][2] >= '0' && argv[1][2] <= '9') { width = atoi(&argv[1][2]); if (width < 1) { printf("Error: table width must be at least 1\n"); return 1; } break; } default: printf("Error: %s is not a valid option.\n" "options: -wN set width of lookup table " "to N points (default: 1024 points)\n" " -t use truncating lookup " "(default: interpolating lookup)\n", argv[1]); return 1; } argc--; argv++; } } /* check for sufficient command line input */ if (argc != ARG_NARGS) { printf("Error: insufficient number of arguments.\n" "Usage: tabgen [-wN] [-t] outfile dur srate nchans amp freq type nharms\n" " -wN: w option sets the width of the lookup table to N points\n" " N >= 1\n" " -t: t option selects truncating table lookup\n" " default is interpolating table lookup\n" " outfile: output soundfile\n" " set to 16-bit format\n" " use any of .wav .aiff .aif .afc .aifc formats\n" " dur: duration of soundfile (seconds)\n" " srate: sample rate\n" " nchans: number of channels\n" " amp: amplitude value or breakpoint file\n" " 0.0 < amp <= 1.0\n" " freq: frequency value or breakpoint file\n" " freq >= 0.0\n" " type: oscillator wave type\n" " sine, square, triangle, sawup, sawdown, pulse\n" " nharms: number of wave harmonics that are\n" " added together in oscillator bank\n" " nharms >= 1\n" ); return 1; } /* check duration input */ dur = atof(argv[ARG_DUR]); if (dur <= 0.0) { printf("Error: duration must be positive.\n"); return 1; } /* check sample rate input */ srate = atoi(argv[ARG_SR]); if (srate <= 0) { printf("Error: sample rate must be positive.\n"); return 1; } outprops.srate = srate; /* check number of channels input */ chans = atoi(argv[ARG_CHANS]); if (chans <= STDWAVE || chans > MC_WAVE_EX) { printf("Error: portsf does not support %d channels.\n", chans); return 1; } outprops.chans = chans; /* check the output soundfile extension */ outformat = psf_getFormatExt(argv[ARG_OUTFILE]); if (outformat == PSF_FMT_UNKNOWN) { printf("Error: outfile extension unknown.\n" " use any of .wav .aiff .aif .afc .aifc formats\n"); return 1; } outprops.format = outformat; /* check for number of harmonics */ nharms = atoi(argv[ARG_NHARMS]); if (nharms <= 0) { printf("Error: you must enter a positive number for harmonics.\n"); return 1; } /* get the wavetype */ int count = 0; while (argv[ARG_TYPE][count] != '\0') count++; switch (count) { case (WAVE_SINE): if (!strcmp(argv[ARG_TYPE],"sine")) wavetype = WAVE_SINE; break; case (WAVE_SAWUP): if (!strcmp(argv[ARG_TYPE],"sawup")) wavetype = WAVE_SAWUP; /* case (WAVE_PULSE): */ if (!strcmp(argv[ARG_TYPE],"pulse")) wavetype = WAVE_PULSE; break; case (WAVE_SQUARE): if (!strcmp(argv[ARG_TYPE],"square")) wavetype = WAVE_SQUARE; break; case (WAVE_SAWDOWN): if (!strcmp(argv[ARG_TYPE],"sawdown")) wavetype = WAVE_SAWDOWN; break; case (WAVE_TRIANGLE): if (!strcmp(argv[ARG_TYPE],"triangle")) wavetype = WAVE_TRIANGLE; break; default: wavetype = -1; } if (wavetype < 0) { printf("Error: %s is not a valid wave type.\n" "wavetype: sine, square, triangle, sawup, sawdown, pulse\n", argv[ARG_TYPE]); return 1; } /* start portsf */ psf_init(); /* create output soundfile - set to 16-bit by default */ outprops.samptype = PSF_SAMP_16; outprops.chformat = PSF_STDWAVE; ofd = psf_sndCreate(argv[ARG_OUTFILE],&outprops,0,0,PSF_CREATE_RDWR); if (ofd < 0) { printf("Error: unable to create soundfile \"%s\"\n", argv[ARG_OUTFILE]); return 1; } /* resources have been gathered at this point use goto upon hitting any errors */ /* get amplitude value or breakpoint file */ fpamp = fopen(argv[ARG_AMP],"r"); if (fpamp == NULL) { /* if the user specified a non-existant breakpoint file or didn't specify a breakpoint value */ if ( (argv[ARG_AMP][0] < '0' || argv[ARG_AMP][0] > '9') && (argv[ARG_AMP][0] != '.' && argv[ARG_AMP][0] != '-') || (argv[ARG_AMP][0] == '.' && (argv[ARG_AMP][1] < '0' || argv[ARG_AMP][1] > '9')) || (argv[ARG_AMP][0] == '-' && (argv[ARG_AMP][1] < '0' || argv[ARG_AMP][1] > '9') && argv[ARG_AMP][1] != '.') ) { printf("Error: breakpoint file \"%s\" does not exist.\n", argv[ARG_AMP]); error++; goto exit; } /* the user specified a breakpoint value */ amp = atof(argv[ARG_AMP]); if (amp <= 0.0 || amp > 1.0) { printf("Error: amplitude value out of range.\n" " 0.0 < amp <= 1.0\n"); error++; goto exit; } } else { /* gather breakpoint values */ ampstream = bps_newstream(fpamp,outprops.srate,&brkampSize); if (ampstream == NULL) { printf("Error reading breakpoint values from file \"%s\".\n", argv[ARG_AMP]); error++; goto exit; } if (bps_getminmax(ampstream,&minval,&maxval)) { printf("Error: unable to read breakpoint range " "from file \"%s\".\n", argv[ARG_AMP]); error++; goto exit; } if (minval <= 0.0 || minval > 1.0 || maxval <= 0.0 || maxval > 1.0) { printf("Error: breakpoint amplitude values out of range in file \"%s\"\n" " 0.0 < amp <= 1.0\n", argv[ARG_AMP]); error++; goto exit; } maxamp = maxval; // retain the maximum breakpoint amplitude value } /* get frequency value or breakpoint file */ fpfreq = fopen(argv[ARG_FREQ],"r"); if (fpfreq == NULL) { if ( (argv[ARG_FREQ][0] < '0' || argv[ARG_FREQ][0] > '9') && (argv[ARG_FREQ][0] != '.' && argv[ARG_FREQ][0] != '-') || (argv[ARG_FREQ][0] == '.' && (argv[ARG_FREQ][1] < '0' || argv[ARG_FREQ][1] > '9')) || (argv[ARG_FREQ][0] == '-' && (argv[ARG_FREQ][1] < '0' || argv[ARG_FREQ][1] > '9') && argv[ARG_FREQ][1] != '.') ) { printf("Error: breakpoint file \"%s\" does not exist.\n", argv[ARG_FREQ]); error++; goto exit; } freq = atof(argv[ARG_FREQ]); if (freq <= 0.0) { printf("Error: frequency must be positive.\n"); error++; goto exit; } } else { freqstream = bps_newstream(fpfreq,outprops.srate,&brkfreqSize); if (freqstream == NULL) { printf("Error reading breakpoing values from file \"%s\".\n", argv[ARG_FREQ]); error++; goto exit; } if (bps_getminmax(freqstream,&minval,&maxval)) { printf("Error: unable to read breakpoint range " "from file \"%s\".\n", argv[ARG_FREQ]); error++; goto exit; } if (minval < 0.0 || maxval < 0.0) { printf("Error: breakpoint frequency values out of range in file \"%s\"\n" " freq >= 0.0\n", argv[ARG_FREQ]); error++; goto exit; } } /* create an outfile buffer */ nframes = DEFAULT_NFRAMES; buffer = (float *) malloc (sizeof(float) * nframes * outprops.chans); if (buffer == NULL) { printf("No memory!\n"); error++; goto exit; } /* calculate the total number of outfile frames */ outframes = (unsigned long) (dur * outprops.srate + 0.5); /* calculate the number of buffers used in outfile */ nbufs = outframes / nframes; remainder = outframes - nframes * nbufs; if (remainder > 0) nbufs++; /* allocate space for new wavetype table oscillator and initialize oscillator */ switch (wavetype) { case (WAVE_SINE): gtable = new_sine(width); if (gtable == NULL) { printf("Error: unable to create a sine wave table.\n"); error++; goto exit; } p_osc = new_oscilt(outprops.srate,gtable,0); break; case (WAVE_SQUARE): gtable = new_square(width,nharms); if (gtable == NULL) { printf("Error: unable to create a square wave table.\n"); error++; goto exit; } p_osc = new_oscilt(outprops.srate,gtable,0); break; case (WAVE_TRIANGLE): gtable = new_triangle(width,nharms); if (gtable == NULL) { printf("Error: unable to create a triangle wave table.\n"); error++; goto exit; } p_osc = new_oscilt(outprops.srate,gtable,0.25); break; case (WAVE_SAWUP): if (!strcmp(argv[ARG_TYPE],"pulse")) /* case (WAVE_PULSE): */ { gtable = new_pulse(width,nharms); if (gtable == NULL) { printf("Error: unable to create a pulse wave table.\n"); error++; goto exit; } p_osc = new_oscilt(outprops.srate,gtable,0); break; } case (WAVE_SAWDOWN): if (wavetype==WAVE_SAWUP) gtable = new_saw(width,nharms,SAW_UP); else gtable = new_saw(width,nharms,SAW_DOWN); if (gtable == NULL) { printf("Error: unable to create a saw%s wave table.\n", (wavetype==SAW_UP)?"up":"down"); error++; goto exit; } p_osc = new_oscilt(outprops.srate,gtable,0); break; } printf("Creating soundfile...\n"); /* process soundfile */ framesread = 0; for (i=0; i < nbufs; i++) { /* update outfile copy status after the buffer is refreshed every 100 times */ if(i%100) { printf("%lu frames copied... %d%%\r", framesread, (int)(framesread*100/outframes)); } /* clear update status when done */ if (i==(nbufs-1)) printf(" \n"); if ((i == (nbufs-1)) && remainder) nframes = remainder; for (j=0; j < nframes; j++) { if (ampstream) amp = bps_tick(ampstream); if (freqstream) freq = bps_tick(freqstream); val = tickfunc(p_osc,freq) * amp; for (i_out=0; i_out < outprops.chans; i_out++) buffer[j*outprops.chans + i_out] = val; } if (psf_sndWriteFloatFrames(ofd,buffer,nframes) != nframes) { printf("Error: unable to write frames to outfile.\n"); error++; break; } framesread += nframes; } /* make sure peak amplitude roughly matches the user requested amplitude */ if (ampstream) peakdiff = maxamp-psf_sndPeakValue(ofd,&outprops); else peakdiff = amp-psf_sndPeakValue(ofd,&outprops); if ((peakdiff>.001) || (peakdiff<-.001)) { printf("ERROR: unable to generate the correct peak\n" " amplitude for %s\n", argv[ARG_OUTFILE]); error++; } printf("Done. %d error%s\n" "soundfile created: %s\n" "number of frames: %lu\n", error, (error==1)?"":"s", argv[ARG_OUTFILE], framesread); /* display soundfile properties if successfully copied */ if (!error) psf_sndInfileProperties(argv[ARG_OUTFILE],ofd,&outprops); /* clean resources */ exit: if (ofd >= 0) { if (psf_sndClose(ofd)) { printf("Error: unable to close soundfile: %s\n", argv[ARG_OUTFILE]); } else if (error) { printf("There was an error while processing the soundfile.\n" "Deleting soundfile: %s\n", argv[ARG_OUTFILE]); if (remove(argv[ARG_OUTFILE])) printf("Error: unable to delete %s\n", argv[ARG_OUTFILE]); else printf("%s successfully deleted.\n", argv[ARG_OUTFILE]); } } if (fpamp) { if (fclose(fpamp)) { printf("Error: unable to close breakpoint file \"%s\"\n", argv[ARG_AMP]); } } if (fpfreq) { if (fclose(fpfreq)) { printf("Error: unable to close breakpoint file \"%s\"\n", argv[ARG_FREQ]); } } if (buffer) { free(buffer); buffer = NULL; } if (ampstream) { bps_freepoints(ampstream); ampstream = NULL; } if (freqstream) { bps_freepoints(freqstream); freqstream = NULL; } if (gtable) gtable_free(>able); if (p_osc) { free(p_osc); p_osc = NULL; } psf_finish(); return error; }
/* Manage the whole stuff. */ void makeit() { checkstruct *check; picstruct *dfield, *field,*pffield[MAXFLAG], *wfield,*dwfield; catstruct *imacat; tabstruct *imatab; patternstruct *pattern; static time_t thetime1, thetime2; struct tm *tm; unsigned int modeltype; int nflag[MAXFLAG], nparam2[2], i, nok, ntab, next, ntabmax, forcextflag, nima0,nima1, nweight0,nweight1, npsf0,npsf1, npat,npat0; next = 0; nok = 1; /* Processing start date and time */ dtime = counter_seconds(); thetimet = time(NULL); tm = localtime(&thetimet); sprintf(prefs.sdate_start,"%04d-%02d-%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday); sprintf(prefs.stime_start,"%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); NFPRINTF(OUTPUT, ""); QPRINTF(OUTPUT, "----- %s %s started on %s at %s with %d thread%s\n\n", BANNER, MYVERSION, prefs.sdate_start, prefs.stime_start, prefs.nthreads, prefs.nthreads>1? "s":""); /* Initialize globals variables */ initglob(); NFPRINTF(OUTPUT, "Setting catalog parameters"); readcatparams(prefs.param_name); useprefs(); /* update things accor. to prefs parameters */ /* Check if a specific extension should be loaded */ /* Never true for an NDF, although we could through all NDFs in a container, */ /* so we make selectext go away. */ nima0 = -1; forcextflag = 0; /* Do the same for other data (but do not force single extension mode) */ nima1 = -1; /* selectext(prefs.image_name[1]) */ nweight0 = -1; /* selectext(prefs.wimage_name[0]) */ nweight1 = -1; /* selectext(prefs.wimage_name[1]) */ if (prefs.dpsf_flag) { npsf0 = -1; /* selectext(prefs.psf_name[0]) */ npsf1 = -1; /* selectext(prefs.psf_name[1]) */ } else npsf0 = -1; /* selectext(prefs.psf_name[0]) */ for (i=0; i<prefs.nfimage_name; i++) nflag[i] = -1; /* selectext(prefs.fimage_name[i]) */ if (prefs.psf_flag) { /*-- Read the first PSF extension to set up stuff such as context parameters */ NFPRINTF(OUTPUT, "Reading PSF information"); if (prefs.dpsf_flag) { thedpsf = psf_load(prefs.psf_name[0],nima0<0? 1 :(npsf0<0? 1:npsf0)); thepsf = psf_load(prefs.psf_name[1], nima1<0? 1 :(npsf1<0? 1:npsf1)); } else thepsf = psf_load(prefs.psf_name[0], nima0<0? 1 :(npsf0<0? 1:npsf0)); /*-- Need to check things up because of PSF context parameters */ updateparamflags(); useprefs(); } if (prefs.prof_flag) { #ifdef USE_MODEL fft_init(prefs.nthreads); /* Create profiles at full resolution */ NFPRINTF(OUTPUT, "Preparing profile models"); modeltype = (FLAG(obj2.prof_offset_flux)? MODEL_BACK : MODEL_NONE) |(FLAG(obj2.prof_dirac_flux)? MODEL_DIRAC : MODEL_NONE) |(FLAG(obj2.prof_spheroid_flux)? (FLAG(obj2.prof_spheroid_sersicn)? MODEL_SERSIC : MODEL_DEVAUCOULEURS) : MODEL_NONE) |(FLAG(obj2.prof_disk_flux)? MODEL_EXPONENTIAL : MODEL_NONE) |(FLAG(obj2.prof_bar_flux)? MODEL_BAR : MODEL_NONE) |(FLAG(obj2.prof_arms_flux)? MODEL_ARMS : MODEL_NONE); theprofit = profit_init(thepsf, modeltype); changecatparamarrays("VECTOR_MODEL", &theprofit->nparam, 1); changecatparamarrays("VECTOR_MODELERR", &theprofit->nparam, 1); nparam2[0] = nparam2[1] = theprofit->nparam; changecatparamarrays("MATRIX_MODELERR", nparam2, 2); if (prefs.dprof_flag) thedprofit = profit_init(thedpsf, modeltype); if (prefs.pattern_flag) { npat0 = prefs.prof_disk_patternvectorsize; if (npat0<prefs.prof_disk_patternmodvectorsize) npat0 = prefs.prof_disk_patternmodvectorsize; if (npat0<prefs.prof_disk_patternargvectorsize) npat0 = prefs.prof_disk_patternargvectorsize; /*---- Do a copy of the original number of pattern components */ prefs.prof_disk_patternncomp = npat0; pattern = pattern_init(theprofit, prefs.pattern_type, npat0); if (FLAG(obj2.prof_disk_patternvector)) { npat = pattern->size[2]; changecatparamarrays("DISK_PATTERN_VECTOR", &npat, 1); } if (FLAG(obj2.prof_disk_patternmodvector)) { npat = pattern->ncomp*pattern->nfreq; changecatparamarrays("DISK_PATTERNMOD_VECTOR", &npat, 1); } if (FLAG(obj2.prof_disk_patternargvector)) { npat = pattern->ncomp*pattern->nfreq; changecatparamarrays("DISK_PATTERNARG_VECTOR", &npat, 1); } pattern_end(pattern); } QPRINTF(OUTPUT, "Fitting model: "); for (i=0; i<theprofit->nprof; i++) { if (i) QPRINTF(OUTPUT, "+"); QPRINTF(OUTPUT, "%s", theprofit->prof[i]->name); } QPRINTF(OUTPUT, "\n"); if (FLAG(obj2.prof_concentration)|FLAG(obj2.prof_concentration)) { thepprofit = profit_init(thepsf, MODEL_DIRAC); theqprofit = profit_init(thepsf, MODEL_EXPONENTIAL); } #else error(EXIT_FAILURE, "*Error*: model-fitting is not supported in this build.\n", " Please check your configure options"); #endif } if (prefs.filter_flag) { NFPRINTF(OUTPUT, "Reading detection filter"); getfilter(prefs.filter_name); /* get the detection filter */ } if (FLAG(obj2.sprob)) { NFPRINTF(OUTPUT, "Initializing Neural Network"); neurinit(); NFPRINTF(OUTPUT, "Reading Neural Network Weights"); getnnw(); } if (prefs.somfit_flag) { int margin; thesom = som_load(prefs.som_name); if ((margin=(thesom->inputsize[1]+1)/2) > prefs.cleanmargin) prefs.cleanmargin = margin; if (prefs.somfit_vectorsize>thesom->neurdim) { prefs.somfit_vectorsize = thesom->neurdim; sprintf(gstr,"%d", prefs.somfit_vectorsize); warning("Dimensionality of the SOM-fit vector limited to ", gstr); } } /* Prepare growth-curve buffer */ if (prefs.growth_flag) initgrowth(); /* Allocate memory for multidimensional catalog parameter arrays */ alloccatparams(); useprefs(); /*-- Init the CHECK-images */ if (prefs.check_flag) { checkenum c; NFPRINTF(OUTPUT, "Initializing check-image(s)"); for (i=0; i<prefs.ncheck_type; i++) if ((c=prefs.check_type[i]) != CHECK_NONE) { if (prefs.check[c]) error(EXIT_FAILURE,"*Error*: 2 CHECK_IMAGEs cannot have the same ", " CHECK_IMAGE_TYPE"); prefs.check[c] = initcheck(prefs.check_name[i], prefs.check_type[i], next); free(prefs.check_name[i]); } } NFPRINTF(OUTPUT, "Initializing catalog"); initcat(); /* Initialize XML data */ if (prefs.xml_flag || prefs.cat_type==ASCII_VO) init_xml(next); /* Go through all images */ /* for all images in an MEF */ /*---- Initial time measurement*/ time(&thetime1); thecat.currext = nok+1; dfield = field = wfield = dwfield = NULL; /*---- Init the Detection and Measurement-images */ if (prefs.dimage_flag) { dfield = newfield(prefs.image_name[0], DETECT_FIELD, nok); field = newfield(prefs.image_name[1], MEASURE_FIELD, nok); if ((field->width!=dfield->width) || (field->height!=dfield->height)) error(EXIT_FAILURE, "*Error*: Frames have different sizes",""); /*---- Prepare interpolation */ if (prefs.dweight_flag && prefs.interp_type[0] == INTERP_ALL) init_interpolate(dfield, -1, -1); if (prefs.interp_type[1] == INTERP_ALL) init_interpolate(field, -1, -1); } else { field = newfield(prefs.image_name[0], DETECT_FIELD | MEASURE_FIELD, nok); /*-- Prepare interpolation */ if ((prefs.dweight_flag || prefs.weight_flag) && prefs.interp_type[0] == INTERP_ALL) init_interpolate(field, -1, -1); /* 0.0 or anything else */ } /*-- Init the WEIGHT-images */ if (prefs.dweight_flag || prefs.weight_flag) { weightenum wtype; PIXTYPE interpthresh; if (prefs.nweight_type>1) { /*------ Double-weight-map mode */ if (prefs.weight_type[1] != WEIGHT_NONE) { /*-------- First: the "measurement" weights */ wfield = newweight(prefs.wimage_name[1],field,prefs.weight_type[1], nok); wtype = prefs.weight_type[1]; interpthresh = prefs.weight_thresh[1]; /*-------- Convert the interpolation threshold to variance units */ weight_to_var(wfield, &interpthresh, 1); wfield->weight_thresh = interpthresh; if (prefs.interp_type[1] != INTERP_NONE) init_interpolate(wfield, prefs.interp_xtimeout[1], prefs.interp_ytimeout[1]); } /*------ The "detection" weights */ if (prefs.weight_type[0] != WEIGHT_NONE) { interpthresh = prefs.weight_thresh[0]; if (prefs.weight_type[0] == WEIGHT_FROMINTERP) { dwfield=newweight(prefs.wimage_name[0],wfield,prefs.weight_type[0], nok); weight_to_var(wfield, &interpthresh, 1); } else { dwfield = newweight(prefs.wimage_name[0], dfield?dfield:field, prefs.weight_type[0], nok); weight_to_var(dwfield, &interpthresh, 1); } dwfield->weight_thresh = interpthresh; if (prefs.interp_type[0] != INTERP_NONE) init_interpolate(dwfield, prefs.interp_xtimeout[0], prefs.interp_ytimeout[0]); } } else { /*------ Single-weight-map mode */ wfield = newweight(prefs.wimage_name[0], dfield?dfield:field, prefs.weight_type[0], nok); wtype = prefs.weight_type[0]; interpthresh = prefs.weight_thresh[0]; /*------ Convert the interpolation threshold to variance units */ weight_to_var(wfield, &interpthresh, 1); wfield->weight_thresh = interpthresh; if (prefs.interp_type[0] != INTERP_NONE) init_interpolate(wfield, prefs.interp_xtimeout[0], prefs.interp_ytimeout[0]); } } /*-- Init the FLAG-images */ for (i=0; i<prefs.nimaflag; i++) { pffield[i] = newfield(prefs.fimage_name[i], FLAG_FIELD, nok); if ((pffield[i]->width!=field->width) || (pffield[i]->height!=field->height)) error(EXIT_FAILURE, "*Error*: Incompatible FLAG-map size in ", prefs.fimage_name[i]); } /*-- Compute background maps for `standard' fields */ QPRINTF(OUTPUT, dfield? "Measurement image:" : "Detection+Measurement image: "); makeback(field, wfield, prefs.wscale_flag[1]); QPRINTF(OUTPUT, (dfield || (dwfield&&dwfield->flags^INTERP_FIELD))? "(M) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n" : "(M+D) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n", field->backmean, field->backsig, (field->flags & DETECT_FIELD)? field->dthresh: field->thresh); if (dfield) { QPRINTF(OUTPUT, "Detection image: "); makeback(dfield, dwfield? dwfield : (prefs.weight_type[0] == WEIGHT_NONE?NULL:wfield), prefs.wscale_flag[0]); QPRINTF(OUTPUT, "(D) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n", dfield->backmean, dfield->backsig, dfield->dthresh); } else if (dwfield && dwfield->flags^INTERP_FIELD) { makeback(field, dwfield, prefs.wscale_flag[0]); QPRINTF(OUTPUT, "(D) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n", field->backmean, field->backsig, field->dthresh); } /*-- For interpolated weight-maps, copy the background structure */ if (dwfield && dwfield->flags&(INTERP_FIELD|BACKRMS_FIELD)) copyback(dwfield->reffield, dwfield); if (wfield && wfield->flags&(INTERP_FIELD|BACKRMS_FIELD)) copyback(wfield->reffield, wfield); /*-- Prepare learn and/or associations */ if (prefs.assoc_flag) init_assoc(field); /* initialize assoc tasks */ /*-- Update the CHECK-images */ if (prefs.check_flag) for (i=0; i<MAXCHECK; i++) if ((check=prefs.check[i])) reinitcheck(field, check); if (!forcextflag && nok>1) { if (prefs.psf_flag) { /*------ Read other PSF extensions */ NFPRINTF(OUTPUT, "Reading PSF information"); psf_end(thepsf, thepsfit); if (prefs.dpsf_flag) { psf_end(thedpsf, thedpsfit); thedpsf = psf_load(prefs.psf_name[0], nok); thepsf = psf_load(prefs.psf_name[1], nok); } else thepsf = psf_load(prefs.psf_name[0], nok); } #ifdef USE_MODEL if (prefs.prof_flag) { /*------ Create profiles at full resolution */ profit_end(theprofit); theprofit = profit_init(thepsf, modeltype); if (prefs.dprof_flag) { profit_end(thedprofit); thedprofit = profit_init(thedpsf, modeltype); } if (prefs.pattern_flag) { pattern = pattern_init(theprofit, prefs.pattern_type, npat0); pattern_end(pattern); } if (FLAG(obj2.prof_concentration)|FLAG(obj2.prof_concentration)) { profit_end(thepprofit); profit_end(theqprofit); thepprofit = profit_init(thepsf, MODEL_DIRAC); theqprofit = profit_init(thepsf, MODEL_EXPONENTIAL); } } #endif } /*-- Initialize PSF contexts and workspace */ if (prefs.psf_flag) { psf_readcontext(thepsf, field); psf_init(); if (prefs.dpsf_flag) { psf_readcontext(thepsf, dfield); psf_init(); } } /*-- Copy field structures to static ones (for catalog info) */ if (dfield) { thefield1 = *field; thefield2 = *dfield; } else thefield1 = thefield2 = *field; if (wfield) { thewfield1 = *wfield; thewfield2 = dwfield? *dwfield: *wfield; } else if (dwfield) thewfield2 = *dwfield; reinitcat(field); /*-- Start the extraction pipeline */ NFPRINTF(OUTPUT, "Scanning image"); scanimage(field, dfield, pffield, prefs.nimaflag, wfield, dwfield); NFPRINTF(OUTPUT, "Closing files"); /*-- Finish the current CHECK-image processing */ if (prefs.check_flag) for (i=0; i<MAXCHECK; i++) if ((check=prefs.check[i])) reendcheck(field, check); /*-- Final time measurements*/ if (time(&thetime2)!=-1) { if (!strftime(thecat.ext_date, 12, "%d/%m/%Y", localtime(&thetime2))) error(EXIT_FAILURE, "*Internal Error*: Date string too long ",""); if (!strftime(thecat.ext_time, 10, "%H:%M:%S", localtime(&thetime2))) error(EXIT_FAILURE, "*Internal Error*: Time/date string too long ",""); thecat.ext_elapsed = difftime(thetime2, thetime1); } reendcat(); /* Update XML data */ if (prefs.xml_flag || prefs.cat_type==ASCII_VO) update_xml(&thecat, dfield? dfield:field, field, dwfield? dwfield:wfield, wfield); /*-- Close ASSOC routines */ end_assoc(field); for (i=0; i<prefs.nimaflag; i++) endfield(pffield[i]); endfield(field); if (dfield) endfield(dfield); if (wfield) endfield(wfield); if (dwfield) endfield(dwfield); QPRINTF(OUTPUT, " Objects: detected %-8d / sextracted %-8d \n\n", thecat.ndetect, thecat.ntotal); /* End look around all images in an MEF */ if (nok<0) error(EXIT_FAILURE, "Not enough valid FITS image extensions in ", prefs.image_name[0]); NFPRINTF(OUTPUT, "Closing files"); /* End CHECK-image processing */ if (prefs.check_flag) for (i=0; i<MAXCHECK; i++) { if ((check=prefs.check[i])) endcheck(check); prefs.check[i] = NULL; } if (prefs.filter_flag) endfilter(); if (prefs.somfit_flag) som_end(thesom); if (prefs.growth_flag) endgrowth(); #ifdef USE_MODEL if (prefs.prof_flag) { profit_end(theprofit); if (prefs.dprof_flag) profit_end(thedprofit); if (FLAG(obj2.prof_concentration)|FLAG(obj2.prof_concentration)) { profit_end(thepprofit); profit_end(theqprofit); } fft_end(); } #endif if (prefs.psf_flag) psf_end(thepsf, thepsfit); if (prefs.dpsf_flag) psf_end(thedpsf, thedpsfit); if (FLAG(obj2.sprob)) neurclose(); /* Processing end date and time */ thetimet2 = time(NULL); tm = localtime(&thetimet2); sprintf(prefs.sdate_end,"%04d-%02d-%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday); sprintf(prefs.stime_end,"%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); prefs.time_diff = counter_seconds() - dtime; /* Write XML */ if (prefs.xml_flag) write_xml(prefs.xml_name); endcat((char *)NULL); if (prefs.xml_flag || prefs.cat_type==ASCII_VO) end_xml(); /* Free FITS headers (now catalogues are closed). */ if (field->fitsheadsize > 0) { free(field->fitshead); } return; }
int main(int argc, char *argv[]) { long outframesize,thissize; int i,ofd; int ifdlist[MAX_INFILES]; //int force_stype = -1,out_stype; int force_wave = 0; int infilearg,inchans; int num_infiles = 0; int halfsec; MYLONG peaktime; float *outframe = NULL,*inframe = NULL; PSF_PROPS firstinprops,inprops; PSF_CHPEAK *fpeaks = NULL; int wave_type = -1; char *create_msg = NULL; psf_format informat = PSF_STDWAVE; char* p_dot = NULL; /* to find extension of outfile */ for(i=0;i < MAX_INFILES;i++) ifdlist[i] = -1; /* CDP version number */ if(argc==2 && (stricmp(argv[1],"--version")==0)){ printf("2.0.1.\n"); return 0; } if(argc < 4) { fprintf(stderr,"interlx: insufficient arguments\n"); usage(); exit(1); } while(argv[1][0] =='-'){ switch(argv[1][1]){ case('t'): if(argv[1][2]=='\0'){ fprintf(stderr,"-t flag requires parameter\n"); usage(); exit(1); } wave_type = atoi(&(argv[1][2])); if((wave_type < 0) || wave_type >= OPT_MAXOPTS){ fprintf(stderr,"wave type out of range\n"); usage(); exit(1); } force_wave = 1; break; default: fprintf(stderr,"\nabfpan: error: illegal flag option %s",argv[1]); exit(1); } argc--; argv++; } if(argc < 4){ fprintf(stderr,"interlx error: at least two infiles required!\n"); usage(); exit(1); } if(psf_init()){ printf("Startup failure.\n"); return 1; } //open first infile and get properties ifdlist[0] = psf_sndOpen(argv[2],&firstinprops,0); if(ifdlist[0] < 0){ fprintf(stderr,"unable to open infile %s\n",argv[2]); cleanup(ifdlist); return 1; } /* we don't know how to deal with speaker positions yet, so disregard these if(firstinprops.chformat > MC_STD){ printf(stderr,"Warning,interlx: ignoring source file speaker positions\n"); } */ outframesize = psf_sndSize(ifdlist[0]); if(outframesize < 0){ fprintf(stderr,"unable to read size of infile %s\n",argv[2]); cleanup(ifdlist); return 1; } inchans = firstinprops.chans; /*we can always allow more channels if someone really needs it! */ if(!(inchans==1 || inchans==2)){ fprintf(stderr,"interlx: error: infile %s has %d channels\n" "\t(only mono and stereo files can be used)\n",argv[2],inchans); cleanup(ifdlist); return 1; } num_infiles = 1; printf("interleaving %d-channel files,sample rate = %d\n",inchans,firstinprops.srate); infilearg = 3; while(argv[infilearg] != NULL){ if(strcmp(argv[infilearg],"0")==0){ ifdlist[num_infiles] = -1; // mark silent channel } else{ if((ifdlist[num_infiles] = psf_sndOpen(argv[infilearg],&inprops,0)) < 0){ fprintf(stderr,"cannot open infile %s\n",argv[infilearg]); cleanup(ifdlist); return 1; } if(inprops.chans != firstinprops.chans){ fprintf(stderr,"interlx: error: channel mismatch from infile %s\n",argv[infilearg]); cleanup(ifdlist); return 1; } if(inprops.srate != firstinprops.srate){ fprintf(stderr,"interlx: error: sample rate mismatch from infile %s\n",argv[infilearg]); cleanup(ifdlist); return 1; } thissize = psf_sndSize(ifdlist[num_infiles]); if(thissize < 0){ fprintf(stderr,"unable to read size of infile %s\n",argv[infilearg]); cleanup(ifdlist); return 1; } outframesize = max(outframesize,thissize); } infilearg++; num_infiles++; if(num_infiles > MAX_INFILES){ fprintf(stderr,"Sorry! too many infiles. Maximum accepted is %d.\n",MAX_INFILES); cleanup(ifdlist); exit(1); } } inframe = malloc(inchans * sizeof(float)); if(inframe==NULL){ puts("interlx: error: no memory for input buffer!\n"); cleanup(ifdlist); return 1; } firstinprops.chans *= num_infiles; outframe = (float *) malloc(firstinprops.chans * sizeof(float)); if(outframe==NULL){ puts("\ninterlx: error: no memory for output buffer!\n"); cleanup(ifdlist); return 1; } fpeaks = (PSF_CHPEAK *) calloc(firstinprops.chans,sizeof(PSF_CHPEAK)); if(fpeaks==NULL){ puts("interlx: error: no memory for internal PEAK buffer\n"); cleanup(ifdlist); return 1; } if(force_wave){ int i,matched; switch(wave_type){ case(OPT_WAVEX_GENERIC): inprops.chformat = MC_STD; informat = PSF_WAVE_EX; create_msg = "creating STD WAVE_EX file"; break; case(OPT_WAVEX): switch(firstinprops.chans){ case(1): firstinprops.chformat = MC_MONO; informat = PSF_WAVE_EX; create_msg = "creating MONO WAVE_EX file"; break; case(2): firstinprops.chformat = MC_STEREO; informat = PSF_WAVE_EX; create_msg = "creating STEREO WAVE_EX file"; break; case(4): firstinprops.chformat = MC_QUAD; informat = PSF_WAVE_EX; create_msg = "creating QUAD WAVE_EX file"; break; default: fprintf(stderr,"infile nchans incompatible with requested WAVE-EX format\n"); usage(); cleanup(ifdlist); return 1; } break; case(OPT_WAVEX_LCRS): if(firstinprops.chans != 4){ fprintf(stderr,"result must have four channels\n"); usage(); cleanup(ifdlist); return 1; } firstinprops.chformat = MC_LCRS; informat = PSF_WAVE_EX; create_msg = "creating LCRS-surround WAVE_EX file"; break; case(OPT_WAVEX_SURROUND): if(firstinprops.chans != 6){ fprintf(stderr,"result must have six channels\n"); usage(); cleanup(ifdlist); exit(1); } firstinprops.chformat = MC_DOLBY_5_1; informat = PSF_WAVE_EX; create_msg = "creating 5.1 surround WAVE_EX file"; break; case(OPT_SURR_5_0): if(firstinprops.chans != 5){ fprintf(stderr,"result must have five channels.\n"); usage(); cleanup(ifdlist); return 1; } firstinprops.chformat = MC_SURR_5_0; informat = PSF_WAVE_EX; create_msg = "creating 5.0 surround WAVE_EX file"; break; case(OPT_WAVEX_BFORMAT): matched = 0; for(i=0;i < N_BFORMATS;i++) { if(firstinprops.chans == bformats[i]){ matched = 1; break; } } if(!matched){ printf("WARNING: No Bformat definition for %d-channel file.\n",inprops.chans); } firstinprops.chformat = MC_BFMT; informat = PSF_WAVE_EX; create_msg = "creating AMBISONIC B-FORMAT WAVE_EX file"; break; case OPT_WAVEX_7_1: if(firstinprops.chans != 8){ fprintf(stderr,"result must have channels\n"); usage(); cleanup(ifdlist); return 1; } firstinprops.chformat = MC_SURR_7_1; informat = PSF_WAVE_EX; create_msg = "creating 7.1 surround WAVE_EX file"; break; case OPT_WAVEX_CUBE: if(firstinprops.chans != 8){ fprintf(stderr,"result must have channels\n"); usage(); cleanup(ifdlist); return 1; } firstinprops.chformat = MC_CUBE; informat = PSF_WAVE_EX; create_msg = "creating cube surround WAVE_EX file"; break; case OPT_WAVEX_6_1: if(firstinprops.chans != 7){ fprintf(stderr,"result must have channels\n"); usage(); cleanup(ifdlist); return 1; } firstinprops.chformat = MC_SURR_6_1; informat = PSF_WAVE_EX; create_msg = "creating 6.1 surround WAVE_EX file"; break; default: inprops.chformat = STDWAVE; informat = PSF_STDWAVE; create_msg = "creating plain sound file"; break; } } /* want to avoid WAVE_EX if just plain WAVE possible */ firstinprops.format = informat; /*firstinprops.chformat = MC_STD;*/ /* RWD April 2006 do this here? */ p_dot = strrchr(argv[1],'.'); if(stricmp(++p_dot,"amb")==0) { int i; int matched = 0; firstinprops.chformat = MC_BFMT; for(i=0;i < N_BFORMATS;i++) { if(firstinprops.chans == bformats[i]){ matched = 1; break; } } if(!matched) printf("\nWARNING: channel count %d unknown for BFormat.\n",firstinprops.chans); } if(!is_legalsize(outframesize,&firstinprops)){ fprintf(stderr,"error: outfile size %ld exceeds capacity of format.\n",outframesize); return 1; } printf("\n%s: %d channels, %ld frames.\n",create_msg,firstinprops.chans,outframesize); ofd = psf_sndCreate(argv[1],&firstinprops,0,0,PSF_CREATE_RDWR); if(ofd < 0){ fprintf(stderr,"interlx: error: unable to create outfile %s.\n",argv[1]); cleanup(ifdlist); return 1; } halfsec = firstinprops.srate / 2; for(i=0;i < outframesize; i++){ // frame loop float *p_framesamp,*p_filesamp; int j; p_framesamp = outframe; memset((char *)outframe,0,firstinprops.chans * sizeof(float)); for(j=0;j < num_infiles;j++) { //file loop int k,got; memset((char *)inframe,0,inchans * sizeof(float)); if(ifdlist[j] < 0){ got = inchans; // placeholder - write silent channel } else{ if((got = psf_sndReadFloatFrames(ifdlist[j],inframe,1)) < 0){ fprintf(stderr,"interlx: error reading from infile %s\n",argv[2+j]); psf_sndClose(ofd); cleanup(ifdlist); return 1; } } if(got==1){ p_filesamp = inframe; for(k=0;k < inchans;k++) //channel loop *p_framesamp++ = *p_filesamp++; } else p_framesamp += inchans; } if(psf_sndWriteFloatFrames(ofd,outframe,1) < 0){ fprintf(stderr,"interlx: error writing to outfile\n"); psf_sndClose(ofd); cleanup(ifdlist); return 1; } if(i % halfsec==0) { printf("%.2lf secs\r",(double)i / (double) firstinprops.srate); fflush(stdout); } } printf("%.4lf secs\nWritten %ld sample frames to %s\n",(double)outframesize / (double)firstinprops.srate,outframesize,argv[1]); if(psf_sndReadPeaks( ofd,fpeaks,&peaktime)){ printf("PEAK data:\n"); for(i = 0; i < firstinprops.chans; i++) { double val, dbval; val = (double) fpeaks[i].val; dbval = 20.0 * log10(val); printf("CH %d: %.6f (%.2lfdB) at frame %u:\t%.4f secs\n",i, val,dbval,(unsigned int) fpeaks[i].pos,(double)fpeaks[i].pos / (double) firstinprops.srate); } } printf("\n"); psf_sndClose(ofd); free(inframe); free(outframe); if(fpeaks) free(fpeaks); cleanup(ifdlist); return 0; }
int main (int argc, char**argv) { PSF_PROPS outprops; unsigned long srate; double amp, freq, pwval, dur, peakdiff; unsigned long nbufs, outframes, remainder, nframes; psf_format outformat = PSF_FMT_UNKNOWN; int wavetype=-1; float wavevalue; int nchans; int ispwval=0; /* flagged if pwval is a value and not a breakpoint file */ char* arg_pwmod; /* retains the name of the pwval breakpoint file */ double minval, maxval; /* used for breakpoint amplitude values */ tickfunc tick; /* point to the specified wavetype function */ char option; /* stores command line options */ psf_stype samptype = PSF_SAMP_16; /* outfile is set to 16-bit by default */ double phase=0.0; /* fraction between 0 and 1 which sets the phase offset default values match the direction of the sine wave (i.e starting at zero, going positive) */ /* init resource values */ int ofd=-1; int error=0; float* outbuf = NULL; /* buffer for outfile */ OSCIL* p_osc = NULL; /* waveform oscillator */ BRKSTREAM* ampstream = NULL; /* breakpoint stream of amplitude values */ BRKSTREAM* freqstream = NULL; /* breakpoint stream of frequency values */ BRKSTREAM* pwmodstream = NULL; /* breakpoint stream of pwmod values */ FILE* fpamp = NULL; /* amplitude breakpoint file */ FILE* fpfreq = NULL; /* frequency breakpoint file */ FILE* fppwmod = NULL; /* pulse width modulation breakpoint file */ unsigned long brkampSize = 0; /* number of amplitude breakpoints */ unsigned long brkfreqSize = 0; /* number of frequency breakpoints */ unsigned long brkpwmodSize = 0; /* number of pulse wave mod breakpoints */ printf("SIGGEN: generate a simple waveform\n"); /* check for command-line options */ if (argc>1) { while (argv[1][0] == '-') { option = argv[1][1]; switch (option) { case('\0'): printf("ERROR: you did not specify an option\n" "options: -sN select the format of the output sound file\n" " N = 16 (16-bit), 24 (24-bit), or 32 (32-bit)\n" ); return 1; case('s'): if (argv[1][2]=='\0') { printf("ERROR: you did not specify a format\n" "formats: 16 (16-bit), 24 (24-bit), or 32 (32-bit)\n", &argv[1][2]); return 1; } samptype = atoi(&argv[1][2]); if (samptype == 16) { samptype = PSF_SAMP_16; break; } if (samptype == 24) { samptype = PSF_SAMP_24; break; } if (samptype == 32) { samptype = PSF_SAMP_32; break; } printf("ERROR: %s is not a valid format\n" "formats: 16 (16-bit), 24 (24-bit), or 32 (32-bit)\n", &argv[1][2]); return 1; case('o'): if (argv[1][2]=='\0') { printf("ERROR: you did not specify an offset\n" "USAGE: -oN (0 <= N <= 1)\n" ); return 1; } if (!(argv[1][2] >= 'a' && argv[1][2]<='z')) { phase = atof(&argv[1][2]); if (phase < 0.0) phase = 0.0; if (phase > 1.0) phase = 1.0; break; } default: printf("ERROR: %s is not a valid option\n" "options: -sN select the format of the output sound file\n" " N = 16 (16-bit), 24 (24-bit), or 32 (32-bit)\n" " -oN select the offset of the oscillator\n" " 0 <= N <= 1\n", argv[1]); return 1; } argc--; argv++; } } if (argc!=ARG_NARGS) if (argc==(ARG_NARGS+1)) { /* check if pwval is specified */ fppwmod = fopen(argv[ARG_PWMOD], "r"); if (fppwmod==NULL) { if (argv[ARG_PWMOD][0] >='a' && argv[ARG_PWMOD][0] <= 'z') { printf("Error: breakpoint file \"%s\" does not exist.\n", argv[ARG_PWMOD]); return 1; } pwval = atof(argv[ARG_PWMOD]); if (pwval < 1 || pwval > 99) { printf("Error: pwval is out of range.\n" " 1 <= pwval <= 99\n"); return 1; } /* we have gathered a pwval value */ ispwval=1; } else { /* breakpoint file exists, gather breakpoint values */ pwmodstream = bps_newstream(fppwmod,atof(argv[ARG_SRATE+1]),&brkpwmodSize); if (pwmodstream==NULL) { printf("ERROR: unable to obtain breakpoints from \"%s\"\n", argv[ARG_PWMOD]); error++; goto exit; } } /* make sure enum ARG values are aligned with command-line arguments */ int argnum = ARG_PWMOD; arg_pwmod = argv[ARG_PWMOD]; while (argnum != (ARG_NARGS+1)) argv[argnum] = argv[++argnum]; } else { printf("ERROR: insufficient number of arguments.\n" "USAGE: siggen [-sN] [-oN] outsndfile wavetype [pwval] duration srate nchans amp freq\n" " -sN: select the format of the output sound file (16-bit by default)\n" " N = 16 (16-bit), 24 (24-bit), or 32 (32-bit)\n" " -oN: select the offset of the oscillator\n" " 0 <= N <= 1\n" " if you set N < 0, then N is set to 0\n" " if you set N > 1, then N is set to 1\n" " wavetype: sine, triangle, square, pwmod, sawtooth_up, sawtooth_down\n" " pwval: pulse wave percentage value or breakpoint file\n" " modulation range is from 1%% to 99%%\n" " a normal square wave is 50%%\n" " pwval must be selected only when the\n" " pwmod wavetype has been selected.\n" " duration: duration of outfile (seconds)\n" " srate: required sample rate of outfile\n" " amp: amplitude value or breakpoint file\n" " (0 < amp <= 1.0)\n" " freq: frequency value or breakpoint file\n" " (freq > 0 )\n" ); return 1; } /** at this point we may have gathered resources we henceforth use goto upon hitting any errors **/ /* define outfile format */ srate = atof(argv[ARG_SRATE]); if (srate<=0) { printf("ERROR: sample rate must be positive.\n"); error++; goto exit; } outprops.srate = srate; outprops.samptype = samptype; outprops.chformat = PSF_STDWAVE; /* get outfile extension */ outformat = psf_getFormatExt(argv[ARG_OUTFILE]); if (outformat==PSF_FMT_UNKNOWN) { printf("Outfile name \"%s\" has unknown format.\n" "Use any of .wav .aiff .aif .afc .aifc\n", argv[ARG_OUTFILE] ); error++; goto exit; } outprops.format = outformat; /* get number of channels */ nchans = atoi(argv[ARG_NCHANS]); if (nchans > MC_WAVE_EX) { printf("ERROR: portsf does not support %d channels\n", nchans); error++; goto exit; } outprops.chans = nchans; /* get the wavetype */ int count = 0; while (argv[ARG_TYPE][count]!='\0') count++; switch (count) { case(WAVE_SINE): if (!strcmp(argv[ARG_TYPE],"sine")) wavetype = WAVE_SINE; break; case(WAVE_PWMOD): if (!strcmp(argv[ARG_TYPE],"pwmod")) wavetype = WAVE_PWMOD; break; case(WAVE_SQUARE): if (!strcmp(argv[ARG_TYPE],"square")) wavetype = WAVE_SQUARE; break; case(WAVE_TRIANGLE): if (!strcmp(argv[ARG_TYPE],"triangle")) wavetype = WAVE_TRIANGLE; break; case(WAVE_SAWUP): if (!strcmp(argv[ARG_TYPE],"sawtooth_up")) wavetype = WAVE_SAWUP; break; case(WAVE_SAWDOWN): if (!strcmp(argv[ARG_TYPE],"sawtooth_down")) wavetype = WAVE_SAWDOWN; break; default: wavetype = -1; } if (wavetype<0) { printf("ERROR: you have chosen an unknown wavetype.\n" "wavetypes: sine, square, pwmod, triangle, sawtooth_up, sawtooth_down\n" ); error++; goto exit; } /* if user specifies pwval, make sure pwmod wavetype is selected */ if (pwmodstream||ispwval) if (wavetype!=WAVE_PWMOD) { printf("ERROR: if you select a value or breakpoint file for pwval,\n" " you must select the pwmod wavetype.\n"); error++; goto exit; } /* select waveform function */ switch (wavetype) { case(WAVE_SINE): tick = sinetick; phase += 0.0; break; case(WAVE_TRIANGLE): tick = tritick; phase += 0.75; break; case(WAVE_SQUARE): tick = sqtick; phase += 0.0; break; case(WAVE_PWMOD): if (pwmodstream==NULL && !ispwval) { printf("ERROR: you didn't specify a pulse wave value " "or breakpoint file.\n"); error++; goto exit; } phase += 0.0; break; case(WAVE_SAWUP): tick = sawutick; phase += 0.5; break; case(WAVE_SAWDOWN): tick = sawdtick; phase += 0.5; } /* get the time duration of the soundfile */ dur = atof(argv[ARG_DUR]); if (dur<=0.0) { printf("ERROR: time duration must be positive.\n"); error++; goto exit; } /* open breakpoint file, or set constant amplitude */ fpamp = fopen(argv[ARG_AMP],"r"); if (fpamp==NULL) { if (argv[ARG_AMP][0] >= 'a' && argv[ARG_AMP][0] <= 'z') { printf("ERROR: breakpoint file \"%s\" does not exist.\n", argv[ARG_AMP]); error++; goto exit; } amp = atof(argv[ARG_AMP]); if (amp<=0.0 || amp>1.0) { printf("ERROR: amplitude value out of range.\n" " 0.0 < amp <= 1.0\n" ); error++; goto exit; } } else { ampstream = bps_newstream(fpamp,outprops.srate,&brkampSize); if (ampstream==NULL) { printf("ERROR: unable to obtain breakpoints from \"%s\"\n", argv[ARG_AMP]); error++; goto exit; } if (bps_getminmax(ampstream,&minval,&maxval)) { printf("Error reading range of breakpoint file \"%s\"\n", argv[ARG_AMP]); error++; goto exit; } if (minval < 0.0 || minval > 1.0 || maxval < 0.0 || maxval > 1.0) { printf("Error: amplitude values out of range in file \"%s\"\n" " 0.0 < amp <= 1.0\n", argv[ARG_AMP]); error++; goto exit; } } /* open breakpoint file, or set constant frequency */ fpfreq = fopen(argv[ARG_FREQ],"r"); if (fpfreq==NULL) { if (argv[ARG_FREQ][0] >= 'a' && argv[ARG_FREQ][0] <='z') { printf("ERROR: breakpoint file \"%s\" does not exist.\n", argv[ARG_FREQ]); error++; goto exit; } freq = atof(argv[ARG_FREQ]); if (freq<=0.0) { printf("ERROR: frequency must be positive.\n"); error++; goto exit; } } else { freqstream = bps_newstream(fpfreq,outprops.srate,&brkfreqSize); if (freqstream==NULL) { printf("ERROR: unable to obtain breakpoint from \"%s\"\n", argv[ARG_FREQ]); error++; goto exit; } } /* start portsf */ psf_init(); ofd = psf_sndCreate(argv[ARG_OUTFILE],&outprops,0,0,PSF_CREATE_RDWR); if (ofd<0) { printf("ERROR: unable to create outfile: %s\n",argv[ARG_OUTFILE]); error++; goto exit; } /* initialize waveform oscillator */ p_osc = new_oscilp(outprops.srate, phase); /* calculate the number of frames for the soundfile */ outframes = (unsigned long) (dur * outprops.srate + 0.5); /* allocate a buffer for outfile */ nframes = NFRAMES; outbuf = (float*)malloc(sizeof(float)*nframes*outprops.chans); /* calculate the number of buffers */ nbufs = outframes/nframes; remainder = outframes - nbufs*nframes; if (remainder > 0) nbufs++; printf("Creating soundfile...\n"); /* process soundfile */ long i,j,i_out; for (i=0; i < nbufs; i++) { if ((i == (nbufs-1)) && remainder) nframes = remainder; for (j=0; j < nframes; j++) { if (ampstream) amp = bps_tick(ampstream); if (freqstream) freq = bps_tick(freqstream); if (pwmodstream) pwval = bps_tick(pwmodstream); if (pwmodstream || ispwval ) wavevalue = (float)(amp * pwmtick(p_osc,freq,pwval)); else wavevalue = (float)(amp * tick(p_osc,freq)); for (i_out=0; i_out < outprops.chans; i_out++) outbuf[j*outprops.chans+i_out] = wavevalue; } if (psf_sndWriteFloatFrames(ofd,outbuf,nframes)!=nframes) { printf("Error writing to outfile\n"); error++; break; } } /* make sure peak amplitude roughly matches the user requested amplitude */ if (ampstream==NULL) peakdiff = amp-psf_sndPeakValue(ofd,&outprops); else peakdiff = maxval-psf_sndPeakValue(ofd,&outprops); if ((peakdiff>.001) || (peakdiff<-.001)) { printf("ERROR: unable to generate the correct peak\n" " amplitude for %s\n", argv[ARG_OUTFILE]); error++; } printf("Done.\t%d error%s\n" "Outfile created: %s\n", error, (error==1)?"":"s", argv[ARG_OUTFILE]); /* display outfile properties */ psf_sndInfileProperties(argv[ARG_OUTFILE],ofd,&outprops); /* cleanup resources */ exit: if (ofd>=0) if (psf_sndClose(ofd)) printf("Error: unable to close outfile: %s\n",argv[ARG_OUTFILE]); else if (error) { printf("There was an error while processing the outfile.\n" "Deleting outfile: %s ...\n", argv[ARG_OUTFILE]); if (remove(argv[ARG_OUTFILE])) printf("Error: failed to delete %s\n", argv[ARG_OUTFILE]); else printf("%s successfully deleted.\n", argv[ARG_OUTFILE]); } if (outbuf) { free(outbuf); outbuf=NULL; } if (p_osc) { free(p_osc); p_osc=NULL; } if (ampstream) bps_freepoints(ampstream); if (freqstream) bps_freepoints(freqstream); if (pwmodstream) bps_freepoints(pwmodstream); if (fpamp) if (fclose(fpamp)) { printf("Error closing breakpoint file \"%s\"\n", argv[ARG_AMP]); } if (fpfreq) if (fclose(fpfreq)) { printf("Error closing breakpoint file \"%s\"\n", argv[ARG_FREQ]); } if (fppwmod) if (fclose(fppwmod)) { printf("Error closing breakpoint file \"%s\"\n", arg_pwmod); } psf_finish(); return error; }
int main(int argc, char**argv) { long inframes,framesread=0,total_framesread; double maxsamp = 0.0,startpos = 0.0,endpos; long startframe = 0,endframe; long halfsecframes = 0; int i,j,ifd = -1; int chans; int do_norm = 0; PSF_PROPS inprops; CH_RMSINFO* rmsinfo = NULL; double* rmsfac = 0; double* ampsum = 0; double* ampsumb = 0; double* inbuf = 0; double* nrmsfac = 0; double* nampsum = 0; /* CDP version number */ if(argc==2 && (stricmp(argv[1],"--version")==0)){ printf("1.0.1\n"); return 0; } #ifdef WIN32 # if _MSC_VER && _MSC_VER <= 1200 _set_new_handler( newhandler ); # endif #endif if(psf_init()){ puts("unable to start portsf\n"); return 1; } if(argc < 2){ usage(); return(1); } while(argv[1][0]=='-'){ switch(argv[1][1]){ case 'n': do_norm = 1; break; default: fprintf(stderr, "Unrecognised flag option %s\n",argv[1]); return 1; } argc--; argv++; } if(argc < 2){ usage(); return(1); } if((ifd = psf_sndOpen(argv[ARG_INFILE],&inprops, 0)) < 0){ fprintf(stderr,"\nUnable to open input soundfile %s",argv[ARG_INFILE]); return(1); } inframes = psf_sndSize(ifd); // m/c frames endframe = inframes; if(inframes <= 0) return 0; if(argc >= 3) { long lpos; startpos = atof(argv[ARG_INFILE+1]); if(startpos < 0.0){ fprintf(stderr,"Error: startpos must be positive\n"); return 1; } lpos = (long)( startpos * inprops.srate); if(lpos > inframes){ fprintf(stderr,"Error: startpos value beyond end of file.\n"); return 1; } startframe = lpos; } if(argc >= 4) { long lpos; endpos = atof(argv[ARG_INFILE+2]); lpos = (long)(endpos * inprops.srate); if(lpos > inframes){ fprintf(stderr,"Warning: endpos value too large - reset to end of file.\n"); return 1; } endframe = lpos; if(!(endframe > startframe)){ fprintf(stderr,"Error: endpos must be beyond startpos.\n"); return 1; } } if(startframe) printf("Starting at frame %ld, ending at frame %ld\n",startframe, endframe); chans = inprops.chans; try { inbuf = new double[BUFLEN * chans]; rmsinfo = new CH_RMSINFO[chans]; rmsfac = new double[chans]; ampsum = new double[chans]; ampsumb = new double[chans]; nrmsfac = new double[chans]; nampsum = new double[chans]; } catch(...){ fputs("no memory!\n",stderr); return 1; } for(i=0; i < chans; i++){ rmsfac[i] = 0.0; ampsum[i] = 0.0; ampsumb[i] = 0.0; nrmsfac[i] = 0.0; nampsum[i] = 0.0; } halfsecframes = inprops.srate / 2; signal(SIGINT,runhandler); long wanted = endframe - startframe; printf("Scanning %ld frames (%.3lf secs):\n",wanted, (double)wanted / inprops.srate); total_framesread = 0; if(startframe) { if(psf_sndSeek(ifd,startframe,PSF_SEEK_SET)){ fprintf(stderr,"File Seek error.\n"); return 1; } } while((framesread = psf_sndReadDoubleFrames(ifd,inbuf,BUFLEN)) > 0){ double fval; for(i = 0;i < framesread;i++) { for(j = 0; j < chans; j++){ double val = inbuf[i*chans + j]; fval = fabs(val); maxsamp = fval > maxsamp ? fval : maxsamp; ampsum[j] += fval; rmsfac[j] += val*val; ampsumb[j] += val; } total_framesread++; if(scanning==0) break; if(total_framesread == wanted) break; if((total_framesread % halfsecframes) == 0){ printf("%.2lf\r",total_framesread / (double) inprops.srate); fflush(stdout); } } if(total_framesread == wanted) { break; } } if(framesread < 0){ fprintf(stderr,"Error reading file.\n"); return 1; } for(i=0;i < chans;i++){ rmsfac[i] /= total_framesread; rmsfac[i] = sqrt(rmsfac[i]); ampsum[i] /= total_framesread; ampsumb[i] /= total_framesread; } double normfac = 1.0 / maxsamp; if(scanning==0) printf("\nScan stopped.\n"); if(total_framesread < inframes){ printf("Scanned %ld frames (%.2lf secs).\n",total_framesread,total_framesread / (double)inprops.srate); } printf("Maximum sample = %lf (%.2lfdB)\n",maxsamp,20.0 * log10(maxsamp)); printf("Maximum normalisation factor = %.4f\n",normfac); for(i=0;i < chans;i++){ rmsinfo[i].rmspower = rmsfac[i]; rmsinfo[i].abssum = ampsum[i]; rmsinfo[i].bisum = ampsumb[i]; rmsinfo[i].norm_rms = normfac * rmsfac[i]; rmsinfo[i].norm_abssum = normfac * ampsum[i]; } if(do_norm){ printf("\t RMS LEVEL\t AVG \t NET DC\t NORM RMS\t NORM AVG\n"); printf("CH\t AMP\t DB\t AMP\t DB\t AMP\t DB\t AMP\t DB\t AMP\t DB \n"); } else{ printf("\t RMS LEVEL\t AVG \t NET DC\n"); printf("CH\t AMP\t DB\t AMP\t DB\t AMP\t DB\n"); } for(i=0;i < chans;i++){ double d1,d2,d3,d4,d5; d1 = 20*log10(rmsfac[i]); d2 = 20*log10(ampsum[i]); d3 = 20*log10(fabs(ampsumb[i])); d4 = 20*log10(normfac * rmsfac[i]); d5 = 20*log10(normfac * ampsum[i]); if(do_norm){ printf("%d\t%.5lf\t%.2lf\t%.5lf\t%.2lf\t%+.4lf\t%.2lf\t%.5lf\t%.2lf\t%.5lf\t%.2lf\n",i+1, rmsinfo[i].rmspower,d1, rmsinfo[i].abssum,d2, rmsinfo[i].bisum,d3, rmsinfo[i].norm_rms,d4, rmsinfo[i].norm_abssum,d5 ); } else { printf("%d\t%.5lf\t%.2lf\t%.5lf\t%.2lf\t%+.4lf\t%.2lf\n",i+1, rmsinfo[i].rmspower,d1, rmsinfo[i].abssum,d2, rmsinfo[i].bisum,d3 ); } } delete [] inbuf; delete [] rmsfac; delete [] ampsum; delete [] ampsumb; delete [] rmsinfo; psf_sndClose(ifd); psf_finish(); return 0; }
int main(int argc, char* argv[]) { int i; PSF_PROPS props; long framesread; long totalread; /* init all dynamic resources to default states */ int ifd = -1,ofd = -1; int error = 0; PSF_CHPEAK* peaks = NULL; float* frame = NULL; psf_format outformat = PSF_FMT_UNKNOWN; printf("SF2FLOAT: convert soundfile to 32bit floats format\n"); if(argc < 4){ printf("insufficient arguments.\n" "usage:\n\t" "sf2float option infile outfile\n"); return 1; } /* be good, and startup portsf */ if(psf_init()){ printf("unable to start portsf\n"); return 1; } ifd = psf_sndOpen(argv[2],&props,0); if(ifd < 0 && atoi(argv[1])!=5){ printf("Error: unable to open infile %s\n",argv[2]); return 1; } if(props.samptype == PSF_SAMP_16){ printf("Info: infile is in 16 bit format.\n"); } if(props.samptype == PSF_SAMP_24){ printf("Info: infile is in 24 bit format.\n"); } if(props.samptype == PSF_SAMP_32){ printf("Info: infile is in 32 bit format.\n"); } /* we now have a resource, so we use goto hereafter on hitting any error */ /* tell user if source file is already floats */ if(props.samptype == PSF_SAMP_IEEE_FLOAT){ printf("Info: infile is already in floats format.\n"); } props.samptype = PSF_SAMP_IEEE_FLOAT; /* check file extension of outfile name, so we use correct output file format*/ // TODO: needs to be changed because output is not always the same format if (atoi(argv[1])<3 || atoi(argv[1])==5) { outformat = psf_getFormatExt(argv[3]); printf("output file format: %d", outformat); if(outformat == PSF_FMT_UNKNOWN){ printf("outfile name %s has unknown format.\n" "Use any of .wav, .aiff, .aif, .afc, .aifc\n",argv[3]); error++; goto exit; } } else if (atoi(argv[1])==4) { outformat = psf_getFormatExt(argv[4]); printf("output file format: %d", outformat); if(outformat == PSF_FMT_UNKNOWN){ printf("outfile name %s has unknown format.\n" "Use any of .wav, .aiff, .aif, .afc, .aifc\n",argv[4]); error++; goto exit; } } props.format = outformat; if(atoi(argv[1])<3 || atoi(argv[1])==5) { props.srate = 44100; props.chans = 2; props.samptype = PSF_SAMP_IEEE_FLOAT; props.format = PSF_STDWAVE; props.chformat = STDWAVE; ofd = psf_sndCreate(argv[3],&props,0,0,PSF_CREATE_RDWR); if(ofd < 0){ printf("Error: unable to create outfile %s\n",argv[3]); error++; goto exit; } } else if (atoi(argv[1])==4) { ofd = psf_sndCreate(argv[4],&props,0,0,PSF_CREATE_RDWR); if(ofd < 0){ printf("Error: unable to create outfile %s\n",argv[4]); error++; goto exit; } } /* allocate space for one sample frame */ frame = (float*) malloc(props.chans * sizeof(float)); if(frame==NULL){ puts("No memory!\n"); error++; goto exit; } /* and allocate space for PEAK info */ peaks = (PSF_CHPEAK*) malloc(props.chans * sizeof(PSF_CHPEAK)); if(peaks == NULL){ puts("No memory!\n"); error++; goto exit; } // if(atoi(argv[1])!=5) { //printf("copying....\n"); /* single-frame loop to do copy: report any read/write errors */ // framesread = psf_sndReadFloatFrames(ifd,frame,1); // totalread = 0; /* count sample frames as they are copied */ // } int part = atoi(argv[1]); /* * Switch depending on the part of the assignment */ switch(part) { /* * Part 0: Output the file as is. */ case 0: while (framesread == 1){ totalread++; if(psf_sndWriteFloatFrames(ofd,frame,1) != 1){ printf("Error writing to outfile\n"); error++; break; } framesread = psf_sndReadFloatFrames(ifd,frame,1); } break; /* * Part 1: Divide the value of each sample by 2. */ case 1: { while (framesread == 1){ frame[0] = frame[0]/2; frame[1] = frame[1]/2; totalread++; if(psf_sndWriteFloatFrames(ofd,frame,1) != 1){ printf("Error writing to outfile\n"); error++; break; } framesread = psf_sndReadFloatFrames(ifd,frame,1); } break; } /* * Part 2: Pan the output from left to right, with an interval of 5 seconds. */ case 2: { double cycle = 5; // cycle of 5 seconds double period = 4 * cycle; // sine or cosine period double sampleLimit = period * 44100; while (framesread == 1){ frame[0] = frame[0]*sin(2*M_PI*totalread/sampleLimit); frame[1] = frame[1]*cos(2*M_PI*totalread/sampleLimit); totalread++; if(psf_sndWriteFloatFrames(ofd,frame,1) != 1){ printf("Error writing to outfile\n"); error++; break; } framesread = psf_sndReadFloatFrames(ifd,frame,1); } break; } /* * Part 3: Create envelope file. */ case 3: { double max = 0; // to keep track of the max in the interval int currentSample = 0; double intervalTime = 0.05; double sampleLimit = 44100*intervalTime; int interval = 1; FILE *file; file = fopen(argv[3], "w+"); printf("sampleLimit %f\n", sampleLimit); while (framesread == 1){ if (currentSample < sampleLimit) { if (max < frame[0]) { max = frame[0]; } if (max < -1*frame[0]) { max = -1*frame[0]; } if (max < frame[1]) { max = frame[1]; } if (max < -1*frame[1]) { max = -1*frame[1]; } currentSample++; } else { fprintf(file, "%f %f\n", (double)(interval*intervalTime), max); max = 0; currentSample = 0; interval++; } totalread++; framesread = psf_sndReadFloatFrames(ifd,frame,1); } fclose(file); break; } /* * apply envelope on file */ case 4: { // open up file, put the reading index on the first line FILE *file; file = fopen(argv[3], "r"); double intervalTime = 0.05; char intervalLimitString[20], valueString[20], space[1]; double intervalLimit=-1, value=0; // to do the linear regression and get the factor double prevValue = 0; double slope = 0; double intercept = 0; double factor = 1; double current=0; while (framesread == 1) { if (!feof(file)) { if(current/44100 >= intervalLimit ) { if(fgets(intervalLimitString, 10, file) != NULL) { intervalLimit = strtod(intervalLimitString, NULL); } if(fgets(space, 1, file) != NULL) {} if(fgets(valueString, 12, file) != NULL) { prevValue = value; value = strtod(valueString, NULL); slope = (value-prevValue)/intervalTime; intercept = prevValue; } } factor = ((double)(totalread%((int)(44100*intervalTime)))/44100)*slope + intercept; frame[0] = frame[0]*factor; frame[1] = frame[1]*factor; int tt = totalread%2205; // printf("time: %d \t value: %f \t slope %f \t intercep %f \t will multiply by: %f\n", tt, value, slope, intercept, factor); } current++; totalread++; if(psf_sndWriteFloatFrames(ofd,frame,1) != 1){ printf("Error writing to outfile\n"); error++; break; } framesread = psf_sndReadFloatFrames(ifd,frame,1); } break; } /* * add up sine waves */ case 5: { FILE *file; file = fopen(argv[2], "r"); char sineAmplitudeString[20], sineFrequencyString[20], space[1]; double sineAmplitude, sineFrequency; double sineAmplitudes[20], sineFrequencies[20]; int currentIndex = 0; int arraySize=0; while (!feof(file)) { if(fgets(sineFrequencyString, 5, file) != NULL) { sineFrequency = strtod(sineFrequencyString, NULL); if(sineFrequency==-1) break; else { sineFrequencies[currentIndex] = sineFrequency; if(fgets(space, 1, file) != NULL) {} if(fgets(sineAmplitudeString, 20, file) != NULL) { sineAmplitude = strtod(sineAmplitudeString, NULL); sineAmplitudes[currentIndex] = sineAmplitude; currentIndex++; } } } } arraySize = currentIndex; currentIndex = 0; int i; for(i=0; i<arraySize; i++) { printf(" %f %f \n", sineAmplitudes[i], sineFrequencies[i]); } // do i assume it is 0.05? double intervalTime = 0.05; char intervalLimitString[20], valueString[20]; double intervalLimit=-1, value=0; // to do the linear regression and get the factor double prevValue = 0; double slope = 0; double intercept = 0; double factor = 1; double current=0; while(!feof(file)){ if(current/44100 >= intervalLimit ) { if(fgets(intervalLimitString, 10, file) != NULL) { intervalLimit = strtod(intervalLimitString, NULL); } if(fgets(space, 1, file) != NULL) {} if(fgets(valueString, 12, file) != NULL) { prevValue = value; value = strtod(valueString, NULL); slope = (value-prevValue)/intervalTime; intercept = prevValue; } } factor = ((double)((int)current%((int)(44100*intervalTime)))/44100)*slope + intercept; printf("%f\n", factor); // compute frame[1] and frame[2] frame[0] = 0; int i; for(i=0; i<arraySize; i++) { frame[0] += sineAmplitudes[i]*sin(2*M_PI*current*sineFrequencies[i]/44100); } frame[0] = frame[0]*factor; frame[1] = frame[0]; if(psf_sndWriteFloatFrames(ofd,frame,1) != 1){ printf("Error writing to outfile\n"); error++; break; } current++; } break; } default: printf("default case"); break; } if(framesread < 0) { printf("Error reading infile. Outfile is incomplete.\n"); error++; } else { if (atoi(argv[1])<3) { printf("Done. %ld sample frames copied to %s\n",totalread,argv[3]); } else if (atoi(argv[1])==4) { printf("Done. %ld sample frames copied to %s\n",totalread,argv[4]); } } /* report PEAK values to user */ if(psf_sndReadPeaks(ofd,peaks,NULL) > 0){ long i; double peaktime; printf("PEAK information:\n"); for(i=0;i < props.chans;i++){ peaktime = (double) peaks[i].pos / (double) props.srate; printf("CH %ld:\t%.4f at %.4f secs\n", i+1, peaks[i].val, peaktime); } } /* do all cleanup */ exit: if(ifd >= 0) psf_sndClose(ifd); if(ofd >= 0) psf_sndClose(ofd); if(frame) free(frame); if(peaks) free(peaks); psf_finish(); return error; }
int main(int argc, char**argv) { PSF_PROPS props; long framesread, totalread; DWORD nFrames; int size; int limit; int N; // copy infile N times /* init all resource vals to default states */ int ifd=-1, ofd=-1; int error=0; psf_format outformat = PSF_FMT_UNKNOWN; PSF_CHPEAK* peaks = NULL; float* buffer = NULL; printf ("SF2FLOAT: convert soundfile to floats format.\n"); if (argc!=ARG_NARGS) { printf("insufficient arguments.\n" "USAGE:\tsf2float infile outfile buffer limit N\n"); return 1; } /* startup portsf */ if(psf_init()) { printf("ERROR: unable to start portsf\n"); return 1; } /* initialize buffer */ nFrames = (DWORD)atoi(argv[ARG_BUFF]); if (nFrames < 1) { printf("ERROR: buffer size must be at least 1\n"); return 1; } /* initialize limit */ limit = atoi(argv[ARG_LIMIT]); if (limit<1) { printf("ERROR: size limit must be positive.\n"); return 1; } /* initialize N */ N = atoi(argv[ARG_N]); if (N<1) { printf("ERROR: N must be at least one.\n"); return 1; } /* open infile */ ifd = psf_sndOpen(argv[ARG_INFILE], &props, 0); if (ifd<0) { printf("ERROR: unable to open infile \"%s\"\n",argv[ARG_INFILE]); return 1; } /* we now have a resource, so we use goto hereafter on hitting any error */ /* get number of frames from infile */ size = psf_sndSize(ifd); if(size<0) { printf("ERROR: unable to obtain the size of \"%s\"\n",argv[ARG_INFILE]); error++; goto exit; } /* check if copy limit is less than size */ if(size<limit) { printf("ERROR: infile size is less than the copy limit.\n" "infile:\t%s\n" "infile size:\t%d frames\n" "copy limit:\t%d frames\n", argv[ARG_INFILE], size, limit); error++; goto exit; } /* tell user if source file is already floats */ if (props.samptype==PSF_SAMP_IEEE_FLOAT) printf("Info: infile is already in floats format.\n"); /* check if infile uses 8-bit samples*/ if (props.samptype==PSF_SAMP_8) { printf("ERROR: sf2float does not support 8-bit format.\n"); error++; goto exit; } if(!psf_sndInfileProperties(argv[ARG_INFILE],ifd,&props)) { error++; goto exit; } props.samptype=PSF_SAMP_IEEE_FLOAT; /* check if outfile extension is one we know about */ outformat = psf_getFormatExt(argv[ARG_OUTFILE]); if (outformat == PSF_FMT_UNKNOWN) { printf("Outfile name \"%s\" has unknown format.\n" "Use any of .wav .aiff .aif .afc .aifc\n", argv[ARG_OUTFILE]); error++; goto exit; } props.format = outformat; /* create outfile */ ofd = psf_sndCreate(argv[ARG_OUTFILE], &props, 0, 0, PSF_CREATE_RDWR); if (ofd<0) { printf("ERROR: unable to create outfile \"%s\"\n",argv[ARG_OUTFILE]); error++; goto exit; } /* allocate space for sample frames */ if (limit<nFrames) nFrames = (DWORD)limit; buffer= (float*)malloc(props.chans*sizeof(float)*nFrames); if (buffer==NULL) { puts("No memory!\n"); error++; goto exit; } /* and allocate space for PEAK info */ peaks = (PSF_CHPEAK*)malloc(props.chans*sizeof(PSF_CHPEAK)); if (peaks==NULL) { puts("No memory!\n"); error++; goto exit; } printf("copying...\n"); int i=0; int j; /* copy the infile N times */ for (j=0; j<N; j++) { /* make sure to set nFrames to the correct value every time you pass through the for loop */ if (limit < atoi(argv[ARG_BUFF])) nFrames = (DWORD)limit; else nFrames = (DWORD)atoi(argv[ARG_BUFF]); totalread = 0; /* running count of sample frames */ if(psf_sndSeek(ifd,0,PSF_SEEK_SET)) { printf("ERROR: cannot reset infile\n"); error++; goto exit; } /* nFrames loop to do copy, report any errors */ framesread = psf_sndReadFloatFrames(ifd,buffer,nFrames); while (framesread>0&&totalread<limit) { i++; /* update copy status after refreshing the buffer every 100 times */ if (i%100==0) printf("%ld samples copied... %ld%%\r",totalread,100*totalread/size); totalread+=framesread; if(psf_sndWriteFloatFrames(ofd,buffer,nFrames)<0) { printf("Error writing to outfile.\n"); error++; break; } /* make sure not to copy frames past the limit */ if (nFrames+totalread > limit) nFrames = (DWORD)(limit-totalread); framesread = psf_sndReadFloatFrames(ifd,buffer,nFrames); } } totalread *= N; /* total number of frames copied */ if(framesread<0) { printf("Error reading infile. Outfile is incomplete.\n"); error++; } else printf("Done. %ld sample frames copied to %s\n", totalread, argv[ARG_OUTFILE]); /* report PEAKS to user */ if (psf_sndReadPeaks(ofd,peaks,NULL)>0) { long i; double peaktime; double peakDB; printf("Peak information:\n"); for (i=0; i<props.chans; i++) { peaktime = (double)peaks[i].pos/props.srate; if (peaks[i].val == 0.0) peaks[i].val = 1.0e-4; peakDB = log10(peaks[i].val); printf("CH %ld:\t%.4f\t(%.4f dB) at %.4f secs\n", i+1, peaks[i].val, peakDB, peaktime); } } /* do all the cleanup */ exit: if (ifd>=0) psf_sndClose(ifd); if (ofd>=0) psf_sndClose(ofd); if (buffer) free(buffer); if (peaks) free(peaks); psf_finish(); return error; }
int main(int argc, char**argv) { PSF_PROPS props; long framesread, totalread; DWORD nFrames; int size; int limit; int N; // copy infile N times float ampfac; float dbval; /* init all resource vals to default states */ int ifd=-1, ofd=-1; int error=0; psf_format outformat = PSF_FMT_UNKNOWN; PSF_CHPEAK* peaks = NULL; float* buffer = NULL; /* init flags for command-line options */ int isamp=0; // default scale factor is in dBs printf ("SFGAIN: change level of soundfile.\n"); if ((argc<ARG_NOPS)||(argc>ARG_OPS)) { printf("insufficient arguments.\n" "USAGE:\tsfgain infile outfile buffer limit N [dBval | -a ampfac]\n" "dBval must be <= 0 or ampfac must be > 0\n"); return 1; } /* check for command-line options */ if (argc==ARG_OPS) { if (argv[ARG_OP][0]=='-') { if (argv[ARG_OP][1]=='a') isamp=1; else { printf("ERROR: %s is not a valid command-line option.\n" "USAGE:\tsfgain infile outfile buffer limit N [dBval | -a ampfac]\n" "dBval must be <= 0 or ampfac must be > 0\n", argv[ARG_OP]); return 1; } } } /* startup portsf */ if(psf_init()) { printf("ERROR: unable to start portsf\n"); return 1; } /* initialize buffer */ nFrames = (DWORD)atoi(argv[ARG_BUFF]); if (nFrames < 1) { printf("ERROR: buffer size must be at least 1\n"); return 1; } /* initialize limit */ limit = atoi(argv[ARG_LIMIT]); if (limit<1) { printf("ERROR: size limit must be positive.\n"); return 1; } /* initialize N */ N = atoi(argv[ARG_N]); if (N<1) { printf("ERROR: N must be at least one.\n"); return 1; } /* initialize dBval or ampfac */ if (isamp) { ampfac = atof(argv[ARG_AMP]); if (ampfac <= 0.0) { printf("ERROR: ampfac must be positive.\n"); return 1; } if (ampfac == 1.0) { printf("ERROR: an ampfac of 1 creates an outfile " " identicle to the infile\n"); return 1; } } else { dbval = atof(argv[ARG_DB]); if (dbval > 0.0) { printf("ERROR: dBval cannot be positive.\n"); return 1; } if (dbval==0.0) { printf("ERROR: dBval of 0 creates an outfile " "identicle to the infile\n"); return 1; } /* convert dB to amps */ ampfac = pow(10.0, dbval/20.0); } /* open infile */ ifd = psf_sndOpen(argv[ARG_INFILE], &props, 0); if (ifd<0) { printf("ERROR: unable to open infile \"%s\"\n",argv[ARG_INFILE]); return 1; } /* we now have a resource, so we use goto hereafter on hitting any error */ /* get number of frames from infile */ size = psf_sndSize(ifd); if(size<0) { printf("ERROR: unable to obtain the size of \"%s\"\n",argv[ARG_INFILE]); error++; goto exit; } /* check if copy limit is less than size */ if(size<limit) { printf("ERROR: infile size is less than the copy limit.\n" "infile:\t%s\n" "infile size:\t%d frames\n" "copy limit:\t%d frames\n", argv[ARG_INFILE], size, limit); error++; goto exit; } /* check if infile uses 8-bit samples*/ if (props.samptype==PSF_SAMP_8) { printf("ERROR: sfgain does not support 8-bit format.\n"); error++; goto exit; } /* display infile properties */ if(!psf_sndInfileProperties(argv[ARG_INFILE],ifd,&props)) { error++; goto exit; } /* check if outfile extension is one we know about */ outformat = psf_getFormatExt(argv[ARG_OUTFILE]); if (outformat == PSF_FMT_UNKNOWN) { printf("Outfile name \"%s\" has unknown format.\n" "Use any of .wav .aiff .aif .afc .aifc\n", argv[ARG_OUTFILE]); error++; goto exit; } props.format = outformat; /* create outfile */ ofd = psf_sndCreate(argv[ARG_OUTFILE], &props, 0, 0, PSF_CREATE_RDWR); if (ofd<0) { printf("ERROR: unable to create outfile \"%s\"\n",argv[ARG_OUTFILE]); error++; goto exit; } /* allocate space for sample frames */ if (limit<nFrames) nFrames = (DWORD)limit; buffer= (float*)malloc(props.chans*sizeof(float)*nFrames); if (buffer==NULL) { puts("No memory!\n"); error++; goto exit; } /* and allocate space for PEAK info */ peaks = (PSF_CHPEAK*)malloc(props.chans*sizeof(PSF_CHPEAK)); if (peaks==NULL) { puts("No memory!\n"); error++; goto exit; } printf("copying...\n"); int update=0; int loop; int i; int j; /* copy the infile N times */ for (loop=0; loop<N; loop++) { /* make sure to set nFrames to the correct value every time you pass through the for loop */ if (limit < atoi(argv[ARG_BUFF])) nFrames = (DWORD)limit; else nFrames = (DWORD)atoi(argv[ARG_BUFF]); totalread = 0; /* running count of sample frames */ if(psf_sndSeek(ifd,0,PSF_SEEK_SET)) { printf("ERROR: cannot reset infile\n"); error++; goto exit; } /* nFrames loop to do copy, report any errors */ framesread = psf_sndReadFloatFrames(ifd,buffer,nFrames); while (framesread>0&&totalread<limit) { update++; /* update copy status after refreshing the buffer every 100 times */ if (update%100==0) printf("%ld samples copied... %ld%%\r",totalread,100*totalread/size); totalread+=framesread; /* change sample values by amp factor */ for (j=0; j<nFrames; j++) for (i=0; i<props.chans; i++) buffer[props.chans*j+i] *= ampfac; if(psf_sndWriteFloatFrames(ofd,buffer,nFrames)<0) { printf("Error writing to outfile.\n"); error++; break; } /* make sure not to copy frames past the limit */ if (nFrames+totalread > limit) nFrames = (DWORD)(limit-totalread); framesread = psf_sndReadFloatFrames(ifd,buffer,nFrames); } } totalread *= N; /* total number of frames copied */ if(framesread<0) { printf("Error reading infile. Outfile is incomplete.\n"); error++; } else printf("Done. %ld sample frames copied to %s\n", totalread, argv[ARG_OUTFILE]); /* report PEAKS to user */ if (psf_sndReadPeaks(ofd,peaks,NULL)>0) { long i; double peaktime; double peakDB; printf("Peak information:\n"); for (i=0; i<props.chans; i++) { peaktime = (double)peaks[i].pos/props.srate; if (peaks[i].val == 0.0) peaks[i].val = 1.0e-4; peakDB = log10(peaks[i].val); printf("CH %ld:\t%.4f\t(%.4f dB) at %.4f secs\n", i+1, peaks[i].val, peakDB, peaktime); } } /* do all the cleanup */ exit: if (ifd>=0) psf_sndClose(ifd); if (ofd>=0) psf_sndClose(ofd); if (buffer) free(buffer); if (peaks) free(peaks); psf_finish(); return error; }
void osc_gen(int argc, char *argv[]) { //This function serves as an Osccilator bank (an arrary of oscillators) for additive synthesis //It produces more natural sounding waves confined in the spectrum of the availabke ranges of the sampling rate at or above the Nyquist limit //Structurally, this program is very similar to sig_gen, except that we will only be calling upon sine_wave_node and implement additive synthesis //to achieve the other waveforms. If osc_gen_num_oscs == 1 (meaning only 1 oscillator) a normal sine wave will be produced. //This function makes use of wave_generator.c/.h objects and functions /***************USAGE*************** oscgen outfile dur srate num_chans, amp, freq, wavetype, num_oscs **********************************/ if(argc < OSC_GEN_NARGS) { fprintf(stderr, "insufficient arguments.\n" "usage: oscgen outfile dur srate num_chans amp freq wavetype num_oscs\n" "where wavetype =:\n" " 0 = square\n" " 1 = triangle\n" " 2 = sawtooth up\n" " 3 = sawtooth down\n" " 4 = pulse\n" "dur = duration of outfile (seconds)\n" "srate = required sample rate of outfile\n" "amp = amplitude value or breakpoint file (0 < amp <= 1.0)\n" "freq = frequency value (freq > 0) or breakpoint file.\n" "num_oscs = number of oscillators (where 1 is normal sine wave)\n" ); exit(1); } /********* initialize all dynamic resources to default states **********/ PSF_PROPS outprops; int ofd = -1; int error = 0; PSF_CHPEAK* peaks = NULL; psf_format outformat = PSF_FMT_UNKNOWN; long nframes = NFRAMES; float* outframe = NULL; double amp,freq,dur; long outframes,nbufs = 0,num_oscs,i; //num_oscs holds number of oscillators for additive synthesis long remainder; int samptype = PSF_SAMP_16; OSCIL** oscs = NULL; //an array of oscil pointers (because additive synthesis requires more than one oscillators) double* oscamps = NULL; //for oscillator bank amplitudes and frequencies double* oscfreqs = NULL; double phase = 0.0; double ampfac,freqfac,ampadjust; //Because we cannot know the number of oscillators before hand, we need to calculate it cumulatively //Recall that adding sine waves amplitude is as simple as numerically adding the amplitude values. So 1 + 3 + 5 + 7 will yield an amp //value of 16. However, we want amp to be between -1 and 1 --> therefore total amp is 1/(1^2) + 1/(3^2) + 1/(5^2) + 1/(7^2) = 1 + 1/9 + 1/25 + 1/49 = 1.172 //General Rule : totalamp = Sum (from n = 0 to N) 1/(2n-1)^2 where N = number of oscillators FILE* fpfreq = NULL; BRKSTREAM *freqstream = NULL, *ampstream = NULL; unsigned long brkfreqSize = 0; double minval = 0.0,maxval= 0.0; FILE* fpamp = NULL; unsigned long brkampSize = 0; double amp_minval = 0.0,amp_maxval= 0.0; clock_t starttime, endtime; int wavetype; oscs = NULL; /********** error checking on wave type **********/ wavetype = atoi(argv[OSC_GEN_WAVETYPE]); if(wavetype < OSC_GEN_SQUARE || wavetype > OSC_GEN_PULSE) { fprintf(stderr, "Error: wavetype not defined\n"); goto exit; } /********** error checking on number of oscillators **********/ num_oscs = atoi(argv[OSC_GEN_NUM_OSCS]); if(num_oscs <= 0) { fprintf(stderr, "Error: number of oscillators must be positive\n"); goto exit; } /********** define outfile format (with embedded error checking) **********/ outprops.srate = atoi(argv[OSC_GEN_SRATE]); if(outprops.srate <= 0) { fprintf(stderr, "Error: srate must be positive\n"); goto exit; } outprops.chans = atoi(argv[OSC_GEN_NUM_CHANS]); if(outprops.chans <= 0) { fprintf(stderr, "Error: number of channels must be positive\n"); goto exit; } outprops.samptype = (psf_stype) samptype; outprops.chformat = STDWAVE; dur = atof(argv[OSC_GEN_DUR]); if(dur <= 0.0) { fprintf(stderr, "Error: duration must be positive (>=0)\n"); goto exit; } outframes = (unsigned long) (dur * outprops.srate + 0.5); nbufs = outframes / nframes; remainder = outframes - nbufs * nframes; if(remainder > 0) nbufs++; /********** open breakpoint files, or set constants if no breakpoint file is defined **********/ /***** amp (amplitude) breakpoints file *****/ fpamp = fopen(argv[OSC_GEN_AMP], "r"); //try opening specified argument if(fpamp == NULL) { //meaning that either argument OSC_GEN_AMP is not a breakpoint file or error opening amp = atof(argv[OSC_GEN_AMP]); if(amp <= 0.0 || amp > 1) { fprintf(stderr, "Error: amplitude must be between 0 and 1 (0 <= amp <= 1)\n"); goto exit; } } else { //meaning that argument OSC_GEN_AMP is indeed a breakpoint file ampstream = bps_newstream(fpamp,outprops.srate,&brkampSize); if(ampstream == NULL) { fprintf(stderr, "Error reading amplitude breakpoint file: %s\n", argv[OSC_GEN_AMP]); goto exit; } if(bps_getminmax(ampstream, &minval, &maxval)) { //meaning we called upon said function that will put into minval and maxval appropriate values //if said function returns 1 --> we have error fprintf(stderr, "Error reading range breakpoint file: %s\n", argv[OSC_GEN_AMP]); goto exit; } if(minval < 0.0 || minval > 1.0 || maxval < 0.0 || maxval > 1.0) { fprintf(stderr, "Error: amplitude values out of range in breakpoint file\n"); goto exit; } } /***** freq (frequency) breakpoints file *****/ fpfreq = fopen(argv[OSC_GEN_FREQ], "r"); if(fpfreq == NULL) { //meaning that either argument OSC_GEN_FREQ is not a breakpoint file or error opening freq = atof(argv[OSC_GEN_FREQ]); if(freq <= 0.0) { fprintf(stderr, "Error: frequency must be positive\n"); goto exit; } } else { //meaning that argument OSC_GEN_FREQ is indeed a breakpoint file freqstream = bps_newstream(fpfreq, outprops.srate, &brkfreqSize); if(freqstream == NULL) { fprintf(stderr, "Error reading frequncy from breakpoint file: %s\n", argv[OSC_GEN_FREQ]); goto exit; } if(bps_getminmax(freqstream, &minval, &maxval)) { fprintf(stderr, "Error reading range from breakpoint file: %s\n", argv[OSC_GEN_FREQ]); goto exit; } if(minval <= 0.0) { fprintf(stderr, "Error in breakpoing file; Frequency values must be positive\n"); goto exit; } } /********** Initialize portsf **********/ if(psf_init()) { fprintf(stderr, "Error: Unable to initialize portsf\n"); goto exit; } /********** Sample Frame Buffer Memory Aloocation **********/ outframe = (float*)malloc(nframes * outprops.chans * sizeof(float)); if(outframe == NULL) { fprintf(stderr, "Error: No memory for outframe buffer\n"); goto exit; } /********** Check for correct output extension **********/ outformat = psf_getFormatExt(argv[OSC_GEN_OUTFILE]); if(outformat == PSF_FMT_UNKNOWN) { fprintf(stderr, "Error: invalid extension\n" "Possible extensions:\t *.wav\t *.aiff\t *.aif\t *.afc\t *.aifc\n"); goto exit; } //reach here meaning outformat is viable extension outprops.format = outformat; /********** create oscillator amp and oscillator freq arrays **********/ oscamps = (double*)malloc(num_oscs * sizeof(double)); oscfreqs = (double*)malloc(num_oscs * sizeof(double)); if(!oscamps || !oscfreqs) { fprintf(stderr, "Error: Failed memory allocation for oscillator amp/freq arrays\n"); goto exit; } /********** create amp/freq data for requested waveshape, normalized to 1 **********/ ampfac = 1.0; freqfac = 1.0; ampadjust = 0.0; switch(wavetype) { case(OSC_GEN_SQUARE): for(i=0; i<num_oscs; i++) { oscamps[i] = ampfac; oscfreqs[i] = freqfac; freqfac += 2.0; ampadjust += ampfac; ampfac = 1.0 / freqfac; } //normalize for(i=0; i<num_oscs; i++) oscamps[i] /= ampadjust; break; case(OSC_GEN_TRIANGLE): for(i=0; i<num_oscs; i++) { oscamps[i] = ampfac; oscfreqs[i] = freqfac; freqfac += 2.0; ampadjust += ampfac; ampfac = 1.0 / (freqfac * freqfac); } //normalize for(i=0; i<num_oscs; i++) oscamps[i] /= ampadjust; phase = 0.25; break; case(OSC_GEN_SAW_UP): case(OSC_GEN_SAW_DOWN): for(i=0; i<num_oscs; i++) { oscamps[i] = ampfac; oscfreqs[i] = freqfac; freqfac += 1.0; ampadjust += ampfac; ampfac = 1.0 / freqfac; } if(wavetype == OSC_GEN_SAW_UP) ampadjust = -ampadjust; //normalize for(i=0; i<num_oscs; i++) oscamps[i] /= ampadjust; break; case(OSC_GEN_PULSE): freqfac = 1.0; ampfac = 1.0 / num_oscs; ampadjust = 0.0; for(i=0; i<num_oscs; i++) { oscamps[i] = ampfac; oscfreqs[i] = freqfac; freqfac += 1.0; } break; } /********** Create Oscillator Bank **********/ oscs = (OSCIL**)malloc(num_oscs * sizeof(OSCIL*)); if(!oscs) { fprintf(stderr, "Error: Failed memory allocation on creation of oscillator bank\n"); goto exit; } for(i=0; i< num_oscs; i++) { oscs[i] = new_oscil_with_phase(outprops.srate, phase); if(!oscs[i]) { fprintf(stderr, "Error: Failed memory allocation on creation of oscillator object\n"); goto exit; } } /********** Initialize Peak nfo **********/ peaks = (PSF_CHPEAK*)malloc(outprops.chans * sizeof(PSF_CHPEAK)); if(peaks == NULL) { fprintf(stderr, "Error: No memory for peaks\n"); goto exit; } /********** Create and Handle outfile **********/ ofd = psf_sndCreate(argv[OSC_GEN_OUTFILE],&outprops, 0, 0, PSF_CREATE_RDWR); //refer to sf2float for documentation if(ofd < 0) { fprintf(stderr, "Error: Unable to create outfile\n"); goto exit; } /********** Audio Processing **********/ printf("Reached here\t\t numbufs: %lu; numframes: %lu\n", nbufs, nframes); starttime = clock(); for(i=0; i<nbufs; i++) { long j,k,l; double val; if(i == nbufs -1) nframes = remainder; for(j=0; j< nframes; j++) { if(freqstream) freq = bps_tick(freqstream); if(ampstream) amp = bps_tick(freqstream); val = 0.0; for(l=0; l<num_oscs; l++) { val += oscamps[l] * sine_wave_node(oscs[l], freq * oscfreqs[l]); } for(k=0; k<outprops.chans; k++) { outframe[j*outprops.chans +k] = (float)(val * amp); } } //end j for-loop if(psf_sndWriteFloatFrames(ofd, outframe, nframes) != nframes) { fprintf(stderr, "Error writing to outfile"); break; } }//end i for-loop endtime = clock(); exit: fprintf(stdout, "Task Finished \nElapsed Time: %f seconds\n", (endtime - starttime)/(double)CLOCKS_PER_SEC); /***** Report PEAK values to user *****/ if(psf_sndReadPeaks(ofd,peaks,NULL) > 0){ long i; double peaktime; printf("PEAK information:\n"); for(i=0;i < outprops.chans;i++){ peaktime = (double) peaks[i].pos / (double) outprops.srate; printf("CH %d:\t%.4f at %.4f secs\n", (int)(i+1), peaks[i].val, peaktime); } } if(ofd >= 0) psf_sndClose(ofd); if(outframe) free(outframe); if(peaks) free(peaks); if(oscs){ for(i=0;i < num_oscs;i++){ free(oscs[i]); } free(oscs); } if(oscamps) free(oscamps); if(oscfreqs) free(oscfreqs); psf_finish(); exit(1); }
void sig_gen(int argc, char *argv[]) { //This function will generate simple waveforms --> Sine, Triangle, Square, Sawtooth up, Sawtooth down //Showcases Pseudo-Object Oriented Programming, Function Pointers (functoids) /***************USAGE*************** siggen outfile wavetype dur srate amp freq **********************************/ if(argc < SIG_GEN_NARGS) { fprintf(stderr, "insufficient arguments.\n" "usage: siggen outfile wavetype dur srate amp freq\n" "where wavetype =:\n" " 0 = sine\n" " 1 = triangle\n" " 2 = square\n" " 3 = sawtooth up\n" " 4 = sawtooth down\n" "dur = duration of outfile (seconds)\n" "srate = required sample rate of outfile\n" "amp = amplitude value or breakpoint file (0 < amp <= 1.0)\n" "freq = frequency value (freq > 0) or breakpoint file.\n" ); exit(1); } /********* initialize all dynamic resources to default states **********/ PSF_PROPS outprops; int ofd = -1; PSF_CHPEAK* peaks = NULL; psf_format outformat = PSF_FMT_UNKNOWN; //all psf* defined in portsf sound library long nframes = NFRAMES; float* outframe = NULL; double amp,freq,dur; unsigned long outframes,nbufs,i; long remainder; int wavetype; OSCIL* osc = NULL; //OSCIL pseudo-object defined in wave_generator.h node_func_ptr node; //function pointer defined in wave_generator.h FILE* fpfreq = NULL; FILE* fpamp = NULL; BRKSTREAM* freqstream = NULL; //BRKSTREAM* defined in breakpoint.h BRKSTREAM* ampstream = NULL; unsigned long brkfreqSize = 0, brkampSize = 0; double minval, maxval; /********** error checking on wave type **********/ wavetype = atoi(argv[SIG_GEN_TYPE]); if(wavetype < SINE_WAVE || wavetype >= NUM_WAVE_TYPES) { fprintf(stderr, "Error: wavetype not defined\n"); goto exit; } /********** assigning function pointer accordingly **********/ switch(wavetype) { case(SINE_WAVE): node = sine_wave_node; //node is a function pointer (type: node_func_ptr); sine_wave_node is function break; case(SQAURE_WAVE): node = square_wave_node; break; case(TRIANGLE_WAVE): node = triangle_wave_node; break; case(SAW_UP_WAVE): node = sawtooth_up_wave_node; break; case(SAW_DOWN_WAVE): node = sawtooth_down_wave_node; break; } /********** define outfile format (with embedded error checking) **********/ outprops.srate = atoi(argv[SIG_GEN_SRATE]); if(outprops.srate <= 0) { fprintf(stderr, "Error: srate must by positive (>=0)\n"); goto exit; } outprops.chans = 1; //as of this sig_gen implementation --> mono outprops.samptype = (psf_stype) PSF_SAMP_16; //16-bit samples outprops.chformat = STDWAVE; //STDWAVE defined in portsf library dur = atof(argv[SIG_GEN_DUR]); if(dur <= 0.0) { fprintf(stderr, "Error: duration must be positive (>=0)\n"); goto exit; } outframes = (unsigned long) (dur * outprops.srate + 0.5); nbufs = outframes / nframes; remainder = outframes - nbufs * nframes; if(remainder) nbufs++; /********** open breakpoint files, or set constants if no breakpoint file is defined **********/ /***** amp (amplitude) breakpoints file *****/ fpamp = fopen(argv[SIG_GEN_AMP], "r"); //try opening specified argument if(fpamp == NULL) { //meaning that either argument SIG_GEN_AMP is not a breakpoint file or error opening amp = atof(argv[SIG_GEN_AMP]); if(amp <= 0.0 || amp > 1) { fprintf(stderr, "Error: amplitude must be between 0 and 1 (0 <= amp <= 1)\n"); goto exit; } } else { //meaning that argument SIG_GEN_AMP is indeed a breakpoint file ampstream = bps_newstream(fpamp,outprops.srate,&brkampSize); if(ampstream == NULL) { fprintf(stderr, "Error reading amplitude breakpoint file: %s\n", argv[SIG_GEN_AMP]); goto exit; } if(bps_getminmax(ampstream, &minval, &maxval)) { //meaning we called upon said function that will put into minval and maxval appropriate values //if said function returns 1 --> we have error fprintf(stderr, "Error reading range breakpoint file: %s\n", argv[SIG_GEN_AMP]); goto exit; } if(minval < 0.0 || minval > 1.0 || maxval < 0.0 || maxval > 1.0) { fprintf(stderr, "Error: amplitude values out of range in breakpoint file\n"); goto exit; } } /***** freq (frequency) breakpoints file *****/ fpfreq = fopen(argv[SIG_GEN_FREQ], "r"); if(fpfreq == NULL) { //meaning that either argument SIG_GEN_FREQ is not a breakpoint file or error opening freq = atof(argv[SIG_GEN_FREQ]); if(freq <= 0.0) { fprintf(stderr, "Error: frequency must be positive\n"); goto exit; } } else { //meaning that argument SIG_GEN_FREQ is indeed a breakpoint file freqstream = bps_newstream(fpfreq, outprops.srate, &brkfreqSize); if(freqstream == NULL) { fprintf(stderr, "Error reading frequncy from breakpoint file: %s\n", argv[SIG_GEN_FREQ]); goto exit; } if(bps_getminmax(freqstream, &minval, &maxval)) { fprintf(stderr, "Error reading range from breakpoint file: %s\n", argv[SIG_GEN_FREQ]); goto exit; } if(minval <= 0.0) { fprintf(stderr, "Error in breakpoing file; Frequency values must be positive\n"); goto exit; } } /********** Initialize OSCIL pseudo-object **********/ osc = new_oscil(outprops.srate); if(osc == NULL) { fprintf(stderr, "No memory for oscillator\n"); goto exit; } /********** Initialize portsf **********/ if(psf_init()) { fprintf(stderr, "Error: Unable to initialize portsf\n"); goto exit; } /********** Sample Frame Buffer Memory Aloocation **********/ outframe = (float*)malloc(nframes * outprops.chans * sizeof(float)); if(outframe == NULL) { fprintf(stderr, "Error: No memory for outframe buffer\n"); goto exit; } /********** Check for correct output extension **********/ outformat = psf_getFormatExt(argv[SIG_GEN_OUTFILE]); if(outformat == PSF_FMT_UNKNOWN) { fprintf(stderr, "Error: invalid extension\n" "Possible extensions:\t *.wav\t *.aiff\t *.aif\t *.afc\t *.aifc\n"); goto exit; } //reach here meaning outformat is viable extension outprops.format = outformat; /********** Initialize Peak nfo **********/ peaks = (PSF_CHPEAK*)malloc(outprops.chans * sizeof(PSF_CHPEAK)); if(peaks == NULL) { fprintf(stderr, "Error: No memory for peaks\n"); goto exit; } /********** Create and Handle outfile **********/ ofd = psf_sndCreate(argv[SIG_GEN_OUTFILE],&outprops, 0, 0, PSF_CREATE_RDWR); //refer to sf2float for documentation if(ofd < 0) { fprintf(stderr, "Error: Unable to create outfile\n"); goto exit; } /********** Audio Processing **********/ for(i=0; i<nbufs; i++) { long j; if(i == nbufs-1) nframes = remainder; for(j=0; j<nframes; j++) { if(freqstream) freq = bps_tick(freqstream); if(ampstream) amp = bps_tick(ampstream); outframe[j] = (float)(amp * node(osc,freq)); //call upon function via function pointer with OSCIL pseudo-object and freq; } if(psf_sndWriteFloatFrames(ofd, outframe, nframes) != nframes) { fprintf(stderr, "Error: unable to write to outfile\n"); goto exit; } } /********** Clean_up section **********/ exit: fprintf(stderr, "Task Finished\n"); /***** Report PEAK values to user *****/ if(psf_sndReadPeaks(ofd,peaks,NULL) > 0){ long i; double peaktime; printf("PEAK information:\n"); for(i=0;i < outprops.chans;i++){ peaktime = (double) peaks[i].pos / (double) outprops.srate; printf("CH %d:\t%.4f at %.4f secs\n", (int)(i+1), peaks[i].val, peaktime); } } if(ofd >= 0) if(psf_sndClose(ofd)) printf("Error closing outfile %s\n",argv[SIG_GEN_OUTFILE]); if(outframe) free(outframe); if(peaks) free(peaks); /*TODO: cleanup any other resources */ if(osc) free(osc); if(fpfreq) if(fclose(fpfreq)) printf("Error closing breakpoint file %s\n",argv[SIG_GEN_FREQ]); if(fpamp) if(fclose(fpamp)) printf("Error closing breakpoint file %s\n",argv[SIG_GEN_AMP]); if(freqstream){ bps_freepoints(freqstream); free(freqstream); } if(ampstream){ bps_freepoints(ampstream); free(ampstream); } psf_finish(); exit(1); }
void sf2float(int argc, char *argv[]) { //This function will sound portsf to convert a soundfile to float format /*=========================USAGE========================= sf2float infile outfile ======================================================*/ if(argc!=NUMARGS) { fprintf(stderr, "USAGE: sf2float infile outfile\n"); exit(1); } PSF_PROPS props; //structure containing sample rate, channels, sample type, format, channel format... long framesread,totalread; int infd =-1 , outfd = -1; psf_format outformat = PSF_FMT_UNKNOWN; //initially make the outformat unknown until properly defined by create function PSF_CHPEAK* peaks = NULL; //in waveform and aiff, peaks holds the channel peak values accordingly float* frame = NULL; //a frame is a set of n datatypes (16-bit --> short; 24 bit --> long; 32 bit --> float) where n is determined by number of channels //initialize portsf int psfd = psf_init(); if(psfd<0) { fprintf(stderr, "Unable to initialize portsf\n"); exit(1); } //function to open exsiting soudfile protoype--> psf_sndOpen(const char *path, PSF_PROPS *props, int rescale) //PSF_PROPS <-- refer to note above on definition line of props //rescale --> normally set to 0 for samples to be read unaltered; otherwise, rescale normalizes sound between -1 and 1 to preven clipping infd = psf_sndOpen(argv[IN_FILE], &props, 0); if(infd<0) { fprintf(stderr, "Unable to open soundfile\n"); goto exit; //Works like a JAL command; exit block must be within same function block; used for cleanup purposes. ALL SUBSEQUENT ERROR HANDLING MUST USE GOTO } //tell user if in_file is already floats (i.e 32-bit) if(props.samptype == PSF_SAMP_IEEE_FLOAT) { fprintf(stderr, "Infile already of type float\t Nothing to do...\n"); goto exit; } else props.samptype = PSF_SAMP_IEEE_FLOAT; //if not already of type float --> then set the samptype parameter of props strcuture to type float //Check to see that outfile extension is applicable --> noncompressed, lossless --> wav, aiff, aif, afc, aifc outformat = psf_getFormatExt(argv[OUTFILE]); if(outformat==PSF_FMT_UNKNOWN) { fprintf(stderr, "Improper outfile format\nAccepted Formats: .wav, .aiff, .aif, .afc, .aifc\n"); goto exit; } else props.format=outformat; //if format applicable --> set the format paramter of props structure to the outformat as discovered by portfs on outfile extension provided //Opening a sound file for writing prototype --> psf_sndCreate(const char *path, const PSF_PROPS *props, int clip_floats, int minheader, int mode) //clip_floats --> set the way the floats are written <-- used in conjunction with rescale value being nonzero in psf_sndOpen //minheader --> normally set to 0; set to 1 for legacy compatibility //mode --> control of read-write access to the file {PSF_CREATE_RDWR, PSF_CREATE_TEMPORARY, PSF_CREATE_WRONLY} outfd = psf_sndCreate(argv[OUTFILE], &props, 0, 0, PSF_CREATE_RDWR); if(outfd<0) { fprintf(stderr, "Unable to create soundfile\n"); goto exit; } //Allocate space for one sample frame frame = (float*)malloc(props.chans * sizeof(float)); if(frame==NULL) goto no_memory; //Allocate space for PEAK info peaks = (PSF_CHPEAK*)malloc(props.chans * sizeof(PSF_CHPEAK)); if(peaks==NULL) goto no_memory; //single-frame loop --> copying as floats (conversion process) framesread = psf_sndReadFloatFrames(infd, frame, 1); totalread = 0; while(framesread==1) { totalread++; if(psf_sndWriteFloatFrames(outfd,frame,1) != 1) { fprintf(stderr, "Error writing to file\n"); break; } /* ===============DO ANY PROCESSING HERE; CORE OF AUDIO PROCESSING===============*/ framesread = psf_sndReadFloatFrames(infd,frame,1); } if(framesread<0) { fprintf(stderr, "Error handling infile; outfile is incomplete\n"); goto exit; } else fprintf(stdout, "Task Finished %d sample frames copied to %s\n", (int)totalread, argv[OUTFILE]); //report PEAK values to user if(psf_sndReadPeaks(outfd, peaks, NULL) >0) { long i; double peakTime; fprintf(stdout, "PEAK Information:\n"); for(i=0; i<props.chans; i++) { peakTime = (double) peaks[i].pos / props.srate; fprintf(stdout, "CH %d:\t%.4f at %.4f secs\n", (int)(i+1), peaks[i].val, peakTime); } } exit: if(infd>=0) psf_sndClose(infd); if(outfd>=0) psf_sndClose(outfd); if(frame) free(frame); if(peaks) free(peaks); psf_finish(); // close portfs fprintf(stderr, "Program Terminated\n"); exit(1); no_memory: fprintf(stderr, "No Memory!\n"); goto exit; }