Ticket #7969: extract.pl

File extract.pl, 1.6 KB (added by SF/olki, 22 years ago)

Perl script to convert simon.voc to simon.wav

Line 
1#!/usr/bin/perl
2use warnings;
3use strict;
4
5my $num = 3624;
6
7my $infile = 'simon.voc';
8
9my $tmpvocfile = 'tmp.voc';
10my $offsetfile = 'offsets';
11my $tmpwavfile_all = 'tmpall.wav';
12
13system("rm $tmpwavfile_all") if stat($tmpwavfile_all);
14
15my $outfile = $infile;
16$outfile =~ s/\.voc$/.wav/;
17
18my $tmpwavfile = $tmpvocfile;
19$tmpwavfile =~ s/\.voc$/.wav/;
20
21if (stat($outfile)) {
22 die "$outfile: File exists. Exiting\n";
23}
24
25open(IN, "<$infile") || die "Could not open file: $infile\n";
26binmode(IN);
27
28my @offsets;
29my @new_offsets;
30
31my $buf;
32my $i;
33
34for ($i = 0; $i < $num; $i++) {
35 seek(IN, $i * 4, 0);
36 read(IN, $buf, 4);
37
38 $offsets[$i] = unpack('I', $buf);
39}
40$offsets[$i] = (stat(IN))[7];
41
42$new_offsets[0] = 0;
43$new_offsets[1] = $num * 4;
44
45for ($i = 1; $i < $num; $i++) {
46 if ($offsets[$i] == $offsets[$i + 1]) {
47 $new_offsets[$i + 1] = $new_offsets[$i];
48 next;
49 }
50
51 seek(IN, $offsets[$i], 0);
52 read(IN, $buf, 8);
53
54 if ($buf eq 'Creative') {
55 printf("Audio found at offset: %x\n", $offsets[$i]);
56 } else {
57 printf("ERROR: No audio found at offset: %x\n", $offsets[$i]);
58 die "Exiting.\n";
59 }
60
61 seek(IN, $offsets[$i], 0);
62 read(IN, $buf, $offsets[$i + 1] - $offsets[$i]);
63
64 open(TMP, ">$tmpvocfile");
65 binmode(TMP);
66 print TMP $buf;
67 close(TMP);
68 undef $buf;
69
70 system("sox $tmpvocfile $tmpwavfile; cat $tmpwavfile >> $tmpwavfile_all");
71
72 $new_offsets[$i + 1] = $new_offsets[$i] + (stat($tmpwavfile))[7];
73}
74pop(@new_offsets);
75
76close(IN);
77
78open(OUT, ">$outfile");
79binmode(OUT);
80foreach (@new_offsets) {
81 print OUT pack('I', $_);
82}
83close(OUT);
84
85system("cat $tmpwavfile_all >> $outfile");
86
87exit(0);
88