Class: HeadMusic::Time::SmpteTimecode
- Inherits:
-
Object
- Object
- HeadMusic::Time::SmpteTimecode
- Includes:
- Comparable, RadixCarry
- Defined in:
- lib/head_music/time/smpte_timecode.rb
Overview
Represents a SMPTE (Society of Motion Picture and Television Engineers) timecode
SMPTE timecode is used for synchronizing audio and video in professional production. It represents time as HH:MM:SS:FF (hours:minutes:seconds:frames).
The framerate determines how many frames occur per second. Common framerates: - 24 fps: Film standard - 25 fps: PAL video standard (Europe) - 30 fps: NTSC video standard (North America) - 29.97 fps: NTSC drop-frame
Constant Summary collapse
- DEFAULT_FRAMERATE =
Default framerate (30 fps NTSC)
30- SECONDS_PER_MINUTE =
Seconds per minute
60- MINUTES_PER_HOUR =
Minutes per hour
60
Instance Attribute Summary collapse
-
#frame ⇒ Integer
readonly
The frame component.
-
#framerate ⇒ Integer
readonly
Frames per second (default: 30 for NTSC).
-
#hour ⇒ Integer
readonly
The hour component.
-
#minute ⇒ Integer
readonly
The minute component.
-
#second ⇒ Integer
readonly
The second component.
Class Method Summary collapse
-
.parse(identifier) ⇒ SmpteTimecode
Parse a timecode from a string representation.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare this timecode to another.
-
#carry(component, radix) ⇒ Integer
included
from RadixCarry
private
Divide the named component by its radix, store the remainder back, and return the amount to carry into the next-higher component.
-
#initialize(hour = 0, minute = 0, second = 0, frame = 0, framerate: DEFAULT_FRAMERATE) ⇒ SmpteTimecode
constructor
Create a new SMPTE timecode.
-
#normalize! ⇒ self
Normalize the timecode, handling overflow and underflow.
-
#to_a ⇒ Array<Integer>
Convert timecode to array format.
-
#to_s ⇒ String
Convert timecode to string format with zero padding.
-
#to_total_frames ⇒ Integer
(also: #to_i)
Convert timecode to total frames from the beginning.
Constructor Details
#initialize(hour = 0, minute = 0, second = 0, frame = 0, framerate: DEFAULT_FRAMERATE) ⇒ SmpteTimecode
Create a new SMPTE timecode
79 80 81 82 83 84 85 86 |
# File 'lib/head_music/time/smpte_timecode.rb', line 79 def initialize(hour = 0, minute = 0, second = 0, frame = 0, framerate: DEFAULT_FRAMERATE) @hour = hour.to_i @minute = minute.to_i @second = second.to_i @frame = frame.to_i @framerate = framerate @total_frames = nil end |
Instance Attribute Details
#frame ⇒ Integer (readonly)
Returns the frame component.
48 49 50 |
# File 'lib/head_music/time/smpte_timecode.rb', line 48 def frame @frame end |
#framerate ⇒ Integer (readonly)
Returns frames per second (default: 30 for NTSC).
51 52 53 |
# File 'lib/head_music/time/smpte_timecode.rb', line 51 def framerate @framerate end |
#hour ⇒ Integer (readonly)
Returns the hour component.
39 40 41 |
# File 'lib/head_music/time/smpte_timecode.rb', line 39 def hour @hour end |
#minute ⇒ Integer (readonly)
Returns the minute component.
42 43 44 |
# File 'lib/head_music/time/smpte_timecode.rb', line 42 def minute @minute end |
#second ⇒ Integer (readonly)
Returns the second component.
45 46 47 |
# File 'lib/head_music/time/smpte_timecode.rb', line 45 def second @second end |
Class Method Details
.parse(identifier) ⇒ SmpteTimecode
Parse a timecode from a string representation
68 69 70 |
# File 'lib/head_music/time/smpte_timecode.rb', line 68 def self.parse(identifier) new(*identifier.scan(/\d+/)[0..3]) end |
Instance Method Details
#<=>(other) ⇒ Integer
Compare this timecode to another
128 129 130 |
# File 'lib/head_music/time/smpte_timecode.rb', line 128 def <=>(other) to_total_frames <=> other.to_total_frames end |
#carry(component, radix) ⇒ Integer (private) Originally defined in module RadixCarry
Divide the named component by its radix, store the remainder back, and return the amount to carry into the next-higher component.
#normalize! ⇒ self
Normalize the timecode, handling overflow and underflow
This method modifies the timecode in place, carrying excess values from lower levels to higher levels (frames → seconds → minutes → hours). Also handles negative values by borrowing from higher levels.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/head_music/time/smpte_timecode.rb', line 112 def normalize! @total_frames = nil # Invalidate cached value # Carry overflow (and borrow underflow) up through each level. # divmod handles both in-range and out-of-range values uniformly. @second += carry(:frame, framerate) @minute += carry(:second, SECONDS_PER_MINUTE) @hour += carry(:minute, MINUTES_PER_HOUR) self end |
#to_a ⇒ Array<Integer>
Convert timecode to array format
91 92 93 |
# File 'lib/head_music/time/smpte_timecode.rb', line 91 def to_a [hour, minute, second, frame] end |
#to_s ⇒ String
Convert timecode to string format with zero padding
98 99 100 |
# File 'lib/head_music/time/smpte_timecode.rb', line 98 def to_s format("%02d:%02d:%02d:%02d", hour, minute, second, frame) end |
#to_total_frames ⇒ Integer Also known as: to_i
Convert timecode to total frames from the beginning
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/head_music/time/smpte_timecode.rb', line 135 def to_total_frames return @total_frames if @total_frames frames_per_minute = SECONDS_PER_MINUTE * framerate frames_per_hour = MINUTES_PER_HOUR * frames_per_minute @total_frames = hour * frames_per_hour + minute * frames_per_minute + second * framerate + frame end |