about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-10-15 07:36:37 +0000
committerEric Wong <normalperson@yhbt.net>2013-10-15 07:36:37 +0000
commit1c77d0d7c414d43f751eeb308a1a47d87de6cc1e (patch)
tree890759d20f789b7f5f34d8e5b71ff09a8c7bedb4
parentbbd2a006152cce4e5fa28bb2793d239ebdfdb491 (diff)
downloaddtas-1c77d0d7c414d43f751eeb308a1a47d87de6cc1e.tar.gz
This will be dependent upon sox for the trim effect,
at least.  Other bits will be exposed as needed.
-rw-r--r--lib/dtas/trimfx.rb22
-rw-r--r--test/test_trimfx.rb10
2 files changed, 26 insertions, 6 deletions
diff --git a/lib/dtas/trimfx.rb b/lib/dtas/trimfx.rb
index 5cfac26..cbb2fa6 100644
--- a/lib/dtas/trimfx.rb
+++ b/lib/dtas/trimfx.rb
@@ -22,7 +22,13 @@ class DTAS::TrimFX
 
   def to_sox_arg(format)
     if @tbeg && @tlen
-      %W(trim #{@tbeg * format.rate}s #{@tlen * format.rate}s)
+      beg = @tbeg * format.rate
+      len = @tlen * format.rate
+      %W(trim #{beg.round}s #{len.round}s)
+    elsif @tbeg
+      return [] if @tbeg == 0
+      beg = @tbeg * format.rate
+      %W(trim #{beg.round}s)
     else
       []
     end
@@ -55,11 +61,15 @@ class DTAS::TrimFX
 
   def parse_trim!(args)
     tbeg = parse_time(args.shift)
-    tlen = args.shift
-    is_stop_time = tlen.sub!(/\A=/, "") ? true : false
-    tlen = parse_time(tlen)
-    if is_stop_time
-      tlen = tlen - tbeg
+    if args[0] =~ /\A=?[\d\.]+\z/
+      tlen = args.shift
+      is_stop_time = tlen.sub!(/\A=/, "") ? true : false
+      tlen = parse_time(tlen)
+      if is_stop_time
+        tlen = tlen - tbeg
+      end
+    else
+      tlen = nil
     end
     @tbeg = tbeg
     @tlen = tlen
diff --git a/test/test_trimfx.rb b/test/test_trimfx.rb
index 107946f..0321e3c 100644
--- a/test/test_trimfx.rb
+++ b/test/test_trimfx.rb
@@ -2,6 +2,7 @@
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 require './test/helper'
 require 'dtas/trimfx'
+require 'dtas/format'
 require 'yaml'
 
 class TestTrimFX < Testcase
@@ -25,6 +26,7 @@ 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)
   end
 
   def test_time
@@ -32,4 +34,12 @@ class TestTrimFX < Testcase
     assert_equal 150, tfx.tbeg
     assert_equal 3.1, 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)
+
+    tfx = DTAS::TrimFX.new(%w(trim 1 foo bar))
+    assert_equal %w(trim 44100s), tfx.to_sox_arg(DTAS::Format.new)
+  end
 end