]> diplodocus.org Git - flac-archive/blobdiff - fa-flacd
Factor out the flac(1)-running part to run_flac, which is identical
[flac-archive] / fa-flacd
index d378a1b42cd54a2478f9dce6d7175547d25ab610..b9f883cfd5230dfc1060837be395d465cc3234b4 100755 (executable)
--- a/fa-flacd
+++ b/fa-flacd
@@ -38,6 +38,7 @@ sub get_tags {
     my $value;
     my $artist;
     my $album;
+    my $discnum;
     my @tags;
 
     verbose("Opening tags file $fn\n");
@@ -54,11 +55,59 @@ sub get_tags {
         } elsif (/^ALBUM=/i) {
             $album = $value;
             verbose("ALBUM $album from $fn\n"); # cperl-mode sucks "
+        } elsif (/^DISCNUMBER=/i) {
+            $discnum = int($value);
+            verbose("DISCNUMBER $discnum from $fn\n");
         }
     }
     close(TAGS) or die("close($fn): $!");
 
-    return ($artist, $album, @tags);
+    return ($artist, $album, $discnum, @tags);
+}
+
+sub bork_tags {
+    my $h = shift;
+    my @result;
+
+    while (my ($key, $vall) = each(%$h)) {
+        for my $val (@$vall) {
+            push(@result, "$key=$val")
+        }
+    }
+
+    return @result;
+}
+
+sub run_flac {
+    my $infile = shift;
+    my $cue = shift;
+    my $outfile = shift;
+
+    my @cue;
+    if (defined($cue)) {
+        @cue = ('--cuesheet', $cue);
+    }
+
+    verbose("Running flac\n");
+    my $status = system('flac', '-o', "$outfile.flac-tmp",
+                     '--delete-input-file', '-V', '--no-padding', '--best',
+                     @cue,
+                     map({ ('-T', $_) } @_),
+                     $infile);
+    if (WIFEXITED($status)) {
+        if (($status = WEXITSTATUS($status)) != 0) {
+            die("flac exited with status $status");
+        }
+    } elsif (WIFSIGNALED($status)) {
+        die("flac killed with signal ", WTERMSIG($status));
+    } elsif (WIFSTOPPED($status)) {
+        die("flac stopped with signal ", WSTOPSIG($status));
+    } else {
+        die("Major horkage on system(flac): \$? = $? \$! = $!");
+    }
+
+    rename("$outfile.flac-tmp", "$outfile.flac")
+      or die("rename($outfile.flac-tmp, $outfile.flac): $!");
 }
 
 # Process the fa-rip output in the directory DIR.
@@ -66,56 +115,79 @@ sub flac {
     my $dir = shift;
     my $artist;
     my $album;
+    my $discnum;
     my @tags;
     my $outfile;
-    my $status;
 
     verbose("Renaming $dir/tags\n");
     rename("$dir/tags", "$dir/using-tags")
       or die("rename($dir/tags, $dir/using-tags): $!");
 
-    ($artist, $album, @tags) = get_tags("$dir/using-tags");
+    ($artist, $album, $discnum, @tags) = get_tags("$dir/using-tags");
+    for ($artist, $album) {
+        s|/|_|g;
+    }
 
     verbose("mkdir($artist)\n");
     -d $artist or mkdir($artist) or die("mkdir($artist): $!");
 
+    my $outdir = "$artist/$album";
+    verbose("mkdir($outdir)\n");
+    -d "$outdir" or mkdir("$outdir") or die("mkdir($outdir): $!");
+
     verbose("chdir($dir)\n");
     chdir($dir) or die("chdir($dir): $!");
 
-    $outfile = "$album";
-    $outfile =~ s/\//_/g;
-
-    verbose("Running flac\n");
-    $status = system('flac', '-o', "../$artist/$outfile.flac-tmp",
-                     '--delete-input-file', '-V', '--cuesheet',
-                     'cue', '--no-padding', '--best',
-                     map({ ('-T', $_) } @tags),
-                     'wav');
-    if (WIFEXITED($status)) {
-        if (($status = WEXITSTATUS($status)) != 0) {
-            die("flac exited with status $status");
+    my @artist;
+    my %things;
+    my (@bork, @titles);
+    for my $tag (@tags) {
+        if ($tag =~ /^([^[]+)\[(\d+)]=(.*)/) {
+            push(@{$things{$2}->{$1}}, $3);
+        } elsif ($tag =~ /^ARTIST=/) {
+            push(@artist, $tag);
+        } else {
+            push(@bork, $tag);
         }
-    } elsif (WIFSIGNALED($status)) {
-        die("flac killed with signal ", WTERMSIG($status));
-    } elsif (WIFSTOPPED($status)) {
-        die("flac stopped with signal ", WSTOPSIG($status));
-    } else {
-        die("Major horkage on system(flac): \$? = $? \$! = $!");
+    }
+    @tags = @bork;
+
+    my @files;
+    for my $tracknum (sort(map(int, keys(%things)))) {
+        my $title = join(' ', map(split, @{$things{$tracknum}->{'TITLE'}}));
+        $title =~ s|/|_|g;
+        $outfile = join(' ',
+                        (defined($discnum)
+                         ? sprintf('%02d', $discnum)
+                         : ()),
+                        sprintf('%02d', $tracknum),
+                        $title);
+        push(@files, "$outdir/$outfile.flac");
+        $outfile = "../$outdir/$outfile";
+
+        my @lartist;
+        if (exists($things{$tracknum}->{'ARTIST'})) {
+            @lartist = ();
+        } else {
+            @lartist = @artist;
+        }
+
+        run_flac(sprintf('track%02d.cdda.wav', $tracknum), undef, $outfile,
+                 @lartist,
+                 grep({ $_ !~ /^ARTIST=/ } @tags),
+                 bork_tags($things{$tracknum}));
     }
 
     verbose("Cleaning up $dir\n");
     unlink('using-tags') or die("unlink(using-tags): $!");
     unlink('cue') or die("unlink(cue): $!");
-    rename('log', "../$artist/$outfile.log")
-      or die("rename(log, ../$artist/$outfile.log): $!");
+    rename('log', "../$outdir/log")
+      or die("rename(log, ../$outdir/log): $!");
     chdir('..') or die("chdir(..): $!");
 
-    rename("$artist/$outfile.flac-tmp", "$artist/$outfile.flac")
-      or die("rename($artist/$outfile.flac-tmp, $artist/$outfile.flac): $!");
-
     if (-x "$dir/post-processor") {
-        verbose("Running './$dir/post-processor $artist/$outfile.flac'\n");
-        system("./$dir/post-processor", "$artist/$outfile.flac");
+        verbose("Running './$dir/post-processor'\n");
+        system("./$dir/post-processor", @files);
         unlink("$dir/post-processor") or die("unlink($dir/post-processor): $!");
     }
 
@@ -146,7 +218,7 @@ sub newjob {
     }
 
     if ($debug or $pid == 0) {
-        $SIG{CHLD} = 'IGNORE';
+        $SIG{CHLD} = 'DEFAULT';
         open(STDERR, ">$dir/log") or die("open(STDERR, >$dir/log): $!");
         exit(flac($dir));
     }