-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracksplit.c
520 lines (448 loc) · 18.9 KB
/
tracksplit.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/***************************************************************************
tracksplit.c - description
-------------------
begin : Mon Aug 27 2001
copyright : (C) 2001 by Jack Cox
email : [email protected]
$Log: tracksplit.c,v $
Revision 1.9 2001/09/21 19:41:04 jack_cox
Fixed bug with parsing of command line options. Doooh!
Revision 1.8 2001/09/21 19:01:22 jack_cox
Fixed a bug where an out of place byte in the wav file throws off the parser
Revision 1.7 2001/08/30 03:50:03 jack_cox
A few more comments and optimizations
Revision 1.6 2001/08/30 03:13:47 jack_cox
Added $Log: tracksplit.c,v $
Added Revision 1.9 2001/09/21 19:41:04 jack_cox
Added Fixed bug with parsing of command line options. Doooh!
Added
Added Revision 1.8 2001/09/21 19:01:22 jack_cox
Added Fixed a bug where an out of place byte in the wav file throws off the parser
Added
Added Revision 1.7 2001/08/30 03:50:03 jack_cox
Added A few more comments and optimizations
Added directive
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#define _GNU_SOURCE
#include <getopt.h>
#include "tracksplit.h"
_i32 minTrack = 300; /* minimum seconds in a track */
char *fileMask = "Track%02d.wav"; /* sprintf mask for file name */
_i32 threshold = 500; /* low volume indicative of silence */
_i32 gapLength = 3; /* seconds of silence indicates gap */
_i32 channelSplit=FALSE;
char *inFilename = NULL;
FILE *inFile = NULL;
InputDesc in;
WAVFile trackFiles[99];
_i32 trackCount = 0;
/************************************************************
* int getFormat -- Read and parse a format tag from a WAV file.
*
************************************************************/
_i32 getFormat(FILE *f, fmtChunk *fc)
{
fread(&fc->formatTag, sizeof fc->formatTag, 1, f);
if (fc->formatTag != 1)
{ /* cannot handle compression */
fprintf(stderr, "Cannot handle compressed files\n");
return(1);
}
fread(&fc->channels, sizeof fc->channels, 1, f);
if (fc->channels > 2)
{ /* cannot handle above stereo */
fprintf(stderr, "Cannot do more than 2 channels\n");
exit(2);
}
fread(&fc->samplesPerSec , sizeof fc->samplesPerSec, 1, f);
fread(&fc->avgBytesPerSec, sizeof fc->avgBytesPerSec, 1, f);
fread(&fc->blockAlign, sizeof fc->blockAlign, 1, f);
fread(&fc->bitsPerSample, sizeof fc->bitsPerSample, 1, f);
in.f = f;
in.frameSize = fc->blockAlign;
in.sampleSize = fc->bitsPerSample;
in.channels = fc->channels;
in.samplesPerSec = fc->samplesPerSec;
in.minTrackSamples = in.samplesPerSec * minTrack;
in.gapLengthSamples = in.samplesPerSec * gapLength;
in.avgBytesPerSec = fc->avgBytesPerSec;
fprintf(stderr, "\tSamples/Sec = %u\n", fc->samplesPerSec);
fprintf(stderr, "\tBytes/Sec = %u\n", fc->avgBytesPerSec);
fprintf(stderr, "\tFrame = %u\n", fc->blockAlign);
fprintf(stderr, "\tBits/Sample = %u\n", fc->bitsPerSample);
return(0);
}
/***************************************************************
* int closeTrack -- Complete a track file. Writes the various
* chunk information then closes the file
*
***************************************************************/
_i32
closeTrack(WAVFile *wf) {
_i32 rcs = wf->bytesWritten+sizeof(fmtChunk)+8;
#ifdef DEBUG
fprintf(stderr, "closeTrack: entering, name=%s\n", wf->filename);
#endif
/* record data chunk size */
fseek(wf->f, wf->dataOffset, SEEK_SET);
fwrite(&wf->bytesWritten, sizeof wf->bytesWritten, 1, wf->f);
/* record RIFF chunk size */
fseek(wf->f, wf->RIFFOffset, SEEK_SET);
fwrite(&rcs, sizeof rcs, 1, wf->f);
fclose(wf->f);
#ifdef DEBUG
fprintf(stderr, "closeTrack: exiting\n");
#endif
return(TRUE);
}
/*************************************************************
* int copyData -- copy a chunk of the input wave file to the
* output wave file. Optionally look for quiet spots where
* the file should be split. This is the most time critical
* piece of the code.
*
*************************************************************/
_i32
copyData(InputDesc *in, WAVFile *wf, _i32 framesToCopy, _i16 ScanForGaps) {
_u8 ucSample[2];
_i16 ssSample[2];
void *sample;
_i32 lastLoud = 0;
_i32 framesDone = 0;
_i32 frameSize = in->frameSize;
_i32 gapLengthSamples = in->gapLengthSamples;
_i32 mode = 0;
// make this decision once since it won't change for this run
if ((in->sampleSize == 16) && (in->channels==1)) {
mode = 1;
} else if ((in->sampleSize == 16) && (in->channels==2)) {
mode = 2;
}
#ifdef DEBUG
fprintf(stderr, "copyData: Entring\n");
fprintf(stderr, "copyData: offset: %d framesToCopy: %d, scan=%s\n",
in->curFrame, framesToCopy,
(ScanForGaps ? "TRUE" : "FALSE"));
#endif
if (in->sampleSize == 16)
sample = (void *) ssSample;
else
sample = (void *) ucSample;
/* do bytesToCopy is done */
while ((in->curFrame < in->frameEnd) && (framesDone < framesToCopy))
{
/* read a sample */
fread(sample, frameSize, 1, in->f);
in->curFrame++;
/* write sample */
fwrite(sample, frameSize, 1, wf->f);
if (channelSplit) {
fwrite(sample, frameSize, 1, wf->f);
}
/* if scanning */
if (ScanForGaps) {
if (mode == 1) {
if (abs(ssSample[0]) > threshold) {
lastLoud = framesDone;
} else if (lastLoud < ((framesDone) - gapLengthSamples)) {
framesDone++;
break;
}
} else if (mode == 2) {
if ((abs(ssSample[0]) > threshold) || (abs(ssSample[1]) > threshold)) {
lastLoud = framesDone;
} else if (lastLoud < ((framesDone) - gapLengthSamples)) {
framesDone++;
break;
}
}
}
framesDone++;
}
wf->bytesWritten += framesDone * (in->frameSize * (channelSplit ? 2 : 1)) ;
#ifdef DEBUG
fprintf(stderr, "copyData: Leaving\n");
fprintf(stderr, "copyData: framesDone: %d framesToCopy: %d\n",
framesDone, framesToCopy);
#endif
return(TRUE);
}
/***********************************************
* newTrack -- create a new track file. Copy the
* first 'minimum-track' length seconds into the
* file
*
***********************************************/
_i32
newTrack(InputDesc *in) {
#ifdef DEBUG
fprintf(stderr, "newTrack: entering, track=%d\n", trackCount+1);
#endif
// oops too many tracks for a cd audio
if (trackCount==99) {
return(FALSE);
}
// make space for the filename
trackFiles[trackCount].filename = malloc(255);
trackCount++;
// make the file name
sprintf(trackFiles[trackCount-1].filename, fileMask, trackCount);
// initialize the track file
trackFiles[trackCount-1].f = fopen(trackFiles[trackCount-1].filename, "wb");
trackFiles[trackCount-1].RIFFOffset = 0;
trackFiles[trackCount-1].bytesWritten = 0;
/* write RIFF */
{
RIFFChunk rc;
rc.header.id = RIFFCHUNK;
rc.header.chunkSize = 0;
memcpy(&rc.id2, "WAVE" , 4);
fwrite(&rc, sizeof(rc), 1, trackFiles[trackCount-1].f);
trackFiles[trackCount-1].RIFFOffset = 4;
}
{
fmtChunk newfc;
trackFiles[trackCount-1].fmtOffset=ftell(trackFiles[trackCount-1].f);
newfc.header.id = FMTCHUNK;
newfc.header.chunkSize = sizeof(fmtChunk)-8;
newfc.formatTag = 1;
if (!channelSplit) { // single channel
newfc.channels = in->channels;
newfc.samplesPerSec = in->samplesPerSec;
newfc.avgBytesPerSec = in->avgBytesPerSec;
newfc.blockAlign = in->frameSize;
} else { // dual channel
newfc.channels = 2;
newfc.samplesPerSec = in->samplesPerSec;
newfc.avgBytesPerSec = in->sampleSize*2*in->samplesPerSec;
newfc.blockAlign = in->frameSize*2;
}
newfc.bitsPerSample = in->sampleSize;
fwrite(&newfc, sizeof(newfc), 1, trackFiles[trackCount-1].f);
}
/* write fmt tag */
{
chunkHeader ch;
trackFiles[trackCount-1].dataOffset=ftell(trackFiles[trackCount-1].f)+4;
memcpy(&ch.id, "data", 4);
ch.chunkSize = 0;
fwrite(&ch, sizeof(ch), 1, trackFiles[trackCount-1].f);
}
/* write data tag */
copyData(in, &trackFiles[trackCount-1],
in->minTrackSamples, FALSE);
#ifdef DEBUG
fprintf(stderr, "newTrack: exiting, track=%d\n", trackCount);
#endif
return(TRUE);
}
/**********************************************
* int processData -- work through the file starting
* tracks, finding gaps, and making new tracks
*
*
***********************************************/
_i32
processData(InputDesc *in)
{
#ifdef DEBUG
fprintf(stderr, "processData: entering\n");
#endif
/* start the first track automatically */
if (newTrack(in)) {
// start copying and scanning for gaps until you find a gap or reach the end
while (copyData(in, &trackFiles[trackCount-1], in->frameEnd - in->curFrame , TRUE)) {
#ifdef DEBUG
fprintf(stderr, "processData: start of while loop\n");
#endif
// close the track
closeTrack(&trackFiles[trackCount-1]);
// if the last frame has been processed then break out
if ((in->frameEnd - in->curFrame) == 0) {
break;
}
// if more to go then start a new track
if (in->curFrame < in->frameEnd) {
if (!newTrack(in)) {
return(FALSE); /* error in newTrack */
}
if (in->curFrame >= in->frameEnd) {
closeTrack(&trackFiles[trackCount-1]);
return(TRUE);
}
}
}
}
}
/*****************************************************************
* int init -- read the arguments
*
*****************************************************************/
_i32
init (_i32 argc, char *argv[]) {
_i32 c;
_i32 usage = 0;
static struct option long_options[] =
{
{"minimum-track", 1, 0, 0},
{"file", 1, 0, 0},
{"gap-length", 1, 0, 0},
{"threshold", 1, 0, 0},
{"channel-split", 0,0,0},
{"input", 1, 0, 0},
{"help", 0, 0, 0},
{0, 0, 0, 0}
};
while ((1) && (!usage)) {
_i32 option_index = 0;
c = getopt_long(argc, argv, "?hm:f:g:t:ci:", long_options, &option_index);
if (c == -1)
break;
switch(c) {
case '?':
case 'h':
usage++;
break;
case 'm' : /* minimum-track */
minTrack = atoi(optarg);
break;
case 'f' : /* file name */
fileMask = (char *) strdup(optarg);
break;
case 'g' : /* gap-length */
gapLength = atoi(optarg);
break;
case 't' : /* silance threshold */
threshold = atoi(optarg);
break;
case 'c' : /* split */
channelSplit=TRUE;
break;
case 'i' : /* input */
inFilename = strdup(optarg);
break;
case 0 :
switch(option_index) {
case 0 : /* minimum-track */
minTrack = atoi(optarg);
break;
case 1 : /* file name */
fileMask = (char *) strdup(optarg);
break;
case 2 : /* gap-length */
gapLength = atoi(optarg);
break;
case 3 : /* silance threshold */
threshold = atoi(optarg);
break;
case 4 : /* split */
channelSplit=TRUE;
break;
case 5 : /* input */
inFilename = strdup(optarg);
break;
default :
usage++;
break;
}
break;
default :
usage++;
break;
}
}
if (usage) { /* oops, user error */
fprintf(stderr, "usage: sts -m | --minimum-track <seconds>\tThe minimum size of a track.\n");
fprintf(stderr, "\t\t-f | --file <filename-mask>\tThe file mask to use for output files. Default=TrackXX.wav\n");
fprintf(stderr, "\t\t-g | --gap-length <seconds>\tThe number of seconds of silence to consider a track break.\n");
fprintf(stderr, "\t\t-t | --threshold <amplitude>\tThe highest amplitude value that indicates silence.\n");
fprintf(stderr, "\t\t-i | --input <filename>\tRead from named file rather than stdin\n");
fprintf(stderr, "\t\t-c | --channel-split\tConvert the output from mono input to stereo output tracks while track splitting\n");
fprintf(stderr, "\t\t-h | -? | --help\t\tShow this message\n");
return(FALSE);
}
return(TRUE);
}
_i32
main(_i32 argc, char *argv[]) {
RIFFChunk rc;
fmtChunk fc;
chunkHeader ch;
chunkHeader dc;
_i32 i;
_i32 preReadOffset;
TP05
GV05(sizeof(_u64))
if (!init(argc, argv)) {
return(1);
}
if (inFilename != NULL)
{
inFile = fopen(inFilename, "rb");
} else {
inFile = stdin;
}
do
{
#ifdef DEBUG
fprintf(stderr, "main: @ offset %X\n", ftell(inFile));
#endif
preReadOffset = ftell(inFile);
i = fread((void *)&ch, sizeof(chunkHeader), 1, inFile);
if (i>0)
{
switch (ch.id)
{
case (RIFFCHUNK) :
fprintf(stderr, "Got RIFF chunk, size=%u\n", ch.chunkSize);
fseek(inFile, 4, SEEK_CUR);
break;
case (FMTCHUNK) :
fprintf(stderr, "Got FMT chunk, size=%u\n", ch.chunkSize);
memcpy(&fc, &ch, sizeof(chunkHeader));
if (getFormat(inFile, &fc))
{
exit(1);
}
break;
case (DATACHUNK) :
fprintf(stderr, "Got DATA chunk, size=%u\n", ch.chunkSize);
memcpy(&dc, &ch, sizeof(chunkHeader));
in.dataStart = ftell(in.f);
in.dataEnd = in.dataStart + dc.chunkSize;
in.frameEnd = dc.chunkSize / in.frameSize;
in.curFrame = 0;
/* start processing tracks */
if (!processData(&in)) {
exit(2);
}
break;
case (LISTCHUNK) :
fprintf(stderr, "Got a List Chunk, size=%u\n", ch.chunkSize);
{
_u8 junk[ch.chunkSize];
fread(junk, ch.chunkSize, 1, inFile);
}
break;
default :
fprintf(stderr, "Unknown data @ offset %X\n", ftell(inFile)-sizeof(chunkHeader));
fseek(inFile, preReadOffset+1, SEEK_SET);
break;
}
}
} while (!feof(inFile));
exit(0);
}