-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfpan.c
315 lines (261 loc) · 7.69 KB
/
sfpan.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
/**
* 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.
*/
// main.c : generic soundfile processing main function
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <portsf.h>
#include <breakpoints.h>
/* set size of multi-channel frame-buffer */
#define NFRAMES (1024)
typedef struct panpos {
double left;
double right;
} PANPOS;
PANPOS constpower(double position);
PANPOS constpower(double position)
{
PANPOS pos;
const double piovr2 = 4.0 * atan(1.0) * 0.5;
const double root2ovr2 = sqrt(2.0) * 0.5;
// const double thispos = position * piovr2;
const double angle = position * piovr2 * 0.5;
// the last 0.5 is to scale for two channels across pi/4 each.
const double sinangle = sin(angle);
const double cosangle = cos(angle);
pos.left = root2ovr2 * (cosangle - sinangle);
pos.right = root2ovr2 * (cosangle + sinangle);
return pos;
}
PANPOS simplepan(double position);
PANPOS simplepan(double position)
{
PANPOS pos;
position *= 0.5;
pos.left = position - 0.5;
pos.right = position + 0.5;
return pos;
}
// TODO define program argument list, excluding flags
enum {ARG_PROGNAME, ARG_INFILE, ARG_OUTFILE, ARG_BRKFILE, ARG_NARGS};
int main(int argc, char* argv[])
{
// STAGE 1
PSF_PROPS inprops, outprops;
long framesread;
/* init all dynamic resources to default states */
int infile = -1, outfile = -1;
int error = 0;
PSF_CHPEAK* peaks = NULL;
psf_format outformat = PSF_FMT_UNKNOWN;
unsigned long nframes = NFRAMES;
float* inframe = NULL;
float* outframe = NULL;
PANPOS pos;
double timeincr, stereopos, sampletime;
// breakpoint file needs
FILE* fp = NULL;
unsigned long size;
BREAKPOINT* points = NULL;
// TODO: define an output frame buffer if channel width different
// STAGE 2
printf("MAIN: generic process\n");
// process any optional flags: remove this block if none used
if (argc > 1) {
char flag;
while (argv[1][0] == '-') {
flag = argv[1][1];
switch (flag) {
// TODO: handle any flag arguments here
case ('\0'):
printf("Error: missing flag name\n");
return 1;
default:
break;
}
argc--;
argv++;
}
}
// check rest of commandline
if (argc < ARG_NARGS) {
printf("insufficient arguments.\n"
// TODO: add required usage message
"usage: \n\t"
"\tsfpan infile outfile posfile.brk\n"
"\tposfile.brk is breakpoint file\n"
"\twith values in range -1.0 < = pos <= 1.0\n"
"\twhere -1.0 = full left, 0 = center, +1.0 = full right\n"
);
return 1;
}
// always startup portsf
if (psf_init()) {
printf("unable to start portsf\n");
return 1;
}
// read breakpoint file and verify it
fp = fopen(argv[ARG_BRKFILE], "r");
if (fp == NULL) {
printf("Error: unable to open"
"breakpoint file %s\n", argv[ARG_BRKFILE]);
error++;
goto exit;
}
points = get_breakpoints(fp, &size);
if (points == NULL) {
printf("No breakpoints read.\n");
error++;
goto exit;
}
if (size < 2) {
printf("Error: at least two breakpoints required\n");
error++;
goto exit;
}
// we require breakpoints to start from 0
if (points[0].time != 0.0) {
printf("Error in breakpoint data: "
"first time must be 0.0\n");
error++;
goto exit;
}
if (!inrange(points, -1, 1.0, size)) {
printf("Error in breakpoint file: "
"values out of range -1 to +1\n");
error++;
goto exit;
}
// STAGE 3
infile = psf_sndOpen(argv[ARG_INFILE], &inprops, 0);
if (infile < 0) {
printf("Error: unable to open infile %s\n", argv[ARG_INFILE]);
error++;
goto exit;
}
if (inprops.chans != 1) {
printf("Error: infile must be mono %d.\n", inprops.chans);
error++;
goto exit;
}
// TODO: verify infile format for this application
// allocate space for sample frame buffer ...
inframe = (float*) malloc(nframes * inprops.chans * sizeof(float));
if (inframe == 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;
}
inprops.format = outformat;
outprops = inprops;
// STAGE 4
// TODO: any other argument processing and setup of variables,
// output buffer, etc., before creating outfile
// handle outfile
// TODO: make any changes to outprops here
// mono -> stereo
outprops.chans = 2;
peaks = (PSF_CHPEAK*) malloc(outprops.chans * sizeof(PSF_CHPEAK));
if (peaks == NULL) {
puts("No memory!\n");
error++;
goto exit;
}
// TODO: if outchans != inchans, allocate outframe, and modify main loop accordingly
// create stereo output buffer
outframe = (float *) malloc(nframes * outprops.chans * sizeof(float));
if (outframe == NULL) {
puts("No memory\n");
error++;
goto exit;
}
outfile = psf_sndCreate(argv[ARG_OUTFILE], &outprops, 0, 0, PSF_CREATE_RDWR);
if (outfile < 0) {
printf("Error: unable to create outfile %s\n", argv[ARG_OUTFILE]);
error++;
goto exit;
}
// STAGE 5
printf("processing....\n");
// init time position counter for reading envelope
timeincr = 1.0 / inprops.srate;
sampletime = 0.0;
while ((framesread = psf_sndReadFloatFrames(infile, inframe, nframes)) > 0) {
int i, out_i;
for (i = 0, out_i = 0; i < framesread; i++) {
stereopos = val_at_brktime(points, size, sampletime);
pos = constpower(stereopos);
outframe[out_i++] = (float) (inframe[i] * pos.left);
outframe[out_i++] = (float) (inframe[i] * pos.right);
sampletime += timeincr;
}
if (psf_sndWriteFloatFrames(outfile, outframe, framesread) != framesread) {
printf("Error writing to outfile\n");
error++;
break;
}
}
if (framesread < 0) {
printf("Error reading infile. Outfile is incomplete.\n");
error++;
} else {
printf("Done: %d errors\n",error);
}
// STAGE 6
// report PEAK values to user
if (psf_sndReadPeaks(outfile, peaks, NULL) > 0) {
long i;
double peaktime;
printf("PEAK information:\n");
for (i = 0; i < inprops.chans; i++) {
peaktime = (double) peaks[i].pos / (double) inprops.srate;
printf("CH %ld:\t%.4f at %.4f secs\n", i+1, peaks[i].val, peaktime);
}
}
// STAGE 7
// do all cleanup
exit:
if (infile >= 0) {
if (psf_sndClose(infile))
printf("%s: Warning: error closing infile %s\n", argv[ARG_PROGNAME], argv[ARG_INFILE]);
}
if (outfile >= 0) {
if (psf_sndClose(outfile))
printf("%s: Warning: error closing outfile %s\n", argv[ARG_PROGNAME], argv[ARG_OUTFILE]);
}
if (outframe) free(outframe);
if (inframe) free(inframe);
if (peaks) free(peaks);
//TODO: cleanup any other resources
psf_finish();
return error;
}