Class: SMPTE::SMPTE

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/smpte/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, df = nil) ⇒ SMPTE

This can be initialized in one of four ways:

smpte string - with or without frame count integer frame count floating point frame count another smpte object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/smpte/base.rb', line 87

def initialize(string, df=nil)
  if string.kind_of?(String) && string.to_s =~ /(\d\d):(\d\d):(\d\d)(?:([:;])(\d\d)(\.\d+)?)?/
    @hours, @minutes, @seconds, sep, @frames, @subframes = $1.to_i, $2.to_i, $3.to_i, $4, $5, $6
    @has_frames = !@frames.nil?
    @frames = 0 unless @frames
    @frames = @frames.to_i
    @has_subframes = !@subframes.nil?
    @subframes = @subframes.to_f   # note that nil.to_f == 0.0
    @frame_count = (((@hours * 60) + @minutes) * 60 + @seconds) * 30 + @frames
    if df.to_s == 'df' || df.to_s == 'drop frame' || sep == ';'
      @df = 'df'
    else
      @df = 'ndf'
    end
    if @df == 'df'
      # Every hour, we drop 108 frame #'s.  Every minute, we drop 2 frame
      # #'s, except for the even 10 minutes (00, 10, 20, 30, 40, & 50).
      # The numbers that are dropped are frames 00 and 01, meaning that we
      # have to subtract two for each minute > 0.
      #
      # Consider, 00:02:59:29, next frame is 00:03:00:02, so 00 and 01 were
      # skipped.  This is going to make to_s fun fun fun...
      @frame_count -= (@hours * 108)
      @frame_count -= ((@minutes/10).floor * 18)
      # In reality, this is a sanity check.  03:01:00:01 isn't a valid
      # drop-frame smpte code.
      # But if it shows up, I'm not going to subtract two frames for it.
      if @minutes%10 >= 1
        if @frames >=2 || @seconds > 0
          @frame_count -= (@minutes%10)*2
        else
          @frame_count -= ((@minutes%10)-1)*2
        end
      end
    end
  elsif string.kind_of?(SMPTE)
    @frame_count = string.to_i
    @has_frames = string.has_frames
    @has_subframes = string.has_subframes
    @df = df.nil? ? string.df : ((df.to_s=='df' || df.to_s=='drop frame') ? 'df' : 'ndf')
    @hours = string.hours
    @minutes = string.minutes
    @seconds = string.seconds
    @frames = string.frames
    @subframes = string.subframes
  elsif string.kind_of?(Integer)
    @frame_count = string.to_i
    @has_frames = true
    @has_subframes = false
    @df = (df.to_s=='df' || df.to_s=='drop frame') ? 'df' : 'ndf'
    compute_times
  elsif string.kind_of?(Float)
    @frame_count = string.floor
    @subframes = string - @frame_count.to_f
    @has_frames = true
    @has_subframes = true
    @df = (df.to_s=='df' || df.to_s=='drop frame') ? 'df' : 'ndf'
    compute_times
  else
    raise InvalidParameterError
  end
  if @has_subframes
    @frame_count = @frame_count.to_f + @subframes
  end
end

Instance Attribute Details

#dfObject (readonly)

Returns the value of attribute df.



75
76
77
# File 'lib/smpte/base.rb', line 75

def df
  @df
end

#frame_countObject (readonly)

Returns the value of attribute frame_count.



75
76
77
# File 'lib/smpte/base.rb', line 75

def frame_count
  @frame_count
end

#framesObject (readonly)

Returns the value of attribute frames.



75
76
77
# File 'lib/smpte/base.rb', line 75

def frames
  @frames
end

#has_framesObject (readonly)

Returns the value of attribute has_frames.



75
76
77
# File 'lib/smpte/base.rb', line 75

def has_frames
  @has_frames
end

#has_subframesObject (readonly)

Returns the value of attribute has_subframes.



