about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rwxr-xr-xbin/dtas-splitfx17
-rw-r--r--examples/splitfx.sample.yml10
-rw-r--r--lib/dtas/splitfx.rb18
-rw-r--r--test/helper.rb2
-rw-r--r--test/test_splitfx.rb41
5 files changed, 66 insertions, 22 deletions
diff --git a/bin/dtas-splitfx b/bin/dtas-splitfx
index 2d66c0d..d75c1de 100755
--- a/bin/dtas-splitfx
+++ b/bin/dtas-splitfx
@@ -4,15 +4,16 @@
 require 'yaml'
 require 'optparse'
 require 'dtas/splitfx'
-usage = "#$0 [-n|--dry-run][-j [JOBS]] SPLITFX_FILE.yml [TARGET]"
+usage = "#$0 [-n|--dry-run][-j [JOBS]][-s|--silent] SPLITFX_FILE.yml [TARGET]"
 overrides = {} # FIXME: not tested
-dryrun = false
+opts = { jobs: 1 }
 jobs = 1
-op = OptionParser.new('', 24, '  ') do |opts|
-  opts.banner = usage
-  opts.on('-n', '--dry-run') { dryrun = true }
-  opts.on('-j', '--jobs [JOBS]', Integer) { |val| jobs = val }
-  opts.parse!(ARGV)
+OptionParser.new('', 24, '  ') do |op|
+  op.banner = usage
+  op.on('-n', '--dry-run') { opts[:dryrun] = true }
+  op.on('-j', '--jobs [JOBS]', Integer) { |val| opts[:jobs] = val }
+  op.on('-s', '--quiet', '--silent') { |val| opts[:silent] = true }
+  op.parse!(ARGV)
 end
 
 args = []
@@ -36,4 +37,4 @@ file = args.shift or abort usage
 target = args.shift || "flac"
 splitfx = DTAS::SplitFX.new
 splitfx.import(YAML.load(File.read(file)), overrides)
-splitfx.run(target, jobs, dryrun)
+splitfx.run(target, opts)
diff --git a/examples/splitfx.sample.yml b/examples/splitfx.sample.yml
index c4655ff..297e50b 100644
--- a/examples/splitfx.sample.yml
+++ b/examples/splitfx.sample.yml
@@ -1,10 +1,12 @@
 # To the extent possible under law, Eric Wong has waived all copyright and
 # related or neighboring rights to this example.
+# Note: be sure to update test/test_splitfx.rb if you change this,
+# test_splitfx.rb relies on this.
 ---
 infile: foo.flac
 env:
-  PATH: /usr/local/bin:/usr/bin:/bin
-  SOX_OPTS: -R
+  PATH: $PATH
+  SOX_OPTS: $SOX_OPTS -R
 comments:
   ARTIST: John Smith
   ALBUM: Hello World
@@ -13,5 +15,5 @@ track_start: 1 # 0 for pregap/intro tracks
 cdda_align: true
 tracks:
   - t 0:04 "track one"
-  - t 0:05 "track two"
-  - stop 1:00
+  - t 0:10 "track two"
+  - stop 24
diff --git a/lib/dtas/splitfx.rb b/lib/dtas/splitfx.rb
index 7a5d705..2326546 100644
--- a/lib/dtas/splitfx.rb
+++ b/lib/dtas/splitfx.rb
@@ -120,7 +120,7 @@ class DTAS::SplitFX # :nodoc:
     { command: CMD, format: DTAS::Format.load(fmt) }
   end
 
-  def spawn(target, t, dryrun = false)
+  def spawn(target, t, opts)
     target = @targets[target] || generic_target(target)
     outfmt = target[:format]
     env = outfmt.to_env
@@ -156,12 +156,14 @@ class DTAS::SplitFX # :nodoc:
       qx(env, "printf %s \"#{arg}\"")
     end
     echo = "echo #{xs(tmp)}"
-    if dryrun
+    if opts[:dryrun]
       command = echo
     else
-      system(echo)
+      system(echo) unless opts[:silent]
     end
