annotate xaim/synthdramp.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
rev   line source
tomwalters@0 1 /* synthdramp
tomwalters@0 2 * ------------
tomwalters@0 3 *
tomwalters@0 4 * (A tidied-up version of makeping)
tomwalters@0 5 *
tomwalters@0 6 * original: M. Akeroyd. Autumn 1992.
tomwalters@0 7 *
tomwalters@0 8 * revisions: roy: 3dec92: decay parameter is now half-life.
tomwalters@0 9 * MAA: sep93: tidied up the code, got rid of the .h file
tomwalters@0 10 *
tomwalters@0 11 * MAA: February 1994: added floor values.
tomwalters@0 12 * MAA: March 1994: Added initial offsets, and 'steps'
tomwalters@0 13 */
tomwalters@0 14
tomwalters@0 15
tomwalters@0 16 #include <stdio.h>
tomwalters@0 17 #include <stdlib.h>
tomwalters@0 18 #include <string.h>
tomwalters@0 19 #include <math.h>
tomwalters@0 20
tomwalters@0 21 /* the following used to be in "makeping.h" */
tomwalters@0 22
tomwalters@0 23 #define PulseFreqDef 40 /* Hz */
tomwalters@0 24 #define SinFreqDef 800 /* carrier freq: Hz */
tomwalters@0 25 #define SampRateDef 20000 /* sampling rate: samples per sec */
tomwalters@0 26 #define SinPhaseDef 0.0 /* of carrier wrt to pulse: degrees */
tomwalters@0 27 #define AmpMaxDef 10000 /* max amplitude */
tomwalters@0 28 #define NoOfPulsesDef 20 /* how many to make */
tomwalters@0 29 #define MaxNoSamplesPerWave 882000 /* limit on array size */
tomwalters@0 30 #define DampCoefDef 5.0 /* half-life; msecs */
tomwalters@0 31 #define FileNameDef "nofilespecified"
tomwalters@0 32 #define FORWARD 2 /* "forward" == damp, */
tomwalters@0 33 #define BACKWARD 3 /* "backward" == ramp.
tomwalters@0 34 * (for historical reasons).
tomwalters@0 35 */
tomwalters@0 36 #define ON 1
tomwalters@0 37 #define OFF 0
tomwalters@0 38 #define PI 3.141592654
tomwalters@0 39
tomwalters@0 40
tomwalters@0 41
tomwalters@0 42
tomwalters@0 43 /*---------------------------------------------------------------------------*/
tomwalters@0 44 /*---------------------------------------------------------------------------*/
tomwalters@0 45
tomwalters@0 46
tomwalters@0 47 main(int argc, char *argv[])
tomwalters@0 48 {
tomwalters@0 49 int x = 1;
tomwalters@0 50 int helpflag = OFF;
tomwalters@0 51 int gateoption = OFF;
tomwalters@0 52 int toneflag = OFF;
tomwalters@0 53 int floorflag = OFF;
tomwalters@0 54 long pulsefreq = PulseFreqDef;
tomwalters@0 55 long sinfreq = SinFreqDef;
tomwalters@0 56 long samprate = SampRateDef;
tomwalters@0 57 long ampmax = AmpMaxDef;
tomwalters@0 58 double sinphase = SinPhaseDef;
tomwalters@0 59 long noofpulses = NoOfPulsesDef;
tomwalters@0 60 double dampcoef = (double) DampCoefDef;
tomwalters@0 61 int directionflag = FORWARD;
tomwalters@0 62 short sample_short[MaxNoSamplesPerWave]; /* the array of data ... */
tomwalters@0 63
tomwalters@0 64 char *filename = FileNameDef;
tomwalters@0 65 FILE *filepointer;
tomwalters@0 66
tomwalters@0 67 long sampleno, localsampleno, lengthwave_samples;
tomwalters@0 68 double lengthpulse_samples;
tomwalters@0 69 double phaserad;
tomwalters@0 70 double expfactor;
tomwalters@0 71 double temp;
tomwalters@0 72 double lengthwave;
tomwalters@0 73 double sinfreqrad;
tomwalters@0 74
tomwalters@0 75 int floor = 0;
tomwalters@0 76 int offset_ms = 0;
tomwalters@0 77 int phase_deg = 0;
tomwalters@0 78 long offset_samples = 0;
tomwalters@0 79 double fraction = 0.0;
tomwalters@0 80 long temp_samples = 0;
tomwalters@0 81 int delay_ms = 0;
tomwalters@0 82 long delay_samples = 0;
tomwalters@0 83
tomwalters@0 84 /*---------------------------------------------------------*/
tomwalters@0 85
tomwalters@0 86 /* Options handler. Hack but it works.
tomwalters@0 87 * ....................................
tomwalters@0 88 */
tomwalters@0 89
tomwalters@0 90 /* special hack for '-h' */
tomwalters@0 91 if ((argc ==2) && (!strcmp(argv[1], "-h"))) {
tomwalters@0 92 argc=1; /* to bypass the upcoming 'while' */
tomwalters@0 93 helpflag=ON;}
tomwalters@0 94
tomwalters@0 95 while (x < argc )
tomwalters@0 96 {
tomwalters@0 97 if (!strcmp(argv[x], "-p")) { pulsefreq=atol(argv[x+1]); x += 2;}
tomwalters@0 98 else if (!strcmp(argv[x], "-f")) { sinfreq=atol(argv[x+1]); x += 2; }
tomwalters@0 99 else if (!strcmp(argv[x], "-s")) { samprate=atol(argv[x+1]); x += 2;}
tomwalters@0 100 else if (!strcmp(argv[x], "-w")) { sinphase=atof(argv[x+1]); x += 2;}
tomwalters@0 101 else if (!strcmp(argv[x], "-m")) { ampmax=atol(argv[x+1]); x += 2;}
tomwalters@0 102 else if (!strcmp(argv[x], "-n")) { noofpulses=atof(argv[x+1]); x += 2;}
tomwalters@0 103 else if (!strcmp(argv[x], "-h")) { dampcoef=atof(argv[x+1]); x += 2;}
tomwalters@0 104 else if (!strcmp(argv[x], "-floor")) { floorflag=ON; floor=atoi(argv[x+1]); x += 2;}
tomwalters@0 105 else if (!strcmp(argv[x], "-offset")) { offset_ms=atoi(argv[x+1]); x += 2;}
tomwalters@0 106 else if (!strcmp(argv[x], "-delay")) { delay_ms=atoi(argv[x+1]); x += 2;}
tomwalters@0 107 else if (!strcmp(argv[x], "-phase")) { phase_deg=atoi(argv[x+1]); x += 2;}
tomwalters@0 108 else if (!strcmp(argv[x], "-frac")) { fraction=atof(argv[x+1]); x += 2;}
tomwalters@0 109 else if (!strcmp(argv[x], "-t")) { toneflag=ON; x += 1;}
tomwalters@0 110 else if (!strcmp(argv[x], "-o")) { filename=argv[x+1]; x+=2;}
tomwalters@0 111 else if (!strcmp(argv[x], "-d")) { directionflag=FORWARD; x+=1;}
tomwalters@0 112 else if (!strcmp(argv[x], "-r")) { directionflag=BACKWARD; x+=1;}
tomwalters@0 113 else if (!strcmp(argv[x], "-help")) { helpflag=ON; x+=1;}
tomwalters@0 114 else if (!strcmp(argv[x], "-g")) { gateoption=ON; x+=1;}
tomwalters@0 115 else { printf("\nUnknown Option : %s \n", argv[x]);
tomwalters@0 116 return 1;}
tomwalters@0 117 }
tomwalters@0 118
tomwalters@0 119 if ((helpflag == ON) || (argc == 1))
tomwalters@0 120 {printf("\n ---------- synthdramp (Winter 1994) --------------\n");
tomwalters@0 121 printf("\n");
tomwalters@0 122 printf("Tone options : \n");
tomwalters@0 123 printf(" -w <flt> phase of sin wave, wrt begining of pulse default: %3.3f degs\n", SinPhaseDef);
tomwalters@0 124 printf(" -f <int> carrier frequency default: %d Hz\n", SinFreqDef);
tomwalters@0 125 printf("\n");
tomwalters@0 126 printf("Gate options : \n");
tomwalters@0 127 printf(" -d damped sinusoid (default)\n");
tomwalters@0 128 printf(" -r ramped sinusoid \n");
tomwalters@0 129 printf(" -s <int> sampling rate default: %d samp/sec\n", SampRateDef);
tomwalters@0 130 printf(" -m <int> max amplitude default: %d \n", AmpMaxDef);
tomwalters@0 131 printf(" -n <int> number of cycles in train default: %d \n", NoOfPulsesDef);
tomwalters@0 132 printf(" -h <flt> half-life default: %3.3f \n", DampCoefDef);
tomwalters@0 133 printf(" -p <int> repitition rate default: %d Hz\n", PulseFreqDef);
tomwalters@0 134 printf(" -floor <int> floor value default: %d \n", floor);
tomwalters@0 135 printf(" -delay <int> delay at begining (ms) default: %d \n", delay_ms);
tomwalters@0 136 printf("\n");
tomwalters@0 137 printf("'Step' options (only work if just gate is created): \n");
tomwalters@0 138 printf(" -offset <int> offset time to step (ms) default: %d \n", offset_ms);
tomwalters@0 139 printf(" -phase <int> offset, measured as phase delay default: %d degrees\n", phase_deg);
tomwalters@0 140 printf(" -frac <flt> fractional height of step default: 3.3f \n", fraction);
tomwalters@0 141 printf("\n");
tomwalters@0 142 printf("Output options : \n");
tomwalters@0 143 printf(" -g gate only : no tone\n");
tomwalters@0 144 printf(" -t tone only : no gate\n");
tomwalters@0 145 printf(" -o '' output filename \n");
tomwalters@0 146 printf("\n");
tomwalters@0 147 exit(-1);
tomwalters@0 148 }
tomwalters@0 149
tomwalters@0 150 if (!strcmp(FileNameDef, filename))
tomwalters@0 151 {
tomwalters@0 152 fprintf(stderr, "\n No file has been specified. Stopping. \n\n");
tomwalters@0 153 exit(-1);
tomwalters@0 154 }
tomwalters@0 155
tomwalters@0 156
tomwalters@0 157 /*---------------------------------------------------------*/
tomwalters@0 158
tomwalters@0 159 /* convert the damping coeeficent from a half-life to whatever the internal
tomwalters@0 160 * value is.
tomwalters@0 161 * roy 23-11-92 thru 3-12-92) */
tomwalters@0 162 dampcoef = 0.693147/(dampcoef/1000) ;
tomwalters@0 163
tomwalters@0 164 /* define useful things:
tomwalters@0 165 * lengthwave: overall length of wave, in seconds.
tomwalters@0 166 * lengthwave_samples: overlall length of wave, in samples.
tomwalters@0 167 * lengthpulse_samples: length of each pulse, in samples.
tomwalters@0 168 * phaserad: the phase angle, in radians as against degrees.
tomwalters@0 169 * sinfreqrad: constant to speed the code up.
tomwalters@0 170 */
tomwalters@0 171 lengthwave = (double) noofpulses / pulsefreq;
tomwalters@0 172 lengthwave_samples = (long) samprate * noofpulses / pulsefreq;
tomwalters@0 173 lengthpulse_samples = (double) samprate / pulsefreq;
tomwalters@0 174
tomwalters@0 175 phaserad = sinphase * PI / 180;
tomwalters@0 176 sinfreqrad = (double) 2 * PI * sinfreq;
tomwalters@0 177
tomwalters@0 178 /* error-check */
tomwalters@0 179 if (lengthwave_samples >= MaxNoSamplesPerWave) {
tomwalters@0 180 fprintf(stderr, "Wave too long for internal arrays. \n");
tomwalters@0 181 exit(-1);}
tomwalters@0 182
tomwalters@0 183 /*---------------------------------------------------------*/
tomwalters@0 184
tomwalters@0 185
tomwalters@0 186 /* delay */
tomwalters@0 187
tomwalters@0 188 for (sampleno=0; sampleno < lengthwave_samples; sampleno++)
tomwalters@0 189 sample_short[sampleno] = (short) 0;
tomwalters@0 190
tomwalters@0 191 filepointer = fopen(filename, "w");
tomwalters@0 192
tomwalters@0 193 if (delay_ms != 0) {
tomwalters@0 194 delay_samples = (long) samprate * delay_ms / 1000;
tomwalters@0 195 fwrite(sample_short, 2, delay_samples, filepointer);}
tomwalters@0 196
tomwalters@0 197 for (sampleno=0; sampleno < lengthwave_samples; sampleno++)
tomwalters@0 198 sample_short[sampleno] = (short) 0;
tomwalters@0 199
tomwalters@0 200
tomwalters@0 201 /*---------------------------------------------------------*/
tomwalters@0 202
tomwalters@0 203 /* for each sample, from 0 to the end, work out its value by (1) taking the
tomwalters@0 204 * value of the carrier, and (2) multiplying it by the exp factor.
tomwalters@0 205 * if the "-g" option has been called, then in effect make the exp "gate"
tomwalters@0 206 * only ...
tomwalters@0 207 * The results go into sample_short[].
tomwalters@0 208 */
tomwalters@0 209
tomwalters@0 210 /*---------------------------*/
tomwalters@0 211 for (sampleno=0; sampleno < lengthwave_samples; sampleno++) {
tomwalters@0 212
tomwalters@0 213 temp = (double) (fmod(sampleno, lengthpulse_samples)) / samprate;
tomwalters@0 214 expfactor = exp( -1 * dampcoef * temp ) ; /* * samprate roy */
tomwalters@0 215
tomwalters@0 216 if (directionflag == FORWARD)
tomwalters@0 217 localsampleno = sampleno;
tomwalters@0 218 else
tomwalters@0 219 localsampleno = lengthwave_samples -2 - sampleno;
tomwalters@0 220 /* -2 seems to work */
tomwalters@0 221
tomwalters@0 222 sample_short[localsampleno]= ampmax * sin(sinfreqrad * temp + phaserad) * expfactor;
tomwalters@0 223 if (gateoption == ON)
tomwalters@0 224 sample_short[localsampleno] = ampmax * expfactor;
tomwalters@0 225 if (floorflag == ON)
tomwalters@0 226 if (sample_short[localsampleno] <= floor )
tomwalters@0 227 sample_short[localsampleno] = (short) floor;
tomwalters@0 228 }
tomwalters@0 229
tomwalters@0 230 if (toneflag == ON) {
tomwalters@0 231 for (sampleno=0; sampleno < lengthwave_samples; sampleno++) {
tomwalters@0 232 temp = (double) (sampleno) / samprate;
tomwalters@0 233 expfactor=1;
tomwalters@0 234 sample_short[sampleno]= ampmax * sin(sinfreqrad * temp + phaserad) * expfactor;}}
tomwalters@0 235
tomwalters@0 236
tomwalters@0 237 /*---------------------------*/
tomwalters@0 238
tomwalters@0 239
tomwalters@0 240
tomwalters@0 241 /* Only do this is step specified: ie, offset or phase ! = 0 */
tomwalters@0 242
tomwalters@0 243 offset_samples = (long) 0;
tomwalters@0 244
tomwalters@0 245 if (offset_ms != 0)
tomwalters@0 246 offset_samples = (long) samprate * offset_ms / 1000;
tomwalters@0 247 if (phase_deg != 0)
tomwalters@0 248 offset_samples = (long) lengthpulse_samples * phase_deg / 360;
tomwalters@0 249
tomwalters@0 250 if (offset_samples != 0) {
tomwalters@0 251 for (sampleno=0; sampleno < lengthwave_samples; sampleno++) {
tomwalters@0 252
tomwalters@0 253 temp = (double) (fmod(sampleno, lengthpulse_samples)) / samprate;
tomwalters@0 254 expfactor = exp( -1 * dampcoef * temp ) ; /* * samprate roy */
tomwalters@0 255
tomwalters@0 256 if (directionflag == FORWARD)
tomwalters@0 257 localsampleno = sampleno;
tomwalters@0 258 else
tomwalters@0 259 localsampleno = lengthwave_samples -2 - sampleno;
tomwalters@0 260 /* -2 seems to work */
tomwalters@0 261
tomwalters@0 262 temp_samples = (long) fmod(sampleno, lengthpulse_samples);
tomwalters@0 263 if (temp_samples <= offset_samples)
tomwalters@0 264 sample_short[localsampleno] = sample_short[localsampleno];
tomwalters@0 265 else
tomwalters@0 266 /* sample_short[localsampleno]= fraction * ampmax * sin(sinfreqrad * temp + phaserad) * expfactor + sample_short[localsampleno];
tomwalters@0 267 *
tomwalters@0 268 * if (gateoption == ON)*/
tomwalters@0 269 sample_short[localsampleno] = fraction * ampmax * expfactor + sample_short[localsampleno];
tomwalters@0 270
tomwalters@0 271
tomwalters@0 272 }}
tomwalters@0 273
tomwalters@0 274 /*---------------------------------------------------------*/
tomwalters@0 275
tomwalters@0 276 fwrite(sample_short, 2, lengthwave_samples, filepointer);
tomwalters@0 277 fclose(filepointer);
tomwalters@0 278
tomwalters@0 279 exit(0);
tomwalters@0 280
tomwalters@0 281 }
tomwalters@0 282
tomwalters@0 283
tomwalters@0 284 /* the end */
tomwalters@0 285
tomwalters@0 286
tomwalters@0 287
tomwalters@0 288