-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiggen.c
323 lines (277 loc) · 8.09 KB
/
siggen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* Copyright (c) 2009 Richard Dobson
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
// siggen.c : oscillator generator
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <portsf.h>
#include <breakpoints.h>
#include <time.h>
#include <wave.h>
// set size of multi-channel frame-buffer
#define NFRAMES (1024)
// TODO define program argument list, excluding flags
enum {ARG_PROGNAME, ARG_OUTFILE, ARG_TYPE, ARG_DUR, ARG_SRATE, ARG_AMP, ARG_FREQ, ARG_NARGS};
enum { WAVE_SINE = 0, WAVE_TRIANGLE, WAVE_SQUARE, WAVE_SAWUP, WAVE_SAWDOWN, WAVE_NTYPES};
int main(int argc, char* argv[])
{
// STAGE 1
// init 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;
unsigned long outframes, nbufs, i;
long remainder;
int wavetype;
OSCIL* osc = NULL;
tickfunc tick;
FILE* fpfreq = NULL;
FILE* fpamp = NULL;
BRKSTREAM* freqstream = NULL;
BRKSTREAM* ampstream = NULL;
unsigned long brkfreqSize = 0, brkampSize = 0;
double minval, maxval;
printf("SIGGEN: generate simple waveforms\n");
// STAGE 2
// check rest of commandline
if (argc < ARG_NARGS) {
printf("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"
);
return 1;
}
// no stage 3
// STAGE 4
// TODO: any other argument processing and setup of variables,
// output buffer, etc., before creating outfile
wavetype = atoi(argv[ARG_TYPE]);
if (wavetype < WAVE_SINE || wavetype > WAVE_NTYPES) {
printf("Error: bad value for wave type\n");
return 1;
}
switch(wavetype) {
case(WAVE_SINE):
tick = sinetick;
break;
case(WAVE_TRIANGLE):
tick = tritick;
break;
case(WAVE_SQUARE):
tick = sqtick;
break;
case(WAVE_SAWUP):
tick = sawutick;
break;
case(WAVE_SAWDOWN):
tick = sawdtick;
break;
}
// define outfile format
outprops.srate = atoi(argv[ARG_SRATE]);
if (outprops.srate <= 0) {
printf("error: srate must be positive\n");
return 1;
}
outprops.chans = 1;
outprops.samptype = (psf_stype) PSF_SAMP_16;
outprops.chformat = STDWAVE;
dur = atof(argv[ARG_DUR]);
if (dur <= 0.0) {
printf("Error: amp must be positive\n");
return 1;
}
outframes = (unsigned long) (dur * outprops.srate + 0.5);
nbufs = outframes / nframes;
remainder = outframes - nbufs * nframes;
if (remainder) nbufs++;
// open breakpoint files, or set constants
fpamp = fopen(argv[ARG_AMP], "r");
if (fpamp == NULL) {
amp = atof(argv[ARG_AMP]);
if (amp <= 0.0 || amp > 1.0) {
printf("Error: amplitude value out of range: 0.0 < amp <= 1.0\n");
error++;
goto exit;
}
} else {
ampstream = bps_newstream(fpamp, outprops.srate, &brkampSize);
if (ampstream == NULL) {
printf("Error reading amplitude breakpoint file %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: 0.0 < amp <= 1.0\n", argv[ARG_AMP]);
error++;
goto exit;
}
}
fpfreq = fopen(argv[ARG_FREQ], "r");
if (fpfreq== NULL) {
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 freq breakpoint file %s\n", argv[ARG_FREQ]);
error++;
goto exit;
}
if (bps_getminmax(freqstream, &minval, &maxval)) {
printf("Error reading range of breakpoint file %s\n", argv[ARG_FREQ]);
error++;
goto exit;
}
if (minval <= 0.0 ) {
// maxval = don't care
printf("Error in file %s: frequency values must be positive\n", argv[ARG_AMP]);
error++;
goto exit;
}
}
osc = new_oscil(outprops.srate);
if (osc == NULL) {
puts("no memory for oscillator\n");
error++;
goto exit;
}
// always startup portsf
if (psf_init()) {
printf("unable to start portsf\n");
error++;
goto exit;
}
// allocate space for sample frame buffer
outframe = (float*) malloc(nframes * outprops.chans * sizeof(float));
if (outframe==NULL) {
puts("No memory!\n");
error++;
goto exit;
}
// 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;
}
outprops.format = outformat;
// handle outfile
// TODO: make any changes to outprops here
peaks = (PSF_CHPEAK*) malloc(outprops.chans * sizeof(PSF_CHPEAK));
if (peaks == NULL) {
puts("No memory!\n");
error++;
goto exit;
}
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;
}
printf("processing....\n");
// STAGE 5
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 * tick(osc,freq));
}
if (psf_sndWriteFloatFrames(ofd, outframe, nframes) != nframes) {
printf("Error writing to outfile\n");
error++;
break;
}
}
printf("Done: %d errors\n",error);
// report PEAK values to user
// STAGE 6
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 %lu:\t%.4f at %.4f secs\n", i + 1, peaks[i].val, peaktime);
}
}
// do all cleanup
// STAGE 7
exit:
if (ofd >= 0) {
if (psf_sndClose(ofd))
printf("Error closing outfile %s\n",argv[ARG_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[ARG_FREQ]);
}
if (fpamp) {
if (fclose(fpamp))
printf("Error closing breakpoint file %s\n", argv[ARG_AMP]);
}
if (freqstream) {
bps_freepoints(freqstream);
free(freqstream);
}
if (ampstream) {
bps_freepoints(ampstream);
free(ampstream);
}
psf_finish();
return error;
}