Class: DTAS::SplitFX
Overview
The backend for dtas-splitfx(1) command, but also supported by dtas-player Unlike the stuff for dtas-player, dtas-splitfx is fairly tied to sox (but we may still pipe to ecasound or anything else)
Defined Under Namespace
Constant Summary collapse
- CMD =
:nodoc:
'sox "$INFILE" $COMMENTS $OUTFMT $OUTDST $TRIMFX $FX $RATEFX $DITHERFX'
Constants included from Process
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#infile ⇒ Object
readonly
Returns the value of attribute infile.
Instance Method Summary collapse
- #_bool(hash, key) ⇒ Object
- #cuebreakpoints ⇒ Object
-
#expand_cmd(env, command) ⇒ Object
for display purposes only.
- #generic_target(target = "flac") ⇒ Object
- #import(hash, overrides = {}) ⇒ Object
- #infile_env(env, infile) ⇒ Object
-
#initialize ⇒ SplitFX
constructor
vars: $CHANNELS (input) $BITS_PER_SAMPLE (input).
- #load_input!(hash) ⇒ Object
- #load_tracks!(hash) ⇒ Object
-
#parse_track(argv) ⇒ Object
argv: [ 't', '0:05', 'track one', 'fade_in=t 4', '.comment=blah' ] [ 'stop', '1:00' ].
- #run(target, opts = {}) ⇒ Object
- #splitfx_spawn(target, t, opts) ⇒ Object
- #t2s(time) ⇒ Object
-
#t2s_cdda(time) ⇒ Object
like t2s, but align to CDDA sectors (75 frames per second).
Methods included from Process
#dtas_spawn, #env_expand, #env_expand_ary, #env_expand_i, #qx, reaper
Methods included from XS
Constructor Details
#initialize ⇒ SplitFX
vars: $CHANNELS (input) $BITS_PER_SAMPLE (input)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/dtas/splitfx.rb', line 67 def initialize @env = {} @comments = {} @track_start = 1 @track_zpad = true @t2s = method(:t2s) @infile = nil @outdir = nil @compression = nil @rate = nil @bits = nil @targets = { "flac-cdda" => { "command" => CMD, "format" => { "bits" => 16, "rate" => 44100, "type" => "flac", "channels" => 2, }, }, } @tracks = [] @infmt = nil # wait until input is assigned @cuebp = nil # for playback @command = nil # top-level, for playback end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
15 16 17 |
# File 'lib/dtas/splitfx.rb', line 15 def command @command end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
15 16 17 |
# File 'lib/dtas/splitfx.rb', line 15 def env @env end |
#infile ⇒ Object (readonly)
Returns the value of attribute infile.
15 16 17 |
# File 'lib/dtas/splitfx.rb', line 15 def infile @infile end |
Instance Method Details
#_bool(hash, key) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/dtas/splitfx.rb', line 95 def _bool(hash, key) val = hash[key] case val when false, true then yield val when nil # ignore else raise TypeError, "'#{key}' must be boolean (true or false)" end end |
#cuebreakpoints ⇒ Object
410 411 412 413 414 |
# File 'lib/dtas/splitfx.rb', line 410 def cuebreakpoints rv = @cuebp and return rv require_relative 'cue_index' @cuebp = @tracks.map { |t| DTAS::CueIndex.new(1, "#{t.tbeg}s") } end |
#expand_cmd(env, command) ⇒ Object
for display purposes only
423 424 425 426 427 |
# File 'lib/dtas/splitfx.rb', line 423 def (env, command) # for display purposes only Shellwords.split(command).map do |arg| qx(env, "printf %s \"#{arg}\"") end end |
#generic_target(target = "flac") ⇒ Object
160 161 162 163 164 |
# File 'lib/dtas/splitfx.rb', line 160 def generic_target(target = "flac") outfmt = @infmt.dup outfmt.type = target { "command" => CMD, "format" => outfmt } end |
#import(hash, overrides = {}) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/dtas/splitfx.rb', line 105 def import(hash, overrides = {}) # merge overrides from the command-line overrides.each do |k,v| case v when Hash then hash[k] = (hash[k] || {}).merge(v) else hash[k] = v end end case v = hash["track_zpad"] when Integer then @track_zpad = v else _bool(hash, "track_zpad") { |val| @track_zpad = val } end _bool(hash, "cdda_align") { |val| @t2s = method(val ? :t2s : :t2s_cdda) } case v = hash["track_start"] when Integer then @track_start = v when nil else raise TypeError, "'track_start' must be an integer" end %w(comments env targets).each do |key| case val = hash[key] when Hash then instance_variable_get("@#{key}").merge!(val) when nil else raise TypeError, "'#{key}' must be a hash" end end @targets.each_value do |thsh| case tfmt = thsh["format"] when Hash thsh["format"] = DTAS::Format.load(tfmt) unless tfmt.empty? end end load_input!(hash) load_tracks!(hash) @command = hash["command"] # nil by default end |
#infile_env(env, infile) ⇒ Object
416 417 418 419 420 421 |
# File 'lib/dtas/splitfx.rb', line 416 def infile_env(env, infile) env["INFILE"] = infile dir, base = File.split(File.(infile)) env["INDIR"] = dir env["INBASE"] = base end |
#load_input!(hash) ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/dtas/splitfx.rb', line 151 def load_input!(hash) @infile = hash["infile"] or raise ArgumentError, "'infile' not specified" if infmt = hash["infmt"] # rarely needed @infmt = DTAS::Format.load(infmt) else # likely @infmt = DTAS::Format.from_file(@env, @infile) end end |
#load_tracks!(hash) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/dtas/splitfx.rb', line 257 def load_tracks!(hash) tracks = hash["tracks"] or raise ArgumentError, "'tracks' not specified" tracks.each { |line| parse_track(Shellwords.split(line)) } fmt = "%d" case @track_zpad when true max = @track_start - 1 + @tracks.size fmt = "%0#{max.to_s.size}d" when Integer fmt = "%0#{@track_zpad}d" end nr = @track_start @tracks.delete_if do |t| case t when Skip true else t.comments["TRACKNUMBER"] = sprintf(fmt, nr) nr += 1 false end end end |
#parse_track(argv) ⇒ Object
argv:
[ 't', '0:05', 'track one', 'fade_in=t 4', '.comment=blah' ]
[ 'stop', '1:00' ]
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/dtas/splitfx.rb', line 285 def parse_track(argv) case cmd = argv.shift when "t" start_time = argv.shift title = argv.shift t = T.new t.tbeg = @t2s.call(start_time) t.comments = @comments.dup title.valid_encoding? or warn "#{title.inspect} encoding invalid" t.comments["TITLE"] = title t.env = @env.dup argv.each do |arg| case arg when %r{\Afade_in=(.+)\z} # $1 = "t 4" or just "4" t.fade_in = $1.split(/\s+/) when %r{\Afade_out=(.+)\z} # $1 = "t 4" or just "4" t.fade_out = $1.split(/\s+/) when %r{\Aenv\.([^=]+)=(.+)\z} then t.env[$1] = -$2 when %r{\A\.(\w+)=(.+)\z} then t.comments[$1] = $2 else raise ArgumentError, "unrecognized arg(s): #{xs(argv)}" end end prev = @tracks.last and prev.commit(t.tbeg) @tracks << t when "skip" stop_time = argv.shift argv.empty? or raise ArgumentError, "skip does not take extra args" s = Skip.new s.tbeg = @t2s.call(stop_time) # s.comments = {} # s.env = {} prev = @tracks.last or raise ArgumentError, "no tracks to skip" prev.commit(s.tbeg) @tracks << s when "stop" stop_time = argv.shift argv.empty? or raise ArgumentError, "stop does not take extra args" samples = @t2s.call(stop_time) prev = @tracks.last and prev.commit(samples) else raise ArgumentError, "unknown command: #{xs(cmd)}" end end |
#run(target, opts = {}) ⇒ Object
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/dtas/splitfx.rb', line 352 def run(target, opts = {}) if @outdir = opts[:outdir] require 'fileutils' FileUtils.mkpath(@outdir) end @compression = opts[:compression] @rate = opts[:rate] @bits = opts[:bits] trim = opts[:trim] and @tracks = [ UTrim.new(trim, @env, @comments) ] if trim && opts[:filter] raise ArgumentError, 'trim and filter are mutually exclusive' end fails = [] tracks = @tracks.dup (opts[:filter] || []).each do |re| field, val = re.split(/=/, 2) if val tracks.delete_if { |t| (t.comments[field] || '') !~ /#{val}/ } else tracks.delete_if { |t| t.comments.values.grep(/#{re}/).empty? } end end pids = {} jobs = opts[:jobs] || tracks.size # jobs == nil => everything at once if opts[:sox_pipe] jobs = 1 @out = $stderr else @out = $stdout end jobs.times.each do t = tracks.shift or break pid, tmp = splitfx_spawn(target, t, opts) pids[pid] = [ t, tmp ] end while pids.size > 0 pid, status = Process.waitpid2(-1) done = pids.delete(pid) if status.success? if t = tracks.shift pid, tmp = splitfx_spawn(target, t, opts) pids[pid] = [ t, tmp ] end @out.puts "DONE #{done[0].inspect}" if $DEBUG done[1].close! else fails << [ done[0], status ] end end return true if fails.empty? && tracks.empty? fails.each do |(_t,s)| warn "FAIL #{s.inspect} #{_t.inspect}" end false end |
#splitfx_spawn(target, t, opts) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/dtas/splitfx.rb', line 166 def splitfx_spawn(target, t, opts) if tgt = @targets[target] target = tgt else target = generic_target(target) end outfmt = target["format"] # default format: unless outfmt outfmt = @infmt.dup outfmt.type = "flac" end outfmt.bits = @bits if @bits outfmt.rate = @rate if @rate # player commands will use SOXFMT by default, so we must output that # as a self-describing format to the actual encoding instances player_cmd = @command suffix = outfmt.type env = outfmt.to_env # set very high quality resampling if using 24-bit or higher output if outfmt.rate != @infmt.rate if outfmt.bits # set very-high resampling quality for 24-bit outputs quality = "-v" if outfmt.bits >= 24 else # assume output bits matches input bits quality = "-v" if @infmt.bits >= 24 end env["RATEFX"] = "rate #{quality} #{outfmt.rate}" end # add noise-shaped dither for 16-bit (sox manual seems to recommend this) if opts[:no_dither] env["SOX_OPTS"] = "#{ENV["SOX_OPTS"]} -D" elsif outfmt.bits && outfmt.bits <= 16 env["DITHERFX"] = "dither -s" end comments = Tempfile.new(%W(dtas-splitfx-#{t.comments["TRACKNUMBER"]} .txt)) t.comments.each do |k,v| env[k] = v.to_s comments.puts("#{k}=#{v}") end comments.flush env["COMMENTS"] = "--comment-file=#{comments.path}" infile_env(env, @infile) outarg = outfmt.to_sox_arg outarg << "-C#@compression" if @compression env["OUTFMT"] = xs(outarg) env["SUFFIX"] = suffix env["OUTDIR"] = @outdir ? "#@outdir/".squeeze('/') : '' env["OUTDST"] = opts[:sox_pipe] ? "-p" : "$OUTDIR$TRACKNUMBER.$SUFFIX" env.merge!(t.env) command = target["command"] # if a default dtas-player command is set, use that. # we'll clobber our default environment since we assume play_cmd # already takes those into account. In other words, use our # target-specific commands like a dtas-player sink: # @command | (INFILE= FX= TRIMFX=; target['command']) if player_cmd sub_env = { 'INFILE' => '-p', 'FX' => '', 'TRIMFX' => '', 'SOXFMT' => '' } env['SOXFMT'] = '-tsox' sub_env['OUTFMT'] = env.delete('OUTFMT') sub_env_s = sub_env.map { |k,v| "#{k}=\"#{v}\"" }.join(' ') show_cmd = [ (env, player_cmd), '|', '(', "#{sub_env_s};", (env.merge(sub_env), command), ')' ].flatten command = "#{player_cmd} | (#{sub_env_s}; #{command})" else show_cmd = (env, command) end @out.puts(show_cmd.join(' ')) unless opts[:silent] command = 'true' if opts[:dryrun] # still gotta fork # pgroup: false so Ctrl-C on command-line will immediately stop everything o = { pgroup: false } e = opts[:err_suffix] and o[:err] = [ "#{env['OUTDIR']}#{env['TRACKNUMBER']}#{e}", 'a' ] [ dtas_spawn(env, command, o), comments ] end |
#t2s(time) ⇒ Object
348 349 350 |
# File 'lib/dtas/splitfx.rb', line 348 def t2s(time) @infmt.hhmmss_to_samples(time) end |
#t2s_cdda(time) ⇒ Object
like t2s, but align to CDDA sectors (75 frames per second)
333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/dtas/splitfx.rb', line 333 def t2s_cdda(time) time = time.dup frac = 0 # fractions of a second, convert to samples based on sample rate # taking into account CDDA alignment if time.sub!(/\.(\d+)\z/, "") s = "0.#$1".to_f * @infmt.rate / 75 frac = s.round * 75 end # feed the rest to the normal function t2s(time) + frac end |