1 | #!/usr/bin/perl
|
---|
2 | use warnings;
|
---|
3 | use strict;
|
---|
4 |
|
---|
5 | unless ($ARGV) {
|
---|
6 | print "usage: $0 { simon.voc | effects.voc }\n";
|
---|
7 | exit(0);
|
---|
8 | }
|
---|
9 |
|
---|
10 | my $infile = $ARGV[0];
|
---|
11 | my $num;
|
---|
12 |
|
---|
13 | if ($infile =~ /simon.voc$/) {
|
---|
14 | $num = 3624;
|
---|
15 | } elsif ($infile =~ /effects.voc$/) {
|
---|
16 | $num = 141;
|
---|
17 | } else {
|
---|
18 | die "Unknown file\n";
|
---|
19 | }
|
---|
20 |
|
---|
21 | my $tmpfile = 'tmp.voc';
|
---|
22 | my $tmpoutfile_all = 'tmpall.mp3';
|
---|
23 |
|
---|
24 | system("rm $tmpoutfile_all") if stat($tmpoutfile_all);
|
---|
25 |
|
---|
26 | my $outfile = $infile;
|
---|
27 | $outfile =~ s/\.voc$/.mp3/;
|
---|
28 |
|
---|
29 | my $tmpoutfile = $tmpfile;
|
---|
30 | $tmpoutfile =~ s/\.voc$/.mp3/;
|
---|
31 |
|
---|
32 | if (stat($outfile)) {
|
---|
33 | die "$outfile: File exists. Exiting\n";
|
---|
34 | }
|
---|
35 |
|
---|
36 | open(IN, "<$infile") || die "Could not open file: $infile\n";
|
---|
37 | binmode(IN);
|
---|
38 |
|
---|
39 | my @offsets;
|
---|
40 | my @new_offsets;
|
---|
41 |
|
---|
42 | my $buf;
|
---|
43 | my $i;
|
---|
44 |
|
---|
45 | for ($i = 0; $i < $num; $i++) {
|
---|
46 | seek(IN, $i * 4, 0);
|
---|
47 | read(IN, $buf, 4);
|
---|
48 |
|
---|
49 | $offsets[$i] = unpack('L', $buf);
|
---|
50 | }
|
---|
51 | $offsets[$i] = (stat(IN))[7];
|
---|
52 |
|
---|
53 | $new_offsets[0] = 0;
|
---|
54 | $new_offsets[1] = $num * 4;
|
---|
55 |
|
---|
56 | my @hdr;
|
---|
57 | my $size;
|
---|
58 | my $samplerate;
|
---|
59 |
|
---|
60 | for ($i = 1; $i < $num; $i++) {
|
---|
61 | if ($offsets[$i] == $offsets[$i + 1]) {
|
---|
62 | $new_offsets[$i + 1] = $new_offsets[$i];
|
---|
63 | next;
|
---|
64 | }
|
---|
65 |
|
---|
66 | seek(IN, $offsets[$i], 0);
|
---|
67 | read(IN, $buf, 32);
|
---|
68 |
|
---|
69 | @hdr = unpack('a20SSSCC3CC', $buf);
|
---|
70 | undef $buf;
|
---|
71 |
|
---|
72 | if ($hdr[0] eq "Creative Voice File\x1A") {
|
---|
73 | printf("Audio found at offset: %x\n", $offsets[$i]);
|
---|
74 | } else {
|
---|
75 | printf("ERROR: No audio found at offset: %x\n", $offsets[$i]);
|
---|
76 | die "Exiting.\n";
|
---|
77 | }
|
---|
78 |
|
---|
79 | $size = $hdr[5] + ($hdr[6] << 8) + ($hdr[7] << 16) - 2;
|
---|
80 | $samplerate = int(1000000/(256-$hdr[8]));
|
---|
81 |
|
---|
82 | read(IN, $buf, $size);
|
---|
83 |
|
---|
84 | open(TMP, ">$tmpfile");
|
---|
85 | binmode(TMP);
|
---|
86 | print TMP $buf;
|
---|
87 | close(TMP);
|
---|
88 | undef $buf;
|
---|
89 |
|
---|
90 | system("lame -t -q 5 -V 4 -B 64 --resample 22.05 -m m -r -s $samplerate --bitwidth 8 $tmpfile $tmpoutfile");
|
---|
91 | system("cat $tmpoutfile >> $tmpoutfile_all");
|
---|
92 |
|
---|
93 | $new_offsets[$i + 1] = $new_offsets[$i] + (stat($tmpoutfile))[7];
|
---|
94 |
|
---|
95 | }
|
---|
96 | pop(@new_offsets);
|
---|
97 |
|
---|
98 | close(IN);
|
---|
99 |
|
---|
100 | open(OUT, ">$outfile");
|
---|
101 | binmode(OUT);
|
---|
102 | foreach (@new_offsets) {
|
---|
103 | print OUT pack('L', $_);
|
---|
104 | }
|
---|
105 | close(OUT);
|
---|
106 |
|
---|
107 | system("cat $tmpoutfile_all >> $outfile");
|
---|
108 |
|
---|
109 | exit(0);
|
---|