75
76
77
# File 'lib/smpte/base.rb', line 75

def has_subframes
  @has_subframes
end

#hoursObject (readonly)

Returns the value of attribute hours.



75
76
77
# File 'lib/smpte/base.rb', line 75

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



75
76
77
# File 'lib/smpte/base.rb', line 75

def minutes
  @minutes
end

#secondsObject (readonly)

Returns the value of attribute seconds.



75
76
77
# File 'lib/smpte/base.rb', line 75

def seconds
  @seconds
end

#subframesObject (readonly)

Returns the value of attribute subframes.



75
76
77
# File 'lib/smpte/base.rb', line 75

def subframes
  @subframes
end

Class Method Details

.valid?(string) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/smpte/base.rb', line 77

def self.valid?(string)
  string.to_s.match(/(\d\d):(\d\d):(\d\d)(?:[:;](\d\d)(\.\d+)?)?/)
end

Instance Method Details

#+(more_frames) ⇒ Object

Adds a number of frames to a SMPTE



170
171
172
173
174
175
176
# File 'lib/smpte/base.rb', line 170

def +(more_frames)
  if @has_subframes
    self.class.new(@frame_count.to_f + @subframe_count + more_frames.to_f, @df)
  else
    self.class.new(@frame_count + more_frames.to_i, @df)
  end
end

#-(other) ⇒ Object

Returns difference between two SMPTEs as a SMPTE - use to_i to get a frame count.



165
166
167
# File 'lib/smpte/base.rb', line 165

def -(other)
  self.class.new(@frame_count - other.frame_count, @df)
end

#<=>(other) ⇒ Object

Comparision operator to make Comparable work



159
160
161
# File 'lib/smpte/base.rb', line 159

def <=>(other)
  @frame_count - other.frame_count
end

#eql?(other) ⇒ Boolean

Check equality (equal frame counts)

Returns:

  • (Boolean)


154
155
156
# File 'lib/smpte/base.rb', line 154

def eql?(other)
  @frame_count == other.frame_count
end

#to_dfObject

Returns a SMPTE with same frame count but forced to drop frame format.



179
180
181
182
183
184
185
# File 'lib/smpte/base.rb', line 179

def to_df
  if @df=='df'
    self.dup
  else
    self.class.new(@frame_count,'df')
  end
end

#to_fObject

Returns frame count as float - with subframe count where applicable



271
272
273
# File 'lib/smpte/base.rb', line 271

def to_f
  @frame_count.to_f
end

#to_iObject

Return the frame count



257
258
259
260
261
262
263
# File 'lib/smpte/base.rb', line 257

def to_i
  if @has_subframes
    @frame_count.round
  else
    @frame_count
  end
end

#to_intObject

Alias for to_i



266
267
268
# File 'lib/smpte/base.rb', line 266

def to_int
  to_i
end

#to_ndfObject

Returns a SMPTE with same frame count but forced to non-drop frame format.



188
189
190
191
192
193
194
# File 'lib/smpte/base.rb', line 188

def to_ndf
  if @df=='ndf'
    self.dup
  else
    self.class.new(@frame_count,'ndf')
  end
end

#to_sObject

Converts to string format



241
242
243
244
245
246
247
248
249
# File 'lib/smpte/base.rb', line 241

def to_s
  if @has_subframes
    sprintf("%02d:%02d:%02d%s%02d.%02d", @hours, @minutes, @seconds, (@df=='df' ? ';' : ':'), @frames, (@subframes * 100).floor)
  elsif @has_frames
    sprintf("%02d:%02d:%02d%s%02d", @hours, @minutes, @seconds, (@df=='df' ? ';' : ':'), @frames)
  else
    sprintf("%02d:%02d:%02d", @hours, @minutes, @seconds)
  end
end

#to_strObject

Alias for to_s



252
253
254
# File 'lib/smpte/base.rb', line 252

def to_str
  to_s
end