From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 208.118.235.0/24 X-Spam-Status: No, score=-2.3 required=3.0 tests=AWL,BAYES_00,URIBL_BLOCKED shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: dtas-all@80x24.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id E2A3A1F5F0 for ; Mon, 29 Dec 2014 12:21:44 +0000 (UTC) Received: from localhost ([::1]:33094 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y5ZKS-00081t-3B for dtas-all@80x24.org; Mon, 29 Dec 2014 07:21:44 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36477) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y5ZKM-0007vA-1y for dtas-all@nongnu.org; Mon, 29 Dec 2014 07:21:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Y5ZKB-0005MJ-9Z for dtas-all@nongnu.org; Mon, 29 Dec 2014 07:21:33 -0500 Received: from dcvr.yhbt.net ([64.71.152.64]:36146) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y5ZKB-0005M7-1p for dtas-all@nongnu.org; Mon, 29 Dec 2014 07:21:27 -0500 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 3172A1F447 for ; Mon, 29 Dec 2014 12:21:25 +0000 (UTC) From: Eric Wong To: Subject: [PATCH] trimfx: require audio format at initialization Date: Mon, 29 Dec 2014 12:21:24 +0000 Message-Id: <1419855685-4476-1-git-send-email-e@80x24.org> X-Mailer: git-send-email 2.2.1.202.g55b961f X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 64.71.152.64 X-BeenThere: dtas-all@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dtas-all-bounces+dtas-all=80x24.org@nongnu.org Sender: dtas-all-bounces+dtas-all=80x24.org@nongnu.org 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