about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--lib/dtas/trimfx.rb32
-rw-r--r--test/test_trimfx.rb28
2 files changed, 33 insertions, 27 deletions
diff --git a/lib/dtas/trimfx.rb b/lib/dtas/trimfx.rb
index d875c75..945fff1 100644
--- a/lib/dtas/trimfx.rb
+++ b/lib/dtas/trimfx.rb
@@ -2,6 +2,7 @@
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 require_relative '../dtas'
 require_relative 'parse_time'
+require_relative 'format'
 require 'shellwords'
 
 class DTAS::TrimFX
@@ -11,9 +12,13 @@ class DTAS::TrimFX
   attr_reader :tlen
   attr_reader :cmd
 
-  def initialize(args)
+  def initialize(args, format = DTAS::Format.new)
+    @format = format
     args = args.dup
     case args.shift
+    when :pad # [ :pad, start_time, end_time ]
+      @tbeg = args.shift
+      @tlen = args.shift - @tbeg
     when "trim"
       parse_trim!(args)
     when "all"
@@ -22,7 +27,7 @@ class DTAS::TrimFX
     else
       raise ArgumentError, "#{args.inspect} not understood"
     end
-    case tmp =  args.shift
+    case tmp = args.shift
     when "sh" then @cmd = args
     when "sox" then tfx_sox(args)
     when "eca" then tfx_eca(args)
@@ -45,15 +50,12 @@ class DTAS::TrimFX
     @cmd.concat(%w(| sox $ECA2SOX - $SOXOUT))
   end
 
-  def to_sox_arg(format)
+  def to_sox_arg
     if @tbeg && @tlen
-      beg = @tbeg * format.rate
-      len = @tlen * format.rate
-      %W(trim #{beg.round}s #{len.round}s)
+      %W(trim #{@tbeg}s #{@tlen}s)
     elsif @tbeg
       return [] if @tbeg == 0
-      beg = @tbeg * format.rate
-      %W(trim #{beg.round}s)
+      %W(trim #{@tbeg}s)
     else
       []
     end
@@ -69,11 +71,11 @@ class DTAS::TrimFX
       is_stop_time = tlen.sub!(/\A=/, "") ? true : false
       tlen = parse_time(tlen)
       tlen = tlen - tbeg if is_stop_time
+      @tlen = (tlen * @format.rate).round
     else
-      tlen = nil
+      @tlen = nil
     end
-    @tbeg = tbeg
-    @tlen = tlen
+    @tbeg = (tbeg * @format.rate).round
   end
 
   def <=>(other)
@@ -137,7 +139,7 @@ class DTAS::TrimFX
 
   # like schedule, but fills in the gaps with pass-through (no-op) TrimFX objs
   # This does not change the number of epochs.
-  def self.expand(ary, total_len)
+  def self.expand(ary, total_samples)
     rv = []
     schedule(ary).each_with_index do |sary, epoch|
       tip = 0
@@ -145,14 +147,14 @@ class DTAS::TrimFX
       while tfx = sary.shift
         if tfx.tbeg > tip
           # fill in the previous gap
-          nfx = new(%W(trim #{tip} =#{tfx.tbeg}))
+          nfx = new([:pad, tip, tfx.tbeg])
           dst << nfx
           dst << tfx
           tip = tfx.tbeg + tfx.tlen
         end
       end
-      if tip < total_len # fill until the last chunk
-        nfx = new(%W(trim #{tip} =#{total_len}))
+      if tip < total_samples # fill until the last chunk
+        nfx = new([:pad, tip, total_samples])
         dst << nfx
       end
     end
diff --git a/test/test_trimfx.rb b/test/test_trimfx.rb
index a8fd902..a59ad73 100644
--- a/test/test_trimfx.rb
+++ b/test/test_trimfx.rb
@@ -6,6 +6,10 @@ require 'dtas/format'
 require 'yaml'
 
 class TestTrimFX < Testcase
+  def rate
+    44100
+  end
+
   def test_example
     ex = YAML.load(File.read("examples/trimfx.sample.yml"))
     effects = []
@@ -14,8 +18,8 @@ class TestTrimFX < Testcase
       case words[0]
       when "trim"
         tfx = DTAS::TrimFX.new(words)
-        assert_equal 52.0, tfx.tbeg
-        assert_equal 1.0, tfx.tlen
+        assert_equal 52 * rate, tfx.tbeg
+        assert_equal rate, tfx.tlen
         effects << tfx
       end
     end
@@ -26,21 +30,21 @@ class TestTrimFX < Testcase
     tfx = DTAS::TrimFX.new(%w(all))
     assert_equal 0, tfx.tbeg
     assert_nil tfx.tlen
-    assert_equal [], tfx.to_sox_arg(DTAS::Format.new)
+    assert_equal [], tfx.to_sox_arg
   end
 
   def test_time
     tfx = DTAS::TrimFX.new(%w(trim 2:30 3.1))
-    assert_equal 150, tfx.tbeg
-    assert_equal 3.1, tfx.tlen
+    assert_equal 150 * rate, tfx.tbeg
+    assert_equal((3.1 * rate).round, tfx.tlen)
   end
 
   def test_to_sox_arg
     tfx = DTAS::TrimFX.new(%w(trim 1 0.5))
-    assert_equal %w(trim 44100s 22050s), tfx.to_sox_arg(DTAS::Format.new)
+    assert_equal %w(trim 44100s 22050s), tfx.to_sox_arg
 
     tfx = DTAS::TrimFX.new(%w(trim 1 sox vol -1dB))
-    assert_equal %w(trim 44100s), tfx.to_sox_arg(DTAS::Format.new)
+    assert_equal %w(trim 44100s), tfx.to_sox_arg
   end
 
   def test_tfx_effects
@@ -56,8 +60,8 @@ class TestTrimFX < Testcase
     ].shuffle
     ary = DTAS::TrimFX.schedule(fx)
     assert_operator 1, :==, ary.size
-    assert_equal [ 0.5, 1, 2 ], ary[0].map(&:tbeg)
-    assert_equal [ 0.5, 0.3, 0.2 ], ary[0].map(&:tlen)
+    assert_equal [ 22050, 44100, 88200 ], ary[0].map(&:tbeg)
+    assert_equal [ 22050, 13230, 8820 ], ary[0].map(&:tlen)
   end
 
   def test_schedule_overlaps
@@ -68,10 +72,10 @@ class TestTrimFX < Testcase
     ]
     ary = DTAS::TrimFX.schedule(fx)
     assert_equal 2, ary.size
-    assert_equal [ 0.5, 1 ], ary[0].map(&:tbeg)
-    assert_equal [ 1.1 ], ary[1].map(&:tbeg)
+    assert_equal [ 22050, 44100 ], ary[0].map(&:tbeg)
+    assert_equal [ 48510 ], ary[1].map(&:tbeg)
 
-    ex = DTAS::TrimFX.expand(fx, 10)
+    ex = DTAS::TrimFX.expand(fx, 10 * rate)
     assert_equal 2, ex.size
     assert_equal 0, ex[0][0].tbeg
     assert_equal 3, ex[0].size