diff options
author | Eric Wong <normalperson@yhbt.net> | 2013-10-13 08:54:05 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2013-10-13 08:57:06 +0000 |
commit | bbd2a006152cce4e5fa28bb2793d239ebdfdb491 (patch) | |
tree | dd4569a64dc6ef337d9d06187fffdad8a25174c6 /lib/dtas/trimfx.rb | |
parent | 36d6b71a4bcf476f3acda6b249486844d76128ef (diff) | |
download | dtas-bbd2a006152cce4e5fa28bb2793d239ebdfdb491.tar.gz |
This will allow editing individual portions of audio of a larger file while creating cross fade effects to join them.
Diffstat (limited to 'lib/dtas/trimfx.rb')
-rw-r--r-- | lib/dtas/trimfx.rb | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/dtas/trimfx.rb b/lib/dtas/trimfx.rb new file mode 100644 index 0000000..5cfac26 --- /dev/null +++ b/lib/dtas/trimfx.rb @@ -0,0 +1,67 @@ +# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors +# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) +require_relative '../dtas' +require 'shellwords' + +class DTAS::TrimFX + attr_reader :tbeg + attr_reader :tlen + + def initialize(args) + args = args.dup + case args.shift + when "trim" + parse_trim!(args) + when "all" + @tbeg = 0 + @tlen = nil + else + raise ArgumentError, "#{args.inspect} not understood" + end + end + + def to_sox_arg(format) + if @tbeg && @tlen + %W(trim #{@tbeg * format.rate}s #{@tlen * format.rate}s) + else + [] + 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) + tlen = args.shift + is_stop_time = tlen.sub!(/\A=/, "") ? true : false + tlen = parse_time(tlen) + if is_stop_time + tlen = tlen - tbeg + end + @tbeg = tbeg + @tlen = tlen + end +end |