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