-    [ dtas_spawn(env, command, {}), comments ]
+
+    # pgroup: false so Ctrl-C on command-line will immediately stop everything
+    [ dtas_spawn(env, command, pgroup: false), comments ]
   end
 
   def load_tracks!(hash)
@@ -245,14 +247,14 @@ class DTAS::SplitFX # :nodoc:
     @infmt.hhmmss_to_samples(time)
   end
 
-  def run(target, jobs = 1, dryrun = false)
+  def run(target, opts = {})
     fails = []
     tracks = @tracks.dup
     pids = {}
-    jobs ||= tracks.size # jobs == nil => everything at once
+    jobs = opts[:jobs] || tracks.size # jobs == nil => everything at once
     jobs.times.each do
       t = tracks.shift or break
-      pid, tmp = spawn(target, t, dryrun)
+      pid, tmp = spawn(target, t, opts)
       pids[pid] = [ t, tmp ]
     end
 
@@ -261,7 +263,7 @@ class DTAS::SplitFX # :nodoc:
       done = pids.delete(pid)
       if status.success?
         if t = tracks.shift
-          pid, tmp = spawn(target, t, dryrun)
+          pid, tmp = spawn(target, t, opts)
           pids[pid] = [ t, tmp ]
         end
         puts "DONE #{done[0].inspect}" if $DEBUG
diff --git a/test/helper.rb b/test/helper.rb
index 7675a7c..fe980c2 100644
--- a/test/helper.rb
+++ b/test/helper.rb
@@ -1,6 +1,8 @@
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 $stdout.sync = $stderr.sync = Thread.abort_on_exception = true
+require 'thread'
+WAIT_ALL_MTX = Mutex.new
 
 # fork-aware coverage data gatherer, see also test/covshow.rb
 if ENV["COVERAGE"]
diff --git a/test/test_splitfx.rb b/test/test_splitfx.rb
index 1d36c54..e946080 100644
--- a/test/test_splitfx.rb
+++ b/test/test_splitfx.rb
@@ -1,14 +1,51 @@
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
-require './test/helper'
+require 'yaml'
 require 'dtas/splitfx'
+require 'thread'
+require_relative 'helper'
 
 class TestSplitfx < Testcase
-  def test_cdda
+  parallelize_me!
+
+  def test_t2s
     sfx = DTAS::SplitFX.new
     sfx.instance_eval do
       @infmt = DTAS::Format.load("rate"=>44100)
     end
     assert_equal 118554000, sfx.t2s_cdda('44:48.3')
+    assert_equal 118554030, sfx.t2s('44:48.3')
+  end
+
+  def test_example
+    hash = YAML.load(File.read("examples/splitfx.sample.yml"))
+    sfx = DTAS::SplitFX.new
+    Dir.mktmpdir do |dir|
+      Dir.chdir(dir) do
+        # create a guitar pluck
+        cmd = '(for n in E2 A2 D3 G3 B3 E4; do '\
+               'sox -n -ts32 -c2 -r44100 - synth 4 pluck $n; done ) | ' \
+               'sox -ts32 -c2 -r44100 - foo.flac'
+        assert system(cmd), cmd.inspect
+        sfx.import(hash, {})
+        opts = { jobs: nil, silent: true }
+        WAIT_ALL_MTX.synchronize do
+          sfx.run("flac", opts)
+        end
+        expect = %w(1.flac 2.flac foo.flac)
+        assert_equal expect, Dir["*.flac"].sort
+
+        res_cmd = "sox 1.flac 2.flac -ts32 -c2 -r44100 result.s32"
+        res_pid = fork { exec res_cmd }
+        exp_cmd = "sox foo.flac -ts32 -c2 -r44100 expect.s32 trim 4"
+        exp_pid = fork { exec exp_cmd }
+        _, s = Process.waitpid2(res_pid)
+        assert s.success?, "#{res_cmd}: #{s.inspect}"
+        _, s = Process.waitpid2(exp_pid)
+        assert s.success?, "#{exp_cmd}: #{s.inspect}"
+        cmp = "cmp result.s32 expect.s32"
+        assert system(cmp), cmp
+      end
+    end
   end
 end