comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:5242703e91d3
1 /*
2 Copyright (c) Applied Psychology Unit, Medical Research Council. 1994
3 ===========================================================================
4
5 Permission to use, copy, modify, and distribute this software without fee
6 is hereby granted for research purposes, provided that this copyright
7 notice appears in all copies and in all supporting documentation, and that
8 the software is not redistributed for any fee (except for a nominal
9 shipping charge). Anyone wanting to incorporate all or part of this
10 software in a commercial product must obtain a license from the Medical
11 Research Council.
12
13 The MRC makes no representations about the suitability of this
14 software for any purpose. It is provided "as is" without express or
15 implied warranty.
16
17 THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
18 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
19 THE A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
20 OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 SOFTWARE.
24 */
25
26 /* synthirn.c
27 * -----------
28 *
29 * Makes iterated ripple noise.
30 */
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <math.h>
35
36 #define ON 1
37 #define OFF 0
38 #define MAXSAMPLES 882000 /* 10 seconds, cd rates */
39 #define SAME 4
40 #define ORIGINAL 5
41 #define SAMPLERATE 20000
42 #define DURATION 1000 /* ms */
43 #define DELAY 8
44 #define GAIN 1.0
45 #define ITERATION 4
46
47
48 short input[MAXSAMPLES];
49 float y[MAXSAMPLES];
50 float temp[MAXSAMPLES];
51 short output[MAXSAMPLES];
52 short sample[2];
53
54
55 /*----------------------------------------------------------------------*/
56 /*----------------------------------------------------------------------*/
57
58
59
60 main(int argc, char *argv[])
61 {
62 long samplerate = SAMPLERATE;
63 long length=DURATION;
64 long datatype = 0;
65 int counter = 0;
66 int msec = 0;
67
68 int max_iterations = ITERATION;
69 float delay = DELAY;
70 long delay_samples;
71 long arraydelay;
72 double gain =GAIN;
73 double den_gain=GAIN;
74
75 char inputfn[255];
76 char outputfn[256];
77 FILE *inputfp = NULL;
78 FILE *outputfp = NULL;
79
80 int helpflag = OFF;
81 int inputflag = OFF;
82 int outputflag = OFF;
83 int verboseflag = OFF;
84 int x=1;
85 int divideflag = OFF;
86 float dividevalue = 1.0;
87 int typeflag = SAME;
88 long outputlength = DURATION;
89 long outputlength_samples = 0;
90
91
92 /* outputfn=(char *)calloc(255, sizeof(char ));
93 outputfn="IRN-Default-File"; */
94
95 /* Parse command line options. */
96
97 if (argc == 1)
98 helpflag = ON;
99
100 while (x < argc) {
101 if (!strcmp(argv[x], "-sample")) { samplerate = atol(argv[x+1]); x+=2;}
102 else if (!strcmp(argv[x], "-s")) { samplerate = atol(argv[x+1]); x+=2;}
103 else if (!strcmp(argv[x], "-Time")) { outputlength = atol(argv[x+1]); x+=2;}
104 else if (!strcmp(argv[x], "-T")) { outputlength = atol(argv[x+1]); x+=2;}
105 else if (!strcmp(argv[x], "-help")) { helpflag = ON; x+=1;}
106 else if (!strcmp(argv[x], "-h")) { helpflag = ON; x+=1;}
107 else if (!strcmp(argv[x], "-verbose")) { verboseflag = ON; x+=1;}
108 else if (!strcmp(argv[x], "-v")) { verboseflag = ON; x+=1;}
109 else if (!strcmp(argv[x], "-same")) { typeflag = SAME; x+=1;}
110 else if (!strcmp(argv[x], "-S")) { typeflag = SAME; x+=1;}
111 else if (!strcmp(argv[x], "-original")) { typeflag = ORIGINAL; x+=1;}
112 else if (!strcmp(argv[x], "-O")) { typeflag = ORIGINAL; x+=1;}
113 else if (!strcmp(argv[x], "-i")) { max_iterations = atoi(argv[x+1]); x+=2;}
114 else if (!strcmp(argv[x], "-d")) { delay = atof(argv[x+1]); x+=2;}
115 else if (!strcmp(argv[x], "-l")) { delay = atof(argv[x+1]); x+=2;}
116 else if (!strcmp(argv[x], "-divide")) { divideflag = ON; dividevalue = atof(argv[x+1]);x+=2;}
117 else if (!strcmp(argv[x], "-div")) { divideflag = ON; dividevalue = atof(argv[x+1]);x+=2;}
118 else if (!strcmp(argv[x], "-gain")) { gain = atof(argv[x+1]); x+=2;}
119 else if (!strcmp(argv[x], "-g")) { gain = atof(argv[x+1]); x+=2;}
120 else if (!strcmp(argv[x], "-input")) { inputflag = ON; strcpy(inputfn, argv[x+1]); x+=2;}
121 else if (!strcmp(argv[x], "-in")) { inputflag = ON; strcpy(inputfn, argv[x+1]); x+=2;}
122 else if (!strcmp(argv[x], "-output")) { outputflag=ON; strcpy(outputfn, argv[x+1]); x+=2;}
123 else if (!strcmp(argv[x], "-out")) { outputflag=ON; strcpy(outputfn, argv[x+1]); x+=2;}
124 else {fprintf(stderr, "synthirn: unknown option %s\n", argv[x]);
125 exit(-1);}
126 }
127
128
129 if (helpflag == ON) {
130 fprintf(stderr, "\n------------- synthirn --------------\n");
131 fprintf(stderr, "Makes an iterated ripple noise. Up to 10 seconds long. \n");
132 fprintf(stderr, "NB: sample rate < 32767.\n");
133 fprintf(stderr, "\n");
134 fprintf(stderr, "options: \n");
135 fprintf(stderr, "-s <long> sampling rate :Default 20000\n");
136 fprintf(stderr, "-d <float> delay (ms) :Default 8ms\n");
137 fprintf(stderr, "-g <float> gain (>=-1.0, <=1.0) :Default 1.0 \n");
138 fprintf(stderr, "-i <int> number of iterations :Default 4\n");
139 fprintf(stderr, "-T <long> length of output (ms) :Default 1000ms \n");
140 /* fprintf(stderr, "-div <float> divide input by <float> \n"); */
141 fprintf(stderr, "\n");
142 fprintf(stderr, "-S Make 'Add-Same' IRN :Default\n");
143 fprintf(stderr, "-O Make 'Add-Original' IRN \n");
144
145 fprintf(stderr, "-in <filename> input noise \n");
146 fprintf(stderr, "-out <filename> output-filename \n");
147 fprintf(stderr, "-v verbose information\n");
148 fprintf(stderr, "\n");
149 exit(1);
150 }
151
152 if (samplerate == 0) {
153 fprintf(stderr, "synthirn: unspecified sampling rate. \n");
154 exit(-1);
155 }
156
157 /* INPUT --------------------------------------*/
158
159 /* Attempt to load input file. */
160 if (inputflag == OFF ) {
161 fprintf(stderr, "synthirn: no input file specified. \n");
162 exit(-1);}
163 if (outputflag == OFF ) {
164 fprintf(stderr, "synthirn: no output file specified.\n");
165 exit(-1);}
166
167 inputfp = fopen(inputfn, "rb");
168 if (inputfp == NULL) {
169 fprintf(stderr, "synthirn: unable to open file %s.\n", inputfn);
170 exit(-1); }
171
172 /* Clear the arrays */
173 if (verboseflag == ON){
174 fprintf(stderr, "clearing ... ");
175 fflush(stderr);}
176
177 for (x=0; x<MAXSAMPLES; x++){
178 input[x] = 0;
179 }
180
181 /* load input */
182 if (verboseflag == ON){
183 fprintf(stderr, "loading ... ");
184 fflush(stderr);}
185 x=0;
186 while( feof(inputfp) ==0) {
187 fread(sample, 2, 1, inputfp);
188 input[x++] = (int) sample[0];}
189 length = x - 1;
190
191 /* close input */
192 fclose(inputfp);
193
194
195
196 /*----------------------------------------------*/
197
198 /* convert into samples */
199 delay_samples = (long) (delay * samplerate) / 1000;
200 outputlength_samples = (long) (outputlength * samplerate) / 1000;
201
202 if (verboseflag == ON){
203 fprintf(stderr, "floats ... ");
204 fflush(stderr);}
205
206 for (x=0; x<length; x++) {
207 if (divideflag == ON)
208 y[x] = (float) ((float) input[x] )/ dividevalue;
209 else
210 y[x] = (float) input[x];}
211
212
213 /* Do the iterations .... */
214
215 if (verboseflag == ON){
216 fprintf(stderr, "Gain is %f\n", gain);
217 den_gain=fabs(gain);
218 fprintf(stderr, "Den Gain is %f\n", den_gain);
219 fprintf(stderr, "n: ");
220 fflush(stderr);}
221
222 for (counter = 1; counter <= max_iterations; counter ++) {
223 if (verboseflag == ON){
224 fprintf(stderr, "%i ", counter);
225 fflush(stderr);}
226
227 /*---------------------------------*/
228
229 if (typeflag == SAME) {
230 arraydelay = delay_samples;
231
232 for (x = 0; x < length; x++)
233 temp[x] = (float) y[x];
234
235 for (x = 0; x < arraydelay; x++)
236 y[x] = (float) temp[x] + (gain * temp[length - arraydelay + x]) ;
237
238 for (x = arraydelay; x < length; x++)
239 y[x] = (float) temp[x] + (gain * temp[x - arraydelay]) ;
240
241 for (x = 0; x < length; x++)
242 y[x] = (float) y[x] / (1.0 + den_gain);}
243
244 /*---------------------------------*/
245
246 else if (typeflag == ORIGINAL) {
247 arraydelay = delay_samples;
248
249 for (x = 0; x < length; x++)
250 temp[x] = (float) y[x];
251
252 for (x = 0; x < arraydelay; x++)
253 if (divideflag == ON)
254 y[x] = (float) ((float) input[x] )/ dividevalue + (den_gain * temp[length - arraydelay + x]) ;
255 else
256 y[x] = (float) input[x] + (gain * temp[length - arraydelay + x]); /* div by pow */
257
258 for (x = arraydelay; x < length; x++)
259 if (divideflag == ON)
260 y[x] = (float) ((float) input[x] )/ dividevalue + (den_gain * temp[x - arraydelay]) ;
261 else
262 y[x] = (float) input[x] + (gain * temp[x - arraydelay]);
263
264 /* for (x=0; x < length; x++)
265 y[x] = (float) y[x] / (1. + den_gain*temp[x]) ; */ }
266
267 /* Not used; makes noisy IRN's . Wrong divisor */
268
269 /*---------------------------------*/
270
271 else {
272 fprintf(stderr, "synthirn: must specify either 'Same' or 'Original' noise.\n");
273 exit(-1);}
274 }
275
276 if (verboseflag == ON){
277 fprintf(stderr, "normalising ... ");
278 fflush(stderr);}
279
280 /* Normalise */
281 if (typeflag == ORIGINAL)
282 for (x=0; x<length; x++)
283 /* output[x] = (short) y[x]; */
284 output[x] = (short) (y[x] /((max_iterations*den_gain)+1));
285 else if (typeflag == SAME)
286 for (x=0; x<length; x++)
287 output[x] = (short) y[x];
288
289 /* reset output positions */
290 arraydelay = max_iterations * delay_samples;
291
292 /* OUTPUT ------------------------------------- */
293
294
295 outputfp = fopen(outputfn, "wb");
296 if (outputfn == NULL){
297 fprintf(stderr, "synthirn: unable to open output file %s.\n", outputfn);
298 exit(-1); }
299
300 /* write data */
301 if (verboseflag == ON){
302 fprintf(stderr, "saving ");
303 fflush(stderr);}
304
305 fwrite(output, 2, outputlength_samples, outputfp);
306
307 fclose(outputfp);
308
309 if (verboseflag == ON){
310 fprintf(stderr, "\n");
311 fflush(stderr);}
312 }
313
314
315 /* The End */
316 /*-----------------------------------------------------------------------*/
317
318
319