class MovieMasher::TimeRange

a time span at a rate

Attributes

length[RW]
rate[RW]
start[RW]

Public Class Methods

input_length(input) click to toggle source
# File lib/util/time_range.rb, line 7
def input_length(input)
  input_time(input, :length)
end
input_time(input, key) click to toggle source
# File lib/util/time_range.rb, line 11
def input_time(input, key)
  time = FloatUtil::ZERO
  duration = input[:duration].to_f
  rel_key = "#{key.id2name}_is_relative".to_sym
  val_key = "#{key.id2name}_relative_value".to_sym
  if input[rel_key] && input[val_key]
    if FloatUtil.gtr(duration, FloatUtil::ZERO)
      time = relative_time(input[rel_key], input[val_key], duration)
    end
  elsif FloatUtil.gtr(input[key], FloatUtil::ZERO)
    time = input[key]
  elsif key == :length && FloatUtil.gtr(duration, FloatUtil::ZERO)
    time = duration - input_trim(input)
  end
  FloatUtil.precision(time)
end
input_trim(input) click to toggle source
# File lib/util/time_range.rb, line 28
def input_trim(input)
  input_time(input, :offset)
end
input_trim_range(input) click to toggle source
# File lib/util/time_range.rb, line 32
def input_trim_range(input)
  range = new(input_trim(input), 1, 1)
  range.length = input_length(input)
  range
end
new(start = 0, rate = 0, length = 1) click to toggle source
# File lib/util/time_range.rb, line 151
def initialize(start = 0, rate = 0, length = 1)
  @length = length
  @start = start.to_i
  @rate = rate.to_i
end
update(inputs, outputs) click to toggle source
# File lib/util/time_range.rb, line 38
def update(inputs, outputs)
  output_duration = update_inputs(inputs)
  update_outputs(outputs, output_duration)
  output_duration
end
update_inputs(inputs) click to toggle source
# File lib/util/time_range.rb, line 44
def update_inputs(inputs)
  start_audio = FloatUtil::ZERO
  start_video = FloatUtil::ZERO
  inputs.each do |input|
    if FloatUtil.cmp(input[:start], FloatUtil::NEG_ONE)
      input[:start] =
        if input[:no_video] || input[:no_audio]
          (input[:no_video] ? start_audio : start_video)
        else
          input[:start] = [start_audio, start_video].max
        end
    end
    length = input_length(input)
    start_video = input[:start] + length unless input[:no_video]
    start_audio = input[:start] + length unless input[:no_audio]
    input[:length] = length
    input[:range] = input_trim_range(input)
    input[:offset] = input_trim(input)
  end

  FloatUtil.max(start_video, start_audio)
end
update_outputs(outputs, output_duration) click to toggle source
# File lib/util/time_range.rb, line 67
def update_outputs(outputs, output_duration)
  outputs.each do |output|
    output[:duration] = output_duration
    if Type::SEQUENCE == output[:type]
      padding = (output[:video_rate].to_f * output_duration)
      output[:sequence] = "%0#{padding.floor.to_i.to_s.length}d"
    end
  end
end

Public Instance Methods

dup() click to toggle source
# File lib/util/time_range.rb, line 128
def dup
  TimeRange.new @start, @length, @rate
end
end_seconds() click to toggle source
# File lib/util/time_range.rb, line 132
def end_seconds
  TimeRange.new(stop, rate).start_seconds
end
equals?(range) click to toggle source
# File lib/util/time_range.rb, line 136
def equals?(range)
  return false unless range
  return false unless rate.positive? && !range.start.negative?

  if rate == range.rate
    ((start == range.start) && (length == range.length))
  else
    # make copies so neither range is changed
    range1 = dup
    range2 = range.dup
    range1.synchronize range2
    range1.start == range2.start && range1.length == range2.length
  end
end
intersection(range) click to toggle source
# File lib/util/time_range.rb, line 157
def intersection(range)
  result = nil
  range1 = self
  range2 = range
  if range1.rate != range2.rate
    range1 = range1.dup
    range2 = range2.dup
    range1.synchronize range2
  end
  last_start = [range1.start, range2.start].max
  first_end = [range1.stop, range2.stop].min
  if last_start < first_end
    result = TimeRange.new(last_start, range1.rate, first_end - last_start)
  end
  result
end
length_seconds(precision = 3) click to toggle source
# File lib/util/time_range.rb, line 174
def length_seconds(precision = 3)
  FloatUtil.precision((@length.to_f / @rate), precision)
end
start_seconds(precision = 3) click to toggle source
# File lib/util/time_range.rb, line 178
def start_seconds(precision = 3)
  FloatUtil.precision((@start.to_f / @rate), precision)
end
stop() click to toggle source
# File lib/util/time_range.rb, line 182
def stop
  start + length
end
synchronize(time, rounding = :round) click to toggle source
# File lib/util/time_range.rb, line 186
def synchronize(time, rounding = :round)
  return unless time
  return unless time.respond_to?(:rate) && time.respond_to?(:scale)
  return if time.rate == @rate

  gcf = TimeRange.lcm(time.rate, @rate)
  scale(gcf, rounding)
  TimeRange.scale(time, gcf, rounding)
end