superrip: limit maximum child processes
[cmccabe-bin] / superrip.rb
1 #!/usr/bin/ruby -w
2
3 #
4 # superrip.rb
5 #
6 # Advanced CD-ROM ripping program
7
8 # Copyright 2010, Colin McCabe
9 #
10
11 require 'fileutils'
12 require 'optparse'
13 require 'optparse/time'
14 require 'ostruct'
15
16 #-----------------------------------------------------------------
17 # constants
18 #-----------------------------------------------------------------
19 $cd_dev = "/dev/cdrom"
20 $children = Hash.new
21
22 #-----------------------------------------------------------------
23 # functions
24 #-----------------------------------------------------------------
25 def my_system(cmd)
26   puts cmd
27   system(cmd) unless $opts.dry_run == true
28   ($?.exitstatus == 0) or raise "#{cmd} failed"
29 end
30
31 def die_unless_installed(cmd)
32   system("which #{cmd} > /dev/null")
33   ($?.exitstatus == 0) or raise "you need to install the #{cmd} program"
34 end
35
36 def get_number_of_tracks_on_cd
37   look_for_tracks = false
38   lines = Array.new
39   IO.popen("cdda2wav -v summary -J dev=#{$cd_dev} 2>&1", "r") do |io|
40     io.readlines.each do |line|
41       line.chomp!
42       lines << line
43       if (line =~ /^AUDIOtrack/) then
44         look_for_tracks = true
45       elsif (look_for_tracks == true) then
46         look_for_tracks = false
47         line =~ /[ \t]*1-([1234567890][1234567890]*)[^1234567890]/ \
48           or raise "couldn't understand cdda2wav output!"
49         return $1.to_i
50       end
51     end
52   end
53   raise "couldn't find what we were looking for in cdda2wav output! \
54 output:#{lines.join('\n')}"
55 end
56
57 # Process the WAV file into an MP3 and FLAC file.
58 # This is done in a background process.
59 def process_wav(track)
60   FileUtils.mkdir_p(track.flac_dir, $fu_args)
61   my_system("flac -f '#{track.wav_file_name}' \
62 --output-name='#{track.flac_file_name}' &>/dev/null")
63   my_system("flac --test '#{track.flac_file_name}' &>/dev/null")
64   FileUtils.mkdir_p(track.mp3_dir, $fu_args)
65   my_system("lame -q 1 -b 192 '#{track.wav_file_name}' \
66 '#{track.mp3_file_name}' &>/dev/null")
67   FileUtils.rm_f(track.wav_file_name, $fu_args)
68 end
69
70 def audiorip(tnum, track)
71   begin
72     my_system("nice -1 cdparanoia -w -d #{$cd_dev} #{tnum}")
73   rescue
74     raise "failed to rip track #{tnum} (#{track.name})"
75   end
76   # cdparanoia always outputs to cdda.wav
77   FileUtils.mv("cdda.wav", track.wav_file_name, $fu_args)
78
79   # If there are too many processes, wait for one of them to terminate
80   if ($children.keys.length > $opts.max_children) then
81     pid = Process.wait(-1)
82     $children.delete(pid)
83   end
84
85   pid = Process.fork
86   if (pid == nil) then
87     begin
88       process_wav(track)
89       Kernel.exit(0)
90     rescue
91       Kernel.exit(1)
92     end
93   else
94     $children[pid] = 1
95   end
96 end
97
98 #-----------------------------------------------------------------
99 # classes
100 #-----------------------------------------------------------------
101 class MyOptions
102   def self.parse(args)
103     opts = OpenStruct.new
104     opts.dry_run = false
105     opts.max_children = 4
106     $fu_args = { :verbose => true }
107
108     # Fill in opts values
109     parser = OptionParser.new do |myparser|
110       myparser.banner = "Usage: #{ File.basename($0) } [opts]"
111       myparser.separator("Specific options:")
112       myparser.on("--dry-run", "-d",
113             "Show what would be done, without doing it.") do |a|
114         opts.dry_run = true
115         $fu_args = { :verbose => true, :noop => true }
116       end
117       myparser.on("--tracklist [FILE]", "-t",
118             "Provide a list of tracks to use.") do |file|
119         opts.manifest_file = file
120         opts.partial = false
121       end
122       myparser.on("--partial-tracklist [FILE]", "-T",
123             "Provide a partial list of tracks to use.") do |file|
124         opts.manifest_file = file
125         opts.partial = true
126       end
127       myparser.on("--max-children [NCHILD]", "-j",
128             "The maximum number of child processes to allow at any \
129 given time") do |nchild|
130         opts.max_children = nchild.to_i
131         if (opts.max_children < 1) then
132           raise "can't set max_children to #{opts.max_children}"
133         end
134       end
135     end
136     parser.parse!(args)
137
138     raise "you must provide a tracklist" unless opts.manifest_file != nil
139     return opts
140   end
141 end
142
143 class Track
144   attr_accessor :name, :flac_dir, :flac_file_name, :mp3_dir, :mp3_file_name,
145     :wav_file_name
146   def initialize(name)
147     if name =~ /\[LL\]/ then
148       raise "you can't include [LL] in a track name" 
149     end
150     if name =~ /\.mp3/ then
151       raise "don't include .mp3 in the track name; that will be added"
152     end
153     if name =~ /\.flac/ then
154       raise "don't include .flac in the track name; that will be added"
155     end
156     (name =~ /([^\/][^\/]*)\/([^\/]*[^\/])/) or \
157       raise "track name must be of the form 'foo/bar'"
158     @name = name
159     @flac_dir = "#{$1} [LL]"
160     @flac_file_name = "#{@flac_dir}/#{$2}.flac"
161     @mp3_dir = "#{$1}"
162     @mp3_file_name = "#{@mp3_dir}/#{$2}.mp3"
163     @wav_file_name = "#{$1}__#{$2}.wav"
164   end
165
166   def inspect
167     "track(\"#{@name}\")"
168   end
169 end
170
171 class Manifest
172   def initialize(filename)
173     @t = Hash.new
174     eval(File.new(filename).read)
175     @t.each do |key, val|
176       @t[key] = Track.new(val)
177     end
178     # TODO: implement some shortcuts that make manifests easier to type.
179     # Probably avoiding the necessity to continue typing the album name if it is the same as the
180     # previous track's name would make things a lot easier without complicating everything too much.
181   end
182
183   def validate(num_tracks)
184     if (@t.empty?) then
185       raise "you must define some tracks"
186     end
187     @t.each { |t| t.validate }
188     if (not $opts.partial) then
189       (1..num_tracks).each do |t|
190         if not @t[t].defined?
191           raise "don't know what to do with track #{t}"
192         end
193       end
194     end
195     # TODO: make sure that tracks inside albums are in order
196     # i.e. we don't map track 2 to a name that sorts to before track 1
197   end
198
199   def rip(num_tracks)
200     (1..num_tracks).each do |tnum|
201       next unless @t.has_key?(tnum)
202       audiorip(tnum, @t[tnum])
203     end
204     Process.waitall
205   end
206
207   def inspect
208     ret = ""
209     @t.keys.sort.each do |key|
210       ret = "#{ret}#{key}:'#{@t[key].inspect()}'\n"
211     end
212     return ret
213   end
214 end
215
216 #-----------------------------------------------------------------
217 # main
218 #-----------------------------------------------------------------
219 # Parse options.
220 begin
221   begin
222     $opts = MyOptions.parse(ARGV)
223   rescue ArgumentError => msg
224   $stderr.puts("#{msg} Type --help to see usage information.\n")
225   exit 1
226   end
227 end
228
229 die_unless_installed("lame")
230 die_unless_installed("flac")
231 die_unless_installed("cdparanoia")
232 die_unless_installed("cdda2wav")
233
234 manifest = Manifest.new($opts.manifest_file)
235 puts manifest.inspect
236 num_tracks = get_number_of_tracks_on_cd()
237 puts "found #{num_tracks} tracks"
238 manifest.rip(num_tracks)
239 puts "*** FINISHED ***"
240 exit 0