everything related to duct tape audio suite (dtas)
 help / color / mirror / Atom feed
* [PATCH] trimfx: require audio format at initialization
@ 2014-12-29 12:21 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2014-12-29 12:21 UTC (permalink / raw)
  To: dtas-all

This makes things easier for scheduling/expansion since we
won't have to deal with floating point numbers when we work
directly with with sample counts (like the rest of dtas).
---
 lib/dtas/trimfx.rb  | 32 +++++++++++++++++---------------
 test/test_trimfx.rb | 28 ++++++++++++++++------------
 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
-- 
EW



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-12-29 12:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-29 12:21 [PATCH] trimfx: require audio format at initialization Eric Wong

everything related to duct tape audio suite (dtas)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://80x24.org/dtas-all
	git clone --mirror http://ou63pmih66umazou.onion/dtas-all

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 dtas-all dtas-all/ https://80x24.org/dtas-all \
		dtas-all@nongnu.org
	public-inbox-index dtas-all

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.audio.dtas
	nntp://ou63pmih66umazou.onion/inbox.comp.audio.dtas
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for the project(s) associated with this inbox:

	../../../dtas.git

AGPL code for this site: git clone http://ou63pmih66umazou.onion/public-inbox.git