Mercurial > hg > aim92
diff xaim/synthirn.c @ 0:5242703e91d3 tip
Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author | tomwalters |
---|---|
date | Fri, 20 May 2011 15:19:45 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xaim/synthirn.c Fri May 20 15:19:45 2011 +0100 @@ -0,0 +1,319 @@ +/* + Copyright (c) Applied Psychology Unit, Medical Research Council. 1994 + =========================================================================== + + Permission to use, copy, modify, and distribute this software without fee + is hereby granted for research purposes, provided that this copyright + notice appears in all copies and in all supporting documentation, and that + the software is not redistributed for any fee (except for a nominal + shipping charge). Anyone wanting to incorporate all or part of this + software in a commercial product must obtain a license from the Medical + Research Council. + + The MRC makes no representations about the suitability of this + software for any purpose. It is provided "as is" without express or + implied warranty. + + THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING + ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL + THE A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES + OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + SOFTWARE. +*/ + +/* synthirn.c +* ----------- +* +* Makes iterated ripple noise. +*/ + +#include <stdio.h> +#include <string.h> +#include <math.h> + +#define ON 1 +#define OFF 0 +#define MAXSAMPLES 882000 /* 10 seconds, cd rates */ +#define SAME 4 +#define ORIGINAL 5 +#define SAMPLERATE 20000 +#define DURATION 1000 /* ms */ +#define DELAY 8 +#define GAIN 1.0 +#define ITERATION 4 + + +short input[MAXSAMPLES]; +float y[MAXSAMPLES]; +float temp[MAXSAMPLES]; +short output[MAXSAMPLES]; +short sample[2]; + + +/*----------------------------------------------------------------------*/ +/*----------------------------------------------------------------------*/ + + + +main(int argc, char *argv[]) +{ + long samplerate = SAMPLERATE; + long length=DURATION; + long datatype = 0; + int counter = 0; + int msec = 0; + + int max_iterations = ITERATION; + float delay = DELAY; + long delay_samples; + long arraydelay; + double gain =GAIN; + double den_gain=GAIN; + + char inputfn[255]; + char outputfn[256]; + FILE *inputfp = NULL; + FILE *outputfp = NULL; + + int helpflag = OFF; + int inputflag = OFF; + int outputflag = OFF; + int verboseflag = OFF; + int x=1; + int divideflag = OFF; + float dividevalue = 1.0; + int typeflag = SAME; + long outputlength = DURATION; + long outputlength_samples = 0; + + +/* outputfn=(char *)calloc(255, sizeof(char )); + outputfn="IRN-Default-File"; */ + + /* Parse command line options. */ + + if (argc == 1) + helpflag = ON; + + while (x < argc) { + if (!strcmp(argv[x], "-sample")) { samplerate = atol(argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-s")) { samplerate = atol(argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-Time")) { outputlength = atol(argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-T")) { outputlength = atol(argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-help")) { helpflag = ON; x+=1;} + else if (!strcmp(argv[x], "-h")) { helpflag = ON; x+=1;} + else if (!strcmp(argv[x], "-verbose")) { verboseflag = ON; x+=1;} + else if (!strcmp(argv[x], "-v")) { verboseflag = ON; x+=1;} + else if (!strcmp(argv[x], "-same")) { typeflag = SAME; x+=1;} + else if (!strcmp(argv[x], "-S")) { typeflag = SAME; x+=1;} + else if (!strcmp(argv[x], "-original")) { typeflag = ORIGINAL; x+=1;} + else if (!strcmp(argv[x], "-O")) { typeflag = ORIGINAL; x+=1;} + else if (!strcmp(argv[x], "-i")) { max_iterations = atoi(argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-d")) { delay = atof(argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-l")) { delay = atof(argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-divide")) { divideflag = ON; dividevalue = atof(argv[x+1]);x+=2;} + else if (!strcmp(argv[x], "-div")) { divideflag = ON; dividevalue = atof(argv[x+1]);x+=2;} + else if (!strcmp(argv[x], "-gain")) { gain = atof(argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-g")) { gain = atof(argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-input")) { inputflag = ON; strcpy(inputfn, argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-in")) { inputflag = ON; strcpy(inputfn, argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-output")) { outputflag=ON; strcpy(outputfn, argv[x+1]); x+=2;} + else if (!strcmp(argv[x], "-out")) { outputflag=ON; strcpy(outputfn, argv[x+1]); x+=2;} + else {fprintf(stderr, "synthirn: unknown option %s\n", argv[x]); + exit(-1);} + } + + + if (helpflag == ON) { + fprintf(stderr, "\n------------- synthirn --------------\n"); + fprintf(stderr, "Makes an iterated ripple noise. Up to 10 seconds long. \n"); + fprintf(stderr, "NB: sample rate < 32767.\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "options: \n"); + fprintf(stderr, "-s <long> sampling rate :Default 20000\n"); + fprintf(stderr, "-d <float> delay (ms) :Default 8ms\n"); + fprintf(stderr, "-g <float> gain (>=-1.0, <=1.0) :Default 1.0 \n"); + fprintf(stderr, "-i <int> number of iterations :Default 4\n"); + fprintf(stderr, "-T <long> length of output (ms) :Default 1000ms \n"); +/* fprintf(stderr, "-div <float> divide input by <float> \n"); */ + fprintf(stderr, "\n"); + fprintf(stderr, "-S Make 'Add-Same' IRN :Default\n"); + fprintf(stderr, "-O Make 'Add-Original' IRN \n"); + + fprintf(stderr, "-in <filename> input noise \n"); + fprintf(stderr, "-out <filename> output-filename \n"); + fprintf(stderr, "-v verbose information\n"); + fprintf(stderr, "\n"); + exit(1); + } + + if (samplerate == 0) { + fprintf(stderr, "synthirn: unspecified sampling rate. \n"); + exit(-1); + } + +/* INPUT --------------------------------------*/ + + /* Attempt to load input file. */ + if (inputflag == OFF ) { + fprintf(stderr, "synthirn: no input file specified. \n"); + exit(-1);} + if (outputflag == OFF ) { + fprintf(stderr, "synthirn: no output file specified.\n"); + exit(-1);} + + inputfp = fopen(inputfn, "rb"); + if (inputfp == NULL) { + fprintf(stderr, "synthirn: unable to open file %s.\n", inputfn); + exit(-1); } + + /* Clear the arrays */ + if (verboseflag == ON){ + fprintf(stderr, "clearing ... "); + fflush(stderr);} + + for (x=0; x<MAXSAMPLES; x++){ + input[x] = 0; + } + + /* load input */ + if (verboseflag == ON){ + fprintf(stderr, "loading ... "); + fflush(stderr);} + x=0; + while( feof(inputfp) ==0) { + fread(sample, 2, 1, inputfp); + input[x++] = (int) sample[0];} + length = x - 1; + + /* close input */ + fclose(inputfp); + + + +/*----------------------------------------------*/ + + /* convert into samples */ + delay_samples = (long) (delay * samplerate) / 1000; + outputlength_samples = (long) (outputlength * samplerate) / 1000; + + if (verboseflag == ON){ + fprintf(stderr, "floats ... "); + fflush(stderr);} + + for (x=0; x<length; x++) { + if (divideflag == ON) + y[x] = (float) ((float) input[x] )/ dividevalue; + else + y[x] = (float) input[x];} + + + /* Do the iterations .... */ + + if (verboseflag == ON){ + fprintf(stderr, "Gain is %f\n", gain); + den_gain=fabs(gain); + fprintf(stderr, "Den Gain is %f\n", den_gain); + fprintf(stderr, "n: "); + fflush(stderr);} + + for (counter = 1; counter <= max_iterations; counter ++) { + if (verboseflag == ON){ + fprintf(stderr, "%i ", counter); + fflush(stderr);} + + /*---------------------------------*/ + + if (typeflag == SAME) { + arraydelay = delay_samples; + + for (x = 0; x < length; x++) + temp[x] = (float) y[x]; + + for (x = 0; x < arraydelay; x++) + y[x] = (float) temp[x] + (gain * temp[length - arraydelay + x]) ; + + for (x = arraydelay; x < length; x++) + y[x] = (float) temp[x] + (gain * temp[x - arraydelay]) ; + + for (x = 0; x < length; x++) + y[x] = (float) y[x] / (1.0 + den_gain);} + + /*---------------------------------*/ + + else if (typeflag == ORIGINAL) { + arraydelay = delay_samples; + + for (x = 0; x < length; x++) + temp[x] = (float) y[x]; + + for (x = 0; x < arraydelay; x++) + if (divideflag == ON) + y[x] = (float) ((float) input[x] )/ dividevalue + (den_gain * temp[length - arraydelay + x]) ; + else + y[x] = (float) input[x] + (gain * temp[length - arraydelay + x]); /* div by pow */ + + for (x = arraydelay; x < length; x++) + if (divideflag == ON) + y[x] = (float) ((float) input[x] )/ dividevalue + (den_gain * temp[x - arraydelay]) ; + else + y[x] = (float) input[x] + (gain * temp[x - arraydelay]); + + /* for (x=0; x < length; x++) + y[x] = (float) y[x] / (1. + den_gain*temp[x]) ; */ } + + /* Not used; makes noisy IRN's . Wrong divisor */ + + /*---------------------------------*/ + + else { + fprintf(stderr, "synthirn: must specify either 'Same' or 'Original' noise.\n"); + exit(-1);} + } + + if (verboseflag == ON){ + fprintf(stderr, "normalising ... "); + fflush(stderr);} + + /* Normalise */ + if (typeflag == ORIGINAL) + for (x=0; x<length; x++) + /* output[x] = (short) y[x]; */ + output[x] = (short) (y[x] /((max_iterations*den_gain)+1)); + else if (typeflag == SAME) + for (x=0; x<length; x++) + output[x] = (short) y[x]; + + /* reset output positions */ + arraydelay = max_iterations * delay_samples; + +/* OUTPUT ------------------------------------- */ + + + outputfp = fopen(outputfn, "wb"); + if (outputfn == NULL){ + fprintf(stderr, "synthirn: unable to open output file %s.\n", outputfn); + exit(-1); } + + /* write data */ + if (verboseflag == ON){ + fprintf(stderr, "saving "); + fflush(stderr);} + + fwrite(output, 2, outputlength_samples, outputfp); + + fclose(outputfp); + + if (verboseflag == ON){ + fprintf(stderr, "\n"); + fflush(stderr);} +} + + +/* The End */ +/*-----------------------------------------------------------------------*/ + + +