-
Notifications
You must be signed in to change notification settings - Fork 1
/
sf2float.c
397 lines (371 loc) · 12.5 KB
/
sf2float.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* sf2float.c : convert soundfile to floats using portsf library */
#include <stdio.h>
#include <stdlib.h>
#include "portsf.h"
#include <math.h>
int main(int argc, char* argv[])
{
int i;
PSF_PROPS props;
long framesread;
long totalread;
/* init all dynamic resources to default states */
int ifd = -1,ofd = -1;
int error = 0;
PSF_CHPEAK* peaks = NULL;
float* frame = NULL;
psf_format outformat = PSF_FMT_UNKNOWN;
printf("SF2FLOAT: convert soundfile to 32bit floats format\n");
if(argc < 4){
printf("insufficient arguments.\n"
"usage:\n\t"
"sf2float option infile outfile\n");
return 1;
}
/* be good, and startup portsf */
if(psf_init()){
printf("unable to start portsf\n");
return 1;
}
ifd = psf_sndOpen(argv[2],&props,0);
if(ifd < 0 && atoi(argv[1])!=5){
printf("Error: unable to open infile %s\n",argv[2]);
return 1;
}
if(props.samptype == PSF_SAMP_16){
printf("Info: infile is in 16 bit format.\n");
}
if(props.samptype == PSF_SAMP_24){
printf("Info: infile is in 24 bit format.\n");
}
if(props.samptype == PSF_SAMP_32){
printf("Info: infile is in 32 bit format.\n");
}
/* we now have a resource, so we use goto hereafter on hitting any error */
/* tell user if source file is already floats */
if(props.samptype == PSF_SAMP_IEEE_FLOAT){
printf("Info: infile is already in floats format.\n");
}
props.samptype = PSF_SAMP_IEEE_FLOAT;
/* check file extension of outfile name, so we use correct output file format*/
// TODO: needs to be changed because output is not always the same format
if (atoi(argv[1])<3 || atoi(argv[1])==5) {
outformat = psf_getFormatExt(argv[3]);
printf("output file format: %d", outformat);
if(outformat == PSF_FMT_UNKNOWN){
printf("outfile name %s has unknown format.\n"
"Use any of .wav, .aiff, .aif, .afc, .aifc\n",argv[3]);
error++;
goto exit;
}
}
else if (atoi(argv[1])==4) {
outformat = psf_getFormatExt(argv[4]);
printf("output file format: %d", outformat);
if(outformat == PSF_FMT_UNKNOWN){
printf("outfile name %s has unknown format.\n"
"Use any of .wav, .aiff, .aif, .afc, .aifc\n",argv[4]);
error++;
goto exit;
}
}
props.format = outformat;
if(atoi(argv[1])<3 || atoi(argv[1])==5) {
props.srate = 44100;
props.chans = 2;
props.samptype = PSF_SAMP_IEEE_FLOAT;
props.format = PSF_STDWAVE;
props.chformat = STDWAVE;
ofd = psf_sndCreate(argv[3],&props,0,0,PSF_CREATE_RDWR);
if(ofd < 0){
printf("Error: unable to create outfile %s\n",argv[3]);
error++;
goto exit;
}
}
else if (atoi(argv[1])==4) {
ofd = psf_sndCreate(argv[4],&props,0,0,PSF_CREATE_RDWR);
if(ofd < 0){
printf("Error: unable to create outfile %s\n",argv[4]);
error++;
goto exit;
}
}
/* allocate space for one sample frame */
frame = (float*) malloc(props.chans * sizeof(float));
if(frame==NULL){
puts("No memory!\n");
error++;
goto exit;
}
/* and allocate space for PEAK info */
peaks = (PSF_CHPEAK*) malloc(props.chans * sizeof(PSF_CHPEAK));
if(peaks == NULL){
puts("No memory!\n");
error++;
goto exit;
}
// if(atoi(argv[1])!=5) {
//printf("copying....\n");
/* single-frame loop to do copy: report any read/write errors */
// framesread = psf_sndReadFloatFrames(ifd,frame,1);
// totalread = 0; /* count sample frames as they are copied */
// }
int part = atoi(argv[1]);
/*
* Switch depending on the part of the assignment
*/
switch(part) {
/*
* Part 0: Output the file as is.
*/
case 0:
while (framesread == 1){
totalread++;
if(psf_sndWriteFloatFrames(ofd,frame,1) != 1){
printf("Error writing to outfile\n");
error++;
break;
}
framesread = psf_sndReadFloatFrames(ifd,frame,1);
}
break;
/*
* Part 1: Divide the value of each sample by 2.
*/
case 1: {
while (framesread == 1){
frame[0] = frame[0]/2;
frame[1] = frame[1]/2;
totalread++;
if(psf_sndWriteFloatFrames(ofd,frame,1) != 1){
printf("Error writing to outfile\n");
error++;
break;
}
framesread = psf_sndReadFloatFrames(ifd,frame,1);
}
break;
}
/*
* Part 2: Pan the output from left to right, with an interval of 5 seconds.
*/
case 2: {
double cycle = 5; // cycle of 5 seconds
double period = 4 * cycle; // sine or cosine period
double sampleLimit = period * 44100;
while (framesread == 1){
frame[0] = frame[0]*sin(2*M_PI*totalread/sampleLimit);
frame[1] = frame[1]*cos(2*M_PI*totalread/sampleLimit);
totalread++;
if(psf_sndWriteFloatFrames(ofd,frame,1) != 1){
printf("Error writing to outfile\n");
error++;
break;
}
framesread = psf_sndReadFloatFrames(ifd,frame,1);
}
break;
}
/*
* Part 3: Create envelope file.
*/
case 3:
{
double max = 0; // to keep track of the max in the interval
int currentSample = 0;
double intervalTime = 0.05;
double sampleLimit = 44100*intervalTime;
int interval = 1;
FILE *file;
file = fopen(argv[3], "w+");
printf("sampleLimit %f\n", sampleLimit);
while (framesread == 1){
if (currentSample < sampleLimit) {
if (max < frame[0]) {
max = frame[0];
}
if (max < -1*frame[0]) {
max = -1*frame[0];
}
if (max < frame[1]) {
max = frame[1];
}
if (max < -1*frame[1]) {
max = -1*frame[1];
}
currentSample++;
}
else {
fprintf(file, "%f %f\n", (double)(interval*intervalTime), max);
max = 0;
currentSample = 0;
interval++;
}
totalread++;
framesread = psf_sndReadFloatFrames(ifd,frame,1);
}
fclose(file);
break;
}
/*
* apply envelope on file
*/
case 4:
{
// open up file, put the reading index on the first line
FILE *file;
file = fopen(argv[3], "r");
double intervalTime = 0.05;
char intervalLimitString[20], valueString[20], space[1];
double intervalLimit=-1, value=0;
// to do the linear regression and get the factor
double prevValue = 0;
double slope = 0;
double intercept = 0;
double factor = 1;
double current=0;
while (framesread == 1) {
if (!feof(file)) {
if(current/44100 >= intervalLimit ) {
if(fgets(intervalLimitString, 10, file) != NULL) {
intervalLimit = strtod(intervalLimitString, NULL);
}
if(fgets(space, 1, file) != NULL) {}
if(fgets(valueString, 12, file) != NULL) {
prevValue = value;
value = strtod(valueString, NULL);
slope = (value-prevValue)/intervalTime;
intercept = prevValue;
}
}
factor = ((double)(totalread%((int)(44100*intervalTime)))/44100)*slope + intercept;
frame[0] = frame[0]*factor;
frame[1] = frame[1]*factor;
int tt = totalread%2205;
// printf("time: %d \t value: %f \t slope %f \t intercep %f \t will multiply by: %f\n", tt, value, slope, intercept, factor);
}
current++;
totalread++;
if(psf_sndWriteFloatFrames(ofd,frame,1) != 1){
printf("Error writing to outfile\n");
error++;
break;
}
framesread = psf_sndReadFloatFrames(ifd,frame,1);
}
break;
}
/*
* add up sine waves
*/
case 5: {
FILE *file;
file = fopen(argv[2], "r");
char sineAmplitudeString[20], sineFrequencyString[20], space[1];
double sineAmplitude, sineFrequency;
double sineAmplitudes[20], sineFrequencies[20];
int currentIndex = 0;
int arraySize=0;
while (!feof(file)) {
if(fgets(sineFrequencyString, 5, file) != NULL) {
sineFrequency = strtod(sineFrequencyString, NULL);
if(sineFrequency==-1)
break;
else {
sineFrequencies[currentIndex] = sineFrequency;
if(fgets(space, 1, file) != NULL) {}
if(fgets(sineAmplitudeString, 20, file) != NULL) {
sineAmplitude = strtod(sineAmplitudeString, NULL);
sineAmplitudes[currentIndex] = sineAmplitude;
currentIndex++;
}
}
}
}
arraySize = currentIndex;
currentIndex = 0;
int i;
for(i=0; i<arraySize; i++) {
printf(" %f %f \n", sineAmplitudes[i], sineFrequencies[i]);
}
// do i assume it is 0.05?
double intervalTime = 0.05;
char intervalLimitString[20], valueString[20];
double intervalLimit=-1, value=0;
// to do the linear regression and get the factor
double prevValue = 0;
double slope = 0;
double intercept = 0;
double factor = 1;
double current=0;
while(!feof(file)){
if(current/44100 >= intervalLimit ) {
if(fgets(intervalLimitString, 10, file) != NULL) {
intervalLimit = strtod(intervalLimitString, NULL);
}
if(fgets(space, 1, file) != NULL) {}
if(fgets(valueString, 12, file) != NULL) {
prevValue = value;
value = strtod(valueString, NULL);
slope = (value-prevValue)/intervalTime;
intercept = prevValue;
}
}
factor = ((double)((int)current%((int)(44100*intervalTime)))/44100)*slope + intercept;
printf("%f\n", factor);
// compute frame[1] and frame[2]
frame[0] = 0;
int i;
for(i=0; i<arraySize; i++) {
frame[0] += sineAmplitudes[i]*sin(2*M_PI*current*sineFrequencies[i]/44100);
}
frame[0] = frame[0]*factor;
frame[1] = frame[0];
if(psf_sndWriteFloatFrames(ofd,frame,1) != 1){
printf("Error writing to outfile\n");
error++;
break;
}
current++;
}
break;
}
default:
printf("default case");
break;
}
if(framesread < 0) {
printf("Error reading infile. Outfile is incomplete.\n");
error++;
}
else {
if (atoi(argv[1])<3) {
printf("Done. %ld sample frames copied to %s\n",totalread,argv[3]);
}
else if (atoi(argv[1])==4) {
printf("Done. %ld sample frames copied to %s\n",totalread,argv[4]);
}
}
/* report PEAK values to user */
if(psf_sndReadPeaks(ofd,peaks,NULL) > 0){
long i;
double peaktime;
printf("PEAK information:\n");
for(i=0;i < props.chans;i++){
peaktime = (double) peaks[i].pos / (double) props.srate;
printf("CH %ld:\t%.4f at %.4f secs\n", i+1, peaks[i].val, peaktime);
}
}
/* do all cleanup */
exit: if(ifd >= 0)
psf_sndClose(ifd);
if(ofd >= 0)
psf_sndClose(ofd);
if(frame)
free(frame);
if(peaks)
free(peaks);
psf_finish();
return error;
}