forked from tarjanm-movidius/cutmp3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
3237 lines (2821 loc) · 90.1 KB
/
main.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
/*
main.c Cut MP3s framewise, VBR supported!
MP2 files should work since 2.0.4, as well!
The code is somewhat ugly and unsafe, sorry! At least it is fast :-)
(c) Jochen Puchalla 2003-2015 <mail@puchalla-online.de>
*/
#include "cutmp3.h"
/* general buffersize */
#define BUFFER 32000
#define FN_LEN 8191
#define FS_BLOCK_SIZE 4096
/* 10 MB for VBR scanning */
#define SCANAREA 10485759
#ifdef DEBUG
int debug=0; // 1=volume, 2=operation, 5=ID3tags 7=saving+cutting 9=seeksilence
#endif
unsigned int silencelength=1000, silvol=1;
FILE *mp3file, *timefile;
unsigned char begin[BUFFER];
long filesize, dataend, inpoint, outpoint;
unsigned int audiobegin=0;
unsigned int startmins, endmins;
real startsecs, endsecs;
int negstart=1, negend=1; /* start- or endpoint counted from end? */
unsigned int totalmins; /* minutes of total time */
real totalsecs; /* seconds of total time (<60) */
int howlong=1; /* play how many seconds? */
unsigned int fix_secondbyte=0, fix_sampfreq=0, fix_channelmode=7, fix_mpeg=7, fix_frsize=1;
real fix_frametime=0;
int vbr=0, a_b_used=0;
real avbr, msec; /* average bitrate and one millisecond in relation to avbr */
long totalframes;
int usefilename=0;
char *filename;
char *forcedname;
char *userin;
char *userout;
int livetime=1;
unsigned int mute=0, forced_file=0, overwrite=0, forced_prefix=0;
int copytags=0, nonint=0;
long silfactor=15; /* silence already longer than wanted by this factor */
int stdoutwrite=0;
long framenumber=0, framecount=0;
real intime=0; /* time of inpoint */
real outtime=0; /* time of outpoint */
real endtime=0; /* total time of MP3 in milliseconds */
int no_tags=0;
unsigned int card=1;
signed int genre=-1;
long *framepos1000 = NULL; // byte position of every 1000th frame
unsigned char *id3v2 = NULL; // space to copy id3v2 tag
unsigned char id3v1[128]; // space to copy id3v1 tag
unsigned int minorv=0; // id3v2 minor version, mostly 3, rarely 4, 2 is obsolete
char artist[255]="";
char album[255]="";
char year[255]="";
char track[255]="";
char comment[255]="";
/* title is used to determine whether we name the file "result000x.mp3"
or "artist - title.mp3", so leave it empty here! */
char title[255]="";
unsigned char ttable[BUFFER];
FILE *tblfile;
char tblname[17] = "/tmp/tableXXXXXX";
FILE *logfile;
char logname[21] = "/tmp/mpg123logXXXXXX";
int fdlog=-1;
void exitseq(int foobar)
{
free(framepos1000);
free(id3v2);
remove(logname);
exit(foobar); /* now really exit */
(void)ht;
(void)htc;
}
void usage(char *text)
{
printf("\ncutmp3 version %s \n", VERSION);
printf("\nUsage: cutmp3 -i file.mp3 [-a inpoint] [-b outpoint] [-f timetable]");
printf("\n [-o outputprefix] [-c] [-C] [-k] [-q]\n");
printf("\n cutmp3 -i file.mp3 -a 0:37 -b 3:57 copies valid data from 0:37 to 3:57");
printf("\n cutmp3 -i file.mp3 -f timetable copies valid data described in timetable");
printf("\n cutmp3 -i file.mp3 -o song writes the output to song0001.mp3, song0002.mp3,...");
printf("\n cutmp3 -i file.mp3 -k appends 0001.mp3, etc. to original filename");
printf("\n cutmp3 -i file.mp3 -O song.mp3 writes the output to song.mp3");
printf("\n cutmp3 -i file.mp3 -O - writes the output to STDOUT");
printf("\n cutmp3 -i file.mp3 -d 2 use second sound card");
printf("\n cutmp3 -i file.mp3 -s 0 no maximum silence length");
printf("\n cutmp3 -I file.mp3 [-F] prints file information [in raw mode]\n\n");
#ifdef DEBUG
printf(" cutmp3 -D <n> Show debug prints: 0:none (default)\n");
printf(" 1:volume, 2:operation, 5:ID3tags, 7:saving+cutting, 9:seeksilence\n\n");
#endif
if (text && text[0]!='\0') printf("%s\n\n",text);
}
void help(void)
{
printf("\n\ncutmp3 version %s \n", VERSION);
printf("\n Seek with 1234567890,.\n");
printf(" Mark beginning with 'a', go there with 'A'\n");
printf(" Mark end with 'b', go there with 'B'\n");
printf(" Save selection with 's'.\n");
printf(" Save selection with ID3-tag with 't'.\n\n");
printf(" Seek to next ID3-tag with 'T'\n");
printf(" Seek to end of next silence with 'p'\n");
printf(" Seek to beginning of next silence with 'P'\n");
printf(" increase volume of silence with '+'.\n");
printf(" decrease volume of silence with '-'.\n");
printf(" increase length of silence with 'm'.\n");
printf(" decrease length of silence with 'n'.\n");
printf(" increase playback length with 'M'.\n");
printf(" decrease playback length with 'N'.\n");
printf(" 'o' turns on overwriting for next time.\n");
printf(" 'i' shows file info.\n\n");
printf(" Replay with 'r'.\n");
printf(" Toggle playing up to / from location with 'R'.\n");
printf(" Toggle sound on/off with '#'.\n");
printf(" Toggle live time on/off with 'l'.\n");
printf(" Write configuration file with 'S'.\n\n");
printf(" Press 'q' to quit.\n\n");
printf(" HINTS: 'w' is a shortcut for 'bsa'.\n");
printf(" 'W' is a shortcut for 'bta'.\n");
return;
}
/* Which MPEG-Type ? */
/* Type is in bits nr. 11&12 */
unsigned int mpeg(unsigned char mpegnumber)
{
switch (mpegnumber & (0x3 << 3))
{
case (0<<3): return 3;
case (3<<3): return 1;
case (2<<3): return 2;
}
DBGPRINT(2, "%s(0x%02hhX) Warning: reserved value\n", __func__, mpegnumber);
return 0;
}
unsigned int crc(unsigned char crcbyte)
{
if (crcbyte & 0x1)
return 0;
else
return 1;
}
unsigned int sampfreq(unsigned char secondbyte, unsigned char thirdbyte)
{
switch (mpeg(secondbyte))
{
case 1:
switch(thirdbyte & (0x3<<2))
{
case (0<<2): return 44100;
case (1<<2): return 48000;
case (2<<2): return 32000;
}
case 2:
switch(thirdbyte & (0x3<<2))
{
case (0<<2): return 22050;
case (1<<2): return 24000;
case (2<<2): return 16000;
}
case 3: // MPEG2.5
switch(thirdbyte & (0x3<<2))
{
case (0<<2): return 11025;
case (1<<2): return 12000;
case (2<<2): return 8000;
}
}
/* In case of an error, sampfreq=1 is returned */
DBGPRINT(2, "%s(0x%02hhX, 0x%02hhX) Warning: reserved value\n", __func__, secondbyte, thirdbyte);
return 1;
}
unsigned int channelmode(unsigned char fourthbyte)
{
// printf(" channelmode()");
return fourthbyte>>6;
/* result: 0=Stereo
1=Joint-Stereo
2=Dual-Channel
3=Mono */
}
unsigned int channels(unsigned char fourthbyte)
{
if (channelmode(fourthbyte)==3) return 1;
else return 2;
}
#define INVALID_HDR { 0x00, 0x08, 0xFC, 0x02 }
inline unsigned int is_hdr(unsigned char *buf)
{
/* http://www.mp3-tech.org/programmer/frame_header.html */
if (buf[0] == 255
&& (buf[1] & (0x7<<5)) == (7<<5)
&& (buf[1] & (0x3<<1)) != 0
#if 0 /* Strict sample rate check */
&& sampfreq(buf[1], buf[2]) == fix_sampfreq
#else
&& (buf[1] & (0x3<<3)) != (1<<3)
&& (buf[2] & (0x3<<2)) != (3<<2)
#endif
&& (buf[2] & (0xF<<4)) != (0xF<<4)
&& (buf[3] & 0x3) != 2) return 1;
else return 0;
}
unsigned int bitrate(unsigned char secondbyte, unsigned char thirdbyte)
{
unsigned char bitratenumber;
bitratenumber = thirdbyte >> 4;
switch( layer(secondbyte) ) {
case 3: /* MPEG Layer3 */
switch (mpeg(secondbyte)) {
case 1: /* MPEG1 */
switch (bitratenumber) {
case 1: return 32;
case 2: return 40;
case 3: return 48;
case 4: return 56;
case 5: return 64;
case 6: return 80;
case 7: return 96;
case 8: return 112;
case 9: return 128;
case 10: return 160;
case 11: return 192;
case 12: return 224;
case 13: return 256;
case 14: return 320;
}
break;
case 2: /* MPEG2 */
case 3: /* MPEG2.5 */
switch (bitratenumber) {
case 1: return 8;
case 2: return 16;
case 3: return 24;
case 4: return 32;
case 5: return 40;
case 6: return 48;
case 7: return 56;
case 8: return 64;
case 9: return 80;
case 10: return 96;
case 11: return 112;
case 12: return 128;
case 13: return 144;
case 14: return 160;
}
break;
}
break;
case 2: /* MPEG Layer2 */
switch (mpeg(secondbyte)) {
case 1: /* MPEG1 */
switch (bitratenumber) {
case 1: return 32;
case 2: return 48;
case 3: return 56;
case 4: return 64;
case 5: return 80;
case 6: return 96;
case 7: return 112;
case 8: return 128;
case 9: return 160;
case 10: return 192;
case 11: return 224;
case 12: return 256;
case 13: return 320;
case 14: return 384;
}
break;
case 2: /* MPEG2 */
case 3: /* MPEG2.5 */
switch (bitratenumber) {
case 1: return 8;
case 2: return 16;
case 3: return 24;
case 4: return 32;
case 5: return 40;
case 6: return 48;
case 7: return 56;
case 8: return 64;
case 9: return 80;
case 10: return 96;
case 11: return 112;
case 12: return 128;
case 13: return 144;
case 14: return 160;
}
break;
}
break;
case 1: /* MPEG Layer1 */
if (mpeg(secondbyte)==1) /* MPEG1 */
switch (bitratenumber)
{
case 1: return 32;
case 2: return 64;
case 3: return 96;
case 4: return 128;
case 5: return 160;
case 6: return 192;
case 7: return 224;
case 8: return 256;
case 9: return 288;
case 10: return 320;
case 11: return 352;
case 12: return 384;
case 13: return 416;
case 14: return 448;
}
break;
}
/* In case of an error, bitrate=1 is returned */
DBGPRINT(2, "%s(0x%02hhX, 0x%02hhX) Warning: reserved value\n", __func__, secondbyte, thirdbyte);
return 1;
}
unsigned int layer(unsigned char secondbyte)
{
switch (secondbyte & (0x3<<1))
{
case (1<<1): return 3;
case (2<<1): return 2;
// case (2<<1): {printf("\nFile is an MP2, not am MP3.\n"); exitseq(99);}
case (3<<1): return 1;
}
/* In case of an error, layer=0 is returned */
DBGPRINT(2, "%s(0x%02hhX) Warning: reserved value\n", __func__, secondbyte);
return 0;
}
unsigned int paddingbit(unsigned char thirdbyte)
{
if (thirdbyte & 0x1<<1)
return 1;
else
return 0;
}
unsigned int frame_sz(unsigned char *buf)
{
unsigned int br, sf, mfactor;
if ((br = bitrate(buf[1], buf[2])) == 1) return 1;
if ((sf = sampfreq(buf[1], buf[2])) == 1) return 1;
if (mpeg(buf[1]) == 1)
mfactor = 2;
else
mfactor = 1;
return (mfactor * 72000 * br / sf) + paddingbit(buf[2]);
}
unsigned int framesize(unsigned char secondbyte,unsigned char thirdbyte,unsigned char fourthbyte)
{
int br=bitrate(secondbyte,thirdbyte);
int mfactor=2;
int sf=sampfreq(secondbyte,thirdbyte);
if (mpeg(secondbyte)!=1) mfactor=1;
if ((br!=1) && (sf!=1)) return (mfactor*72000*br/sf)+paddingbit(thirdbyte);
else return 1;
}
/* find next frame at seekpos */
long nextframe(long seekpos)
{
unsigned char hdrbuf[4] = INVALID_HDR;
unsigned int framesz;
if (seekpos >= filesize) return filesize;
fseek(mp3file, seekpos, SEEK_SET);
/* if seekpos is a header and framesize is valid, jump to next header via framesize, else move on one byte */
if(fread(hdrbuf, 1, 4, mp3file) < 4) return seekpos;
if (is_hdr(hdrbuf))
seekpos = seekpos + frame_sz(hdrbuf);
else
seekpos = seekpos + 1;
/* find next possible header, start right at seekpos */
if (seekpos >= filesize) return filesize;
fseek(mp3file, seekpos, SEEK_SET);
while(seekpos < filesize)
{
hdrbuf[0] = fgetc(mp3file); /* get next byte */
if (hdrbuf[0] == 255)
{
if ((framesz = fread(&hdrbuf[1], 1, 3, mp3file)) == 3 && is_hdr(hdrbuf))
break;
else
fseek(mp3file, -1l * framesz, SEEK_CUR);
}
seekpos++;
}
/* also check next frame if possible */
if (seekpos + (framesz = frame_sz(hdrbuf)) + 4 < filesize)
{
fseek(mp3file, seekpos + framesz, SEEK_SET);
if(fread(hdrbuf, 1, 4, mp3file) < 4) return seekpos;
if (!is_hdr(hdrbuf))
seekpos = nextframe(seekpos + 1);
}
// TODO do we guarantee file's at seekpos? If so, why not at the beginning too
// fseek(mp3file, seekpos, SEEK_SET);
return seekpos;
}
/* find previous frame at seekpos */
long prevframe(long seekpos)
{
unsigned char hdrbuf[4] = INVALID_HDR;
long nextheaderpos;
if (seekpos <= audiobegin + 1) return audiobegin;
/* find next header, start right at seekpos */
// nextheaderpos = nextframe(seekpos);
fseek(mp3file, seekpos, SEEK_SET);
if(fread(hdrbuf, 1, 4, mp3file) < 4) return seekpos;
if (!is_hdr(hdrbuf))
nextheaderpos = nextframe(seekpos + 1);
else
nextheaderpos = seekpos;
/* rewind to previous header and check if nextheader is more than framesize bytes further */
while(--seekpos > audiobegin) // if seekpos == audiobegin we'll just return that
{
fseek(mp3file, seekpos, SEEK_SET);
hdrbuf[0] = fgetc(mp3file);
if (hdrbuf[0] == 255)
{
if (fread(&hdrbuf[1], 1, 3, mp3file) == 3 && is_hdr(hdrbuf) \
&& seekpos + frame_sz(hdrbuf) <= nextheaderpos) /* previous frame found */
break;
}
}
// TODO do we guarantee file's at seekpos? If so, why not at the beginning too
// fseek(mp3file, seekpos, SEEK_SET);
return seekpos;
}
void zaptitle (void)
{
title[0]='\0';
}
/*
time of frame in milliseconds is: framesize*8/bitrate == 1152000/sampfreq !
because bitrate=128 means 128000bits/s
*/
/* scan whole file, can take seconds */
void scanframes()
{
long pos;
unsigned char hdrbuf[4] = INVALID_HDR;
unsigned int count, ii;
framepos1000[0] = audiobegin;
pos = nextframe(audiobegin);
count = 1, ii = 1;
while (pos < filesize) {
fseek(mp3file, pos, SEEK_SET);
if (fread(hdrbuf, 1, 4, mp3file) < 4 || !is_hdr(hdrbuf))
break;
pos += frame_sz(hdrbuf);
count++;
if (!(count % 1000))
framepos1000[ii++] = pos;
}
if (pos < filesize) {
fseek(mp3file, -4l, SEEK_CUR);
if (fgetc(mp3file)!='T' || fgetc(mp3file)!='A' || fgetc(mp3file)!='G' ) {
printf("\n%s(): rerunning scan with paranoia due to error at %ld\n", __func__, pos);
count = 0, ii = 1;
while (pos < filesize)
{
pos = nextframe(pos);
count++;
if (!(count % 1000))
framepos1000[ii++] = pos;
}
}
}
totalframes = count;
framepos1000[ii] = filesize + 1; // TODO why?
return;
}
/* get frame number from position, quite slow */
long pos2frame (long seekpos)
{
long pos;
long count=0,i=0;
if (seekpos<=audiobegin) return 0;
if (seekpos>dataend) return totalframes;
pos=audiobegin;
while (pos>0 && pos<seekpos) // go forward until too far
{
i++;
pos=framepos1000[i];
}
while (pos>=seekpos) // go back to one before
{
i--;
pos=framepos1000[i];
}
count=1000*i; // we know at how many thousands of frames we are
while (pos<seekpos) // find exact frame
{
pos=nextframe(pos);
// printf(".");
count++;
}
return count;
}
/* get time from position, quite slow */
real pos2time (long seekpos)
{
long framenr;
framenr=pos2frame(seekpos);
return (real)framenr*fix_frametime;
}
/* get whole minutes from position, quite slow */
unsigned int pos2mins (long seekpos)
{
long framenr;
framenr=pos2frame(seekpos);
return (real)framenr*fix_frametime/60000;
}
/* returns seconds without minutes to display mm:ss.ss, quite slow */
real pos2secs (long seekpos)
{
long framenr;
real msecs,temp;
int mins;
framenr=pos2frame(seekpos);
msecs=(real)framenr*fix_frametime;
mins=msecs/60000;
temp=(msecs/1000-(mins*60));
if (temp>59.99) return 59.99; /* 59.997 would be displayed as 60:00 without this */
return temp;
}
/* time in milliseconds from frame number, fast! */
real frame2time (long framenr)
{
if (framenr>totalframes) framenr=totalframes;
return (real)framenr*fix_frametime;
}
/* whole minutes from frame number, fast! */
unsigned int frame2mins (long framenr)
{
return frame2time(framenr)/(real)60000;
}
/* seconds over whole minutes from frame number, fast! */
real frame2secs (long framenr)
{
real temp;
temp=frame2time(framenr)/1000 - frame2mins(framenr)*60;
if (temp>59.99) return 59.99; /* 59.997 would be displayed as 60:00 without this */
return temp;
}
/* position from frame number, quite fast */
long frame2pos (long framenr)
{
long pos,i;
int tsd;
tsd=framenr/1000; /* start search at 1000s frames */
// printf(" tsd=%i \n",tsd);
pos=framepos1000[tsd];
for (i=0;i<framenr-tsd*1000;i++) pos=nextframe(pos);
return pos;
}
unsigned long fforward (long seekpos,long skiptime)
{
long skipframes=0;
if (frame2time(framenumber)+skiptime>=endtime)
{
framenumber=totalframes;
return filesize;
}
// go forward skiptime/fix_frametime +1 headers
skipframes=(real)skiptime/(real)fix_frametime+(real)1;
framenumber=framenumber+skipframes;
return frame2pos(framenumber);
}
unsigned long frewind (long seekpos,long rewtime)
{
long rewframes=0;
if (rewtime>=frame2time(framenumber))
{
framenumber=0;
return audiobegin;
}
// go back rewtime/fix_frametime +1 headers
rewframes=(real)rewtime/(real)fix_frametime+(real)1;
framenumber=framenumber-rewframes;
return frame2pos(framenumber);
}
void copyfile(FILE *outfile, FILE *infile, size_t nbyte)
{
char *buf = malloc(FS_BLOCK_SIZE);
ssize_t read_sz, write_sz;
while(nbyte) {
read_sz = fread(buf, 1, nbyte < FS_BLOCK_SIZE ? nbyte : FS_BLOCK_SIZE, infile);
if(read_sz < 0) {perror("copyfile read()"); exitseq(10);}
nbyte -= read_sz;
while(read_sz>0) {
write_sz = fwrite(buf, 1, read_sz, outfile);
if(write_sz < 0) {perror("copyfile write()"); exitseq(10);}
read_sz -= write_sz;
}
}
free(buf);
}
/* Determination of average VBR bitrate */
real avbitrate(void)
{
real frmcount=0, bitratesum=0;
long pos=audiobegin;
unsigned char hdrbuf[4] = INVALID_HDR;
real thisfrsize=1, thisbitrate=0;
// printf(" avbitrate");
fseek(mp3file, audiobegin, SEEK_SET);
while(pos < filesize-4 && pos < SCANAREA)
{
// printf("\navbitrate in loop, pos=%ld, filesize=%ld\n",pos,filesize);
if (pos>=filesize-1 || pos>SCANAREA) break;
hdrbuf[0]=fgetc(mp3file);
// printf("\na=%u\n",a);
if (hdrbuf[0]==255)
{
if(fread(&hdrbuf[1], 1, 3, mp3file) < 3) break;
thisfrsize = frame_sz(hdrbuf); /* cache framesize(b,c,d) */
// if (isheader(b,c,d)==1 && thisfrsize!=1 && sampfreq(b,c)!=1) /* next frame found */
if (thisfrsize!=1 && sampfreq(hdrbuf[1], hdrbuf[2])==fix_sampfreq) /* next frame found */
{
pos += thisfrsize; /* adjust pos to actual SEEK_SET */
fseek(mp3file, pos, SEEK_SET); /* skip framesize bytes */
frmcount++; /* one more frame */
thisbitrate=bitrate(hdrbuf[1], hdrbuf[2]); /* cache bitrate(b,c,d) */
bitratesum += thisbitrate;
// printf("\nbitratesum:%ld frmcount:%ld ",bitratesum,frmcount);
if (bitratesum/frmcount!=thisbitrate && vbr==0) /* VBR check */
{
vbr=1; /* if file is VBR */
// printf("\nnoVBR: bitrate=%.0f at %ld Bytes \n",thisbitrate,pos);
}
}
else fseek(mp3file, ++pos, SEEK_SET); /* rewind to next byte for if (a==255) */
}
}
if (bitratesum==0 || frmcount==0) return 0;
return bitratesum/frmcount;
}
/* This function shall return the volume (energy level)
of one frame. Taken from mpglib and mpcut. */
unsigned int volume(long playpos)
{
unsigned char a,b,c,d;
unsigned int level, framenum=4;
unsigned int i, frsize;
long pos;
FILE *volfile;
static char volname[] = "/dev/shm/volXXXXXX";
int fd=-1;
DBGPRINT(1, "\nvolume(): before prevframe(playpos)");
for (i=0; i<framenum ; i++) playpos=prevframe(playpos); /* step back framenum frames */
DBGPRINT(1, "\nvolume(): after %ux prevframe %ld \n",framenum,playpos);
if ((fd = mkstemp(volname)) == -1 || (volfile = fdopen(fd, "w+b")) == NULL)
{
DBGPRINT(1, "\nvolume(): /dev/shm not writable\n");
strcpy(volname, "/tmp/volXXXXXX");
if ((fd = mkstemp(volname)) == -1 || (volfile = fdopen(fd, "w+b")) == NULL)
{
/* open temp.vol.file read-writable zero length */
perror("\ncutmp3: failed writing temporary volume mp3 in /tmp");
exitseq(6);
}
}
frsize=1;
pos=playpos-1;
/* Copy 2*framenum valid frames to volfile */
for (framecount=0; framecount < 2*framenum ; framecount++)
{
while (frsize==1)
{
/* seek next frame */
pos=nextframe(pos);
DBGPRINT(1, "\nvolume(): pos=%ld\n",pos);
fseek(mp3file, pos, SEEK_SET);
a=fgetc(mp3file);
if(a==255) {
b=fgetc(mp3file);
c=fgetc(mp3file);
d=fgetc(mp3file);
frsize=framesize(b,c,d);
}
}
DBGPRINT(1, "\nvolume(): frsize=%u\n",frsize);
/* write this frame */
fseek(mp3file, pos, SEEK_SET);
for (i=0 ; i<frsize && pos+frsize<filesize ; i++)
{
fputc(fgetc(mp3file),volfile);
pos++;
}
}
/* get energy level */
fseek(volfile, 0, SEEK_SET);
DBGPRINT(1, "volume(): before InitMP3() \n");
InitMP3();
DBGPRINT(1, "volume(): before get_framelevel \n");
level = get_framelevel(volfile,framenum);
DBGPRINT(1, "volume(): after get_framelevel \n");
if (level==MP3_ERR) level=65535;
// if (level<0) level=(-1)*level;
if (level>65535) level=65535;
ExitMP3();
fclose(volfile);
remove(volname);
return level;
}
/* This function returns the position of the end of the next silence */
long seeksilence(long seekpos)
{
unsigned int vol;
long silend, silfirstfound;
// printf(" seeksilence()");
seekpos=fforward(seekpos,silencelength); /* move on */
if (seekpos>=dataend) /* dataend reached? */
{
printf(" seeking at ");
framenumber=totalframes;
printf("\b\b\b\b\b\b\b%4u:%02.0f",frame2mins(framenumber),frame2secs(framenumber));
return filesize;
}
printf(" seeking at ");
printf("\b\b\b\b\b\b\b%4u:%02.0f",frame2mins(framenumber),frame2secs(framenumber));
while (seekpos<dataend)
{
while ((vol=volume(seekpos)) >= silvol)
{
/* look for possible silence every silencelength milliseconds: */
DBGPRINT(9, "\ncandidate seek at %ld volume is %u\n",seekpos,vol);
printf("\b\b\b\b\b\b\b%4u:%02.0f",frame2mins(framenumber),frame2secs(framenumber));
seekpos=fforward(seekpos,silencelength);
framenumber=pos2frame(seekpos);
if (seekpos>=dataend)
{
return filesize; /* dataend reached? */
}
}
/* we found a candidate! */
DBGPRINT(9, "\ncandidate found at %ld volume is %u.\n",seekpos,vol);
// printf("\b\b\b\b\b\b\b%4u:%02.0f",frame2mins(framenumber),frame2secs(framenumber)); unnötig;
silfirstfound=seekpos; /* remember first found silent frame */
framenumber=pos2frame(seekpos);
/* forward to last silent frame */
while ((vol=volume(seekpos)) < silvol && seekpos<dataend)
{
seekpos=nextframe(seekpos); framenumber++;
printf("\b\b\b\b\b\b\b%4u:%02.0f",frame2mins(framenumber),frame2secs(framenumber));
DBGPRINT(9, "\nforward seek1 at %ld volume is %u\n",seekpos,vol);
}
silend=seekpos;
DBGPRINT(9, "\nsilend at %ld \n",silend);
/* quick check if silence is already long enough */
if (pos2time(silend)-pos2time(silfirstfound) >= silencelength)
{
framenumber--;
return frame2pos(framenumber);
}
/* rewind silencelength ms */
seekpos=frewind(seekpos,silencelength);
framenumber=pos2frame(seekpos);
DBGPRINT(9, "\nrewind to %ld \n",seekpos);
/* forward to last silent frame again and see if position is silend */
while ((vol=volume(seekpos)) < silvol && seekpos<dataend)
{
seekpos=nextframe(seekpos); framenumber++;
DBGPRINT(9, "\nforward seek2 at %ld volume is %u\n",seekpos,vol);
}
DBGPRINT(9, "\nafter seek2 at %ld volume is %u\n",seekpos,vol);
if (seekpos==silend) /* we found a silence */
{
framenumber--;
return frame2pos(framenumber);
}
else
{
framenumber=pos2frame(silend);
seekpos=fforward(silend,silencelength);
DBGPRINT(9, "\nfforward to %ld \n",seekpos);
DBGPRINT(9, "\nsilend at %ld \n",silend);
}
}
framenumber=totalframes;
return filesize; /* EOF */
}
/* This function returns the position of the beginning of the next silence */
long seeksilstart(long seekpos)
{
unsigned int vol;
long silstart, silend, silfirstfound;
long silfirstfoundframe;
seekpos=fforward(seekpos,silencelength); /* move on */
if (seekpos>=dataend) /* dataend reached? */
{
printf(" seeking at ");
framenumber=totalframes;
printf("\b\b\b\b\b\b\b%4u:%02.0f",frame2mins(framenumber),frame2secs(framenumber));
return filesize;
}
printf(" seeking at ");
printf("\b\b\b\b\b\b\b%4u:%02.0f",frame2mins(framenumber),frame2secs(framenumber));
while (seekpos<dataend)
{
while ((vol=volume(seekpos)) >= silvol)
{
/* look for possible silence every silencelength milliseconds: */
DBGPRINT(9, "\ncandidate seek at %ld volume is %u\n",seekpos,vol);
printf("\b\b\b\b\b\b\b%4u:%02.0f",frame2mins(framenumber),frame2secs(framenumber));
seekpos=fforward(seekpos,silencelength);
framenumber=pos2frame(seekpos);
if (seekpos>=dataend)
{
return filesize; /* dataend reached? */
}
}
/* we found a candidate! */
DBGPRINT(9, "\ncandidate found at %ld volume is %u.\n",seekpos,vol);
// printf("\b\b\b\b\b\b\b%4u:%02.0f",frame2mins(framenumber),frame2secs(framenumber)); unnötig;
silfirstfound=seekpos; /* remember first found silent frame */
framenumber=pos2frame(seekpos);
silfirstfoundframe=framenumber; /* remember first found silent frame number */
/* forward to last silent frame */
while ((vol=volume(seekpos)) < silvol && seekpos<dataend)
{
seekpos=nextframe(seekpos); framenumber++;
DBGPRINT(9, "\nforward seek at %ld volume is %u\n",seekpos,vol);
/* quick check if silence is already long enough */
if (framenumber-silfirstfoundframe >= silencelength/fix_frametime)
{
framenumber=silfirstfoundframe+1;
return frame2pos(framenumber);
}
}
silend=seekpos;
DBGPRINT(9, "\nsilend at %ld \n",silend);
/* rewind to first silent frame from first found silent frame */