Ticket #8112: voc2mp3.pl

File voc2mp3.pl, 2.1 KB (added by SF/olki, 18 years ago)
Line 
1#!/usr/bin/perl
2use warnings;
3use strict;
4
5unless ($ARGV) {
6 print "usage: $0 { simon.voc | effects.voc }\n";
7 exit(0);
8}
9
10my $infile = $ARGV[0];
11my $num;
12
13if ($infile =~ /simon.voc$/) {
14 $num = 3624;
15} elsif ($infile =~ /effects.voc$/) {
16 $num = 141;
17} else {
18 die "Unknown file\n";
19}
20
21my $tmpfile = 'tmp.voc';
22my $tmpoutfile_all = 'tmpall.mp3';
23
24system("rm $tmpoutfile_all") if stat($tmpoutfile_all);
25
26my $outfile = $infile;
27$outfile =~ s/\.voc$/.mp3/;
28
29my $tmpoutfile = $tmpfile;
30$tmpoutfile =~ s/\.voc$/.mp3/;
31
32if (stat($outfile)) {
33 die "$outfile: File exists. Exiting\n";
34}
35
36open(IN, "<$infile") || die "Could not open file: $infile\n";
37binmode(IN);
38
39my @offsets;
40my @new_offsets;
41
42my $buf;
43my $i;
44
45for ($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
56my @hdr;
57my $size;
58my $samplerate;
59
60for ($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}
96pop(@new_offsets);
97
98close(IN);
99
100open(OUT, ">$outfile");
101binmode(OUT);
102foreach (@new_offsets) {
103 print OUT pack('L', $_);
104}
105close(OUT);
106
107system("cat $tmpoutfile_all >> $outfile");
108
109exit(0);