Class: Castaway::Timeline

Inherits:
Object
  • Object
show all
Defined in:
lib/castaway/timeline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolution, fps) ⇒ Timeline

Returns a new instance of Timeline.



9
10
11
12
13
14
15
16
# File 'lib/castaway/timeline.rb', line 9

def initialize(resolution, fps)
  @resolution = resolution
  @elements = []
  @fps = fps

  @cached_command = nil
  @cached_file = nil
end

Instance Attribute Details

#fpsObject (readonly)

Returns the value of attribute fps.



7
8
9
# File 'lib/castaway/timeline.rb', line 7

def fps
  @fps
end

#resolutionObject (readonly)

Returns the value of attribute resolution.



7
8
9
# File 'lib/castaway/timeline.rb', line 7

def resolution
  @resolution
end

Instance Method Details



60
61
62
63
64
65
# File 'lib/castaway/timeline.rb', line 60

def _link(old, new)
  # FIXME: detect whether linking is supported, and fallback to copy
  # if not.

  FileUtils.ln(old, new)
end

#_log(frame, t, msg) ⇒ Object



75
76
77
# File 'lib/castaway/timeline.rb', line 75

def _log(frame, t, msg)
  _logger.info { format('[%d:%.2fs] %s', frame, t, msg) }
end

#_loggerObject



67
68
69
70
71
72
73
# File 'lib/castaway/timeline.rb', line 67

def _logger
  @_logger ||= Logger.new('build.log').tap do |logger|
    logger.formatter = lambda do |severity, _datetime, _progname, msg|
      "#{severity}: #{msg}\n"
    end
  end
end

#add(element) ⇒ Object



18
19
20
# File 'lib/castaway/timeline.rb', line 18

def add(element)
  @elements << element
end

#durationObject



22
23
24
# File 'lib/castaway/timeline.rb', line 22

def duration
  @elements.map(&:t2).max
end

#render_frame(frame, name: 'frame') ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/castaway/timeline.rb', line 26

def render_frame(frame, name: 'frame')
  t = frame / fps.to_f

  signature = nil
  tool = MiniMagick::Tool::Convert.new.tap do |convert|
    convert << '-size' << resolution.to_geometry
    convert.xc 'black'

    @elements.sort_by(&:t1).each do |element|
      next unless element.alive_at?(t)
      element.render_at(t, convert)
    end

    convert.colorspace 'sRGB'
    convert.type 'TrueColor'
    convert.depth '16'

    signature = convert.command
    convert << "PNG48:#{name}.png"
  end

  if signature != @cached_command
    _log frame, t, tool.command.join(' ')
    @cached_command = signature
    @cached_file = name
    tool.call
  else
    old = "#{@cached_file}.png"
    new = "#{name}.png"
    _log frame, t, "duplicate #{old} as #{new}"
    _link old, new
  end
end