superrip: Fork a new process to do lame and flac
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Sat, 3 Apr 2010 20:09:09 +0000 (13:09 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Sat, 3 Apr 2010 20:09:09 +0000 (13:09 -0700)
superrip.rb

index 9c5caec..d069633 100755 (executable)
@@ -34,9 +34,11 @@ end
 
 def get_number_of_tracks_on_cd
   look_for_tracks = false
+  lines = Array.new
   IO.popen("cdda2wav -v summary -J dev=#{$cd_dev} 2>&1", "r") do |io|
     io.readlines.each do |line|
       line.chomp!
+      lines << line
       if (line =~ /^AUDIOtrack/) then
         look_for_tracks = true
       elsif (look_for_tracks == true) then
@@ -47,7 +49,21 @@ def get_number_of_tracks_on_cd
       end
     end
   end
-  raise "couldn't find what we were looking for in cdda2wav output!"
+  raise "couldn't find what we were looking for in cdda2wav output! \
+output:#{lines.join('\n')}"
+end
+
+# Process the WAV file into an MP3 and FLAC file.
+# This is done in a background process.
+def process_wav(track)
+  FileUtils.mkdir_p(track.flac_dir, $fu_args)
+  my_system("flac -f '#{track.wav_file_name}' \
+--output-name='#{track.flac_file_name}' &>/dev/null")
+  my_system("flac --test '#{track.flac_file_name}' &>/dev/null")
+  FileUtils.mkdir_p(track.mp3_dir, $fu_args)
+  my_system("lame -q 1 -b 192 '#{track.wav_file_name}' \
+'#{track.mp3_file_name}' &>/dev/null")
+  FileUtils.rm_f(track.wav_file_name, $fu_args)
 end
 
 def audiorip(tnum, track)
@@ -59,19 +75,16 @@ def audiorip(tnum, track)
   # cdparanoia always outputs to cdda.wav
   FileUtils.mv("cdda.wav", track.wav_file_name, $fu_args)
 
-  # TODO: spawn a thread to do this stuff in the background
-  FileUtils.mkdir_p(track.flac_dir, $fu_args)
-  my_system("flac -f '#{track.wav_file_name}' \
---output-name='#{track.flac_file_name}' &>/dev/null")
-  begin
-    my_system("flac --test '#{track.flac_file_name}' &>/dev/null")
-  rescue
-    raise "failed to encode #{track.flac_file_name}"
+  # TODO: if there are more than N processes, wait for one of them to terminate
+  pid = Process.fork
+  if (pid == nil) then
+    begin
+      process_wav(track)
+      Kernel.exit(0)
+    rescue
+      Kernel.exit(1)
+    end
   end
-  FileUtils.mkdir_p(track.mp3_dir, $fu_args)
-  my_system("lame -q 1 -b 192 '#{track.wav_file_name}' \
-'#{track.mp3_file_name}' &>/dev/null")
-  FileUtils.rm_f(track.wav_file_name, $fu_args)
 end
 
 #-----------------------------------------------------------------