[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/2] trimfx: flesh out parsing of commands
From: |
Eric Wong |
Subject: |
[PATCH 1/2] trimfx: flesh out parsing of commands |
Date: |
Sat, 19 Oct 2013 07:33:22 +0000 |
Only lightly tested, but this should give us some idea of where
we'll be going...
---
lib/dtas/parse_time.rb | 29 ++++++++++++++++++++++++++++
lib/dtas/trimfx.rb | 51 +++++++++++++++++++++++++-------------------------
test/test_trimfx.rb | 7 ++++++-
3 files changed, 61 insertions(+), 26 deletions(-)
create mode 100644 lib/dtas/parse_time.rb
diff --git a/lib/dtas/parse_time.rb b/lib/dtas/parse_time.rb
new file mode 100644
index 0000000..c2ca777
--- /dev/null
+++ b/lib/dtas/parse_time.rb
@@ -0,0 +1,29 @@
+# Copyright (C) 2013, Eric Wong <address@hidden> and all contributors
+# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+require_relative '../dtas'
+module DTAS::ParseTime
+ def parse_time(time)
+ case time
+ when /\A\d+\z/
+ time.to_i
+ when /\A[\d\.]+\z/
+ time.to_f
+ when /\A[:\d\.]+\z/
+ hhmmss = time.dup
+ rv = hhmmss.sub!(/\.(\d+)\z/, "") ? "0.#$1".to_f : 0
+
+ # deal with HH:MM:SS
+ t = hhmmss.split(/:/)
+ raise ArgumentError, "Bad time format: #{hhmmss}" if t.size > 3
+
+ mult = 1
+ while part = t.pop
+ rv += part.to_i * mult
+ mult *= 60
+ end
+ rv
+ else
+ raise ArgumentError, "unparseable: #{time.inspect}"
+ end
+ end
+end
diff --git a/lib/dtas/trimfx.rb b/lib/dtas/trimfx.rb
index cbb2fa6..794e657 100644
--- a/lib/dtas/trimfx.rb
+++ b/lib/dtas/trimfx.rb
@@ -1,11 +1,15 @@
# Copyright (C) 2013, Eric Wong <address@hidden> and all contributors
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require_relative '../dtas'
+require_relative 'parse_time'
require 'shellwords'
class DTAS::TrimFX
+ include DTAS::ParseTime
+
attr_reader :tbeg
attr_reader :tlen
+ attr_reader :cmd
def initialize(args)
args = args.dup
@@ -18,6 +22,28 @@ class DTAS::TrimFX
else
raise ArgumentError, "#{args.inspect} not understood"
end
+ case tmp = args.shift
+ when "sh" then @cmd = args
+ when "sox" then tfx_sox(args)
+ when "eca" then tfx_eca(args)
+ when nil
+ @cmd = []
+ else
+ raise ArgumentError, "unknown effect type: #{tmp}"
+ end
+ end
+
+ def tfx_sox(args)
+ @cmd = %w(sox $SOXIN $SOXOUT $TRIMFX)
+ @cmd.concat(args)
+ @cmd.concat(%w($FADEFX))
+ end
+
+ def tfx_eca(args)
+ @cmd = %w(sox $SOXIN $SOX2ECA $TRIMFX)
+ @cmd.concat(%w(| ecasound $ECAFMT -i stdin -o stdout))
+ @cmd.concat(args)
+ @cmd.concat(%w(| sox $ECA2SOX - $SOXOUT $FADEFX))
end
def to_sox_arg(format)
@@ -34,31 +60,6 @@ class DTAS::TrimFX
end
end
- def parse_time(tbeg)
- case tbeg
- when /\A\d+\z/
- tbeg.to_i
- when /\A[\d\.]+\z/
- tbeg.to_f
- when /\A[:\d\.]+\z/
- hhmmss = tbeg.dup
- rv = hhmmss.sub!(/\.(\d+)\z/, "") ? "0.#$1".to_f : 0
-
- # deal with HH:MM:SS
- t = hhmmss.split(/:/)
- raise ArgumentError, "Bad time format: #{hhmmss}" if t.size > 3
-
- mult = 1
- while part = t.pop
- rv += part.to_i * mult
- mult *= 60
- end
- rv
- else
- raise ArgumentError, "unparseable: #{tbeg.inspect}"
- end
- end
-
def parse_trim!(args)
tbeg = parse_time(args.shift)
if args[0] =~ /\A=?[\d\.]+\z/
diff --git a/test/test_trimfx.rb b/test/test_trimfx.rb
index 0321e3c..0ef7264 100644
--- a/test/test_trimfx.rb
+++ b/test/test_trimfx.rb
@@ -39,7 +39,12 @@ class TestTrimFX < Testcase
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))
+ tfx = DTAS::TrimFX.new(%w(trim 1 sox vol -1dB))
assert_equal %w(trim 44100s), tfx.to_sox_arg(DTAS::Format.new)
end
+
+ def test_tfx_effects
+ tfx = DTAS::TrimFX.new(%w(trim 1 sox vol -1dB))
+ assert_equal %w(sox $SOXIN $SOXOUT $TRIMFX vol -1dB $FADEFX), tfx.cmd
+ end
end
--
1.8.4.483.g7fe67e6.dirty
- [PATCH 1/2] trimfx: flesh out parsing of commands,
Eric Wong <=