aboutsummaryrefslogtreecommitdiff
path: root/audiocue2bincue
diff options
context:
space:
mode:
Diffstat (limited to 'audiocue2bincue')
-rw-r--r--audiocue2bincue90
1 files changed, 90 insertions, 0 deletions
diff --git a/audiocue2bincue b/audiocue2bincue
new file mode 100644
index 0000000..84c1582
--- /dev/null
+++ b/audiocue2bincue
@@ -0,0 +1,90 @@
+#!/usr/bin/env perl -w
+
+# Convert an audio file based CD image into a raw bin/cue.
+# Uses libsndfile to do the audio file decoding and conversion,
+# except for mp3 files (use lame for those).
+
+# Please don't try to convince me to use CPAN modules for things like
+# cue sheets or audio format conversions. This is me using perl as a
+# 'glue' language, not an 'enterprise applications platform'.
+
+($self = $0) =~ s,.*/,,;
+
+if(!@ARGV || $ARGV[0] eq '--help') {
+ print <<EOF;
+Usage: $self [-s] [input.cue] [output.cue]
+See man page for details.
+EOF
+ exit 0
+}
+
+if($ARGV[0] eq '-s') {
+ $swapbytes = 1;
+ shift @ARGV;
+}
+
+if($ARGV[1] && $ARGV[1] ne '-') {
+ open $out, ">", $ARGV[1];
+ select $out;
+}
+
+# Everyone always complains about regex looking like gibberish,
+# so here's my attempt at writing a readable(ish) one.
+$match_file = qr{
+ ^\s* # optional leading space/tabs
+ FILE # required token
+ \s+ # required one or more spaces before the filename
+ "? # optional quote (spec doesn't require it)
+ ([^"]+) # actual filename goes in $1
+ "? # optional quote afterwards
+ \s+ # required space(s) before the type
+ (\w+) # type (BINARY, WAVE, MP3 maybe, etc) goes in $2
+}x;
+
+@ARGV = ($ARGV[0]);
+
+%audiofiles = ();
+
+while(<>) {
+ if(/$match_file/) {
+ my ($file, $type) = ($1, $2);
+ $audiofiles{$file}++;
+ print fix_line($_, $file, $type);
+ } else {
+ print;
+ }
+}
+
+for(keys %audiofiles) {
+ my $newfilename = $new_filenames{$_};
+ (my $rawfilename = $newfilename) =~ s/\.bin$/.raw/;
+
+ my @cmd = ();
+
+ if($_ =~ /mp3$/i) {
+ @cmd = ( "lame", "-t", "--quiet", "--decode" );
+ push @cmd, "-x" if $swapbytes;
+ push @cmd, ( $_, $rawfilename );
+ } else {
+ my $endarg = "-endian=" . ($swapbytes ? "big" : "little");
+ @cmd = ( "sndfile-convert", "-pcm16", $endarg, $_, $rawfilename );
+ }
+
+ warn "Converting $_ to raw .bin with $cmd[0]\n";
+ system(@cmd);
+ rename($rawfilename, $newfilename);
+}
+
+sub fix_line {
+ my ($line, $filename, $type) = @_;
+ my $newfilename;
+
+ if($type eq 'BINARY') {
+ warn "$filename is already type BINARY, leaving as-is\n";
+ return $line;
+ }
+
+ ($newfilename = $filename) =~ s/(?:\.\w+)?$/.bin/;
+ $new_filenames{$filename} = $newfilename;
+ return "FILE \"$newfilename\" BINARY\r\n";
+}