class Zappa::Clip
Attributes
cache[RW]
wav[RW]
Public Class Methods
new(wav = nil)
click to toggle source
# File lib/zappa/clip.rb, line 9 def initialize(wav = nil) if wav @wav = wav else @wav = Wave.new end @cache = nil @processor = Processor.new end
Public Instance Methods
+(other)
click to toggle source
# File lib/zappa/clip.rb, line 42 def +(other) return amplify(other) if other.class == Fixnum if other.class == Zappa::Clip fail 'format mismatch' unless @wav.format == other.wav.format w = Wave.new w.format = @wav.format samples = [] samples += @wav.samples if @wav.samples samples += other.wav.samples if other.wav.samples w.set_samples(samples) return Clip.new(w) end fail "cannot add Zappa::Clip to #{other.class}" end
amplify(db)
click to toggle source
# File lib/zappa/clip.rb, line 69 def amplify(db) clone(@processor.amplify(@wav.samples, db)) end
compress(ratio = 2.0, threshold = - 20.0)
click to toggle source
# File lib/zappa/clip.rb, line 65 def compress(ratio = 2.0, threshold = - 20.0) clone(@processor.compress(@wav.samples, ratio, threshold)) end
export(path)
click to toggle source
# File lib/zappa/clip.rb, line 24 def export(path) persist_cache if @cache.nil? cmd = 'ffmpeg -i ' + @cache + ' -y -f wav ' + path Open3.popen3(cmd) do |_stdin, _stdout, _stderr, wait_thr| fail 'Cannot export to' + path unless wait_thr.value.success? end end
from_file(path)
click to toggle source
# File lib/zappa/clip.rb, line 19 def from_file(path) @wav.unpack(path) persist_cache end
invert()
click to toggle source
# File lib/zappa/clip.rb, line 73 def invert clone(@processor.invert(@wav.samples)) end
normalize(headroom)
click to toggle source
Processor
Wrappers
# File lib/zappa/clip.rb, line 61 def normalize(headroom) clone(@processor.normalize(@wav.samples, headroom)) end
slice(pos, len)
click to toggle source
# File lib/zappa/clip.rb, line 32 def slice(pos, len) slice_samples(ms_to_samples(pos), ms_to_samples(len)) end
slice_samples(pos, len)
click to toggle source
# File lib/zappa/clip.rb, line 36 def slice_samples(pos, len) fail 'invalid index' if pos < 0 || (pos + len) > @wav.sample_count slice = @wav.samples[pos, len] clone(slice) end
Private Instance Methods
clone(samples = nil)
click to toggle source
# File lib/zappa/clip.rb, line 97 def clone(samples = nil) clone = Clip.new clone.wav = Marshal.load(Marshal.dump(@wav)) clone.wav.set_samples(samples) if samples clone end
ffmpeg_wav_export(source, destination)
click to toggle source
# File lib/zappa/clip.rb, line 89 def ffmpeg_wav_export(source, destination) cmd = 'ffmpeg -i ' + source + ' -vn -y -f wav ' + destination Open3.popen3(cmd) do |_stdin, _stdout, _stderr, wait_thr| fail 'Cannot open file ' + path unless wait_thr.value.success? end destination end
ms_to_samples(ms)
click to toggle source
# File lib/zappa/clip.rb, line 79 def ms_to_samples(ms) (ms * @wav.format.sample_rate / 1000).round end
persist_cache()
click to toggle source
# File lib/zappa/clip.rb, line 83 def persist_cache tmp = Tempfile.new('zappa') @cache = tmp.path File.write(@cache, @wav.pack) end