Class: OverLayer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ OverLayer

Returns a new instance of OverLayer.


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/overlayer.rb', line 99

def initialize filename
  @filename = filename
  @am_muted = false
  @am_blanked = false
  @mutex = Mutex.new
  @cv = ConditionVariable.new
  @file_mtime = nil
  check_reload_yaml
  @just_unblanked = false
  @start_time = Time.now_f # assume they want to start immediately...
end

Instance Attribute Details

#all_sequencesObject

Returns the value of attribute all_sequences


72
73
74
# File 'lib/overlayer.rb', line 72

def all_sequences
  @all_sequences
end

Class Method Details

.new_raw(ruby_hash) ⇒ Object


94
95
96
97
# File 'lib/overlayer.rb', line 94

def self.new_raw ruby_hash
  File.write 'temp.yml', YAML.dump(ruby_hash)
  OverLayer.new('temp.yml')
end

.translate_file(filename) ⇒ Object

note this is actually deprecated and won't work anymore <sigh> and needs to be updated.


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
# File 'lib/overlayer.rb', line 112

def self.translate_file filename
  begin
    all = EdlParser.parse_file(filename)
  rescue NoMethodError, ArgumentError => e
    p 'appears your file has a syntax error in it--perhaps missing quotation marks?', e.to_s
    raise e # hope this is ok...
  end
  
  # now it's like {:mutes => {"1:02.0" => "1:3.0"}}
  # translate to floats like 62.0 => 63.0

  for type in [:mutes, :blank_outs] # LODO standardize these :mutes and :blank_outs somewhere...scary to have them repeated *everywhere*
    maps = all[type] || all[type.to_s] || {}
    new = {}
    maps.each{|start,endy|
      # both are like 1:02.0
      start2 = EdlParser.translate_string_to_seconds(start) if start
      endy2 = EdlParser.translate_string_to_seconds(endy) if endy
      if start2 == 0 || endy2 == 0 || start == nil || endy == nil
        p 'warning--possible error in the Edit Decision List file some line not parsed! (NB if you want one to start at time 0 please use 0.0001)', start, endy unless $AM_IN_UNIT_TEST
        # drop this line into the bitbucket...
        next
      end
      
      if start2 == endy2 || endy2 < start2
        p 'warning--found a line that had poor interval', start, endy, type unless defined?($AM_IN_UNIT_TEST)
        next
      end
      if(endy2 > 60*60*3)
        p 'warning--found setting past 3 hours [?]', start, endy, type unless defined?($AM_IN_UNIT_TEST)
      end
      new[start2] = endy2
    }
    all.delete(type.to_s) # cleanup
    all[type] = new.sort
  end
  all
end

Instance Method Details

#blank!(seconds) ⇒ Object


51
52
53
54
# File 'lib/overlayer.rb', line 51

def blank! seconds
  @am_blanked = true
  Blanker.blank_full_screen! seconds
end

#blank?Boolean

Returns:

  • (Boolean)

35
36
37
# File 'lib/overlayer.rb', line 35

def blank?
  @am_blanked
end

#check_reload_yamlObject


62
63
64
65
66
67
68
69
70
# File 'lib/overlayer.rb', line 62

def check_reload_yaml
  current_mtime = File.stat(@filename).mtime
  if @file_mtime != current_mtime
    reload_yaml!
    @file_mtime = current_mtime 
  else
    #p 'same mtime:', @file_mtime if $DEBUG && $VERBOSE
  end
end

#continue_until_past_all(continue_forever) ⇒ Object


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/overlayer.rb', line 307

def continue_until_past_all continue_forever
  if RUBY_VERSION < '1.9.2'
    raise 'need 1.9.2+ for MRI for the mutex stuff' unless RUBY_PLATFORM =~ /java/
  end

  @mutex.synchronize {
    loop {
      muted, blanked, next_point = get_current_state
      if next_point == :done
        if continue_forever
          time_till_next_mute_starts = 1_000_000
        else
          #set_states! # for the unit tests' sake
          return
        end
      else
        time_till_next_mute_starts = next_point - cur_time
      end
      
     # pps 'sleeping until next action (%s) begins in %fs (%f) %f' % [next_point, time_till_next_mute_starts, Time.now_f, cur_time] if $VERBOSE
      
      @cv.wait(@mutex, time_till_next_mute_starts) if time_till_next_mute_starts > 0
      set_states!
    }
  }
end

#cur_english_timeObject


168
169
170
# File 'lib/overlayer.rb', line 168

def cur_english_time
  EdlParser.translate_time_to_human_readable(cur_time)
end

#cur_timeObject

returns seconds it's at currently…


164
165
166
# File 'lib/overlayer.rb', line 164

def cur_time
  Time.now_f - @start_time
end

#discover_state(type, cur_time) ⇒ Object

returns [start, end, active|:done]


259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/overlayer.rb', line 259

def discover_state type, cur_time
    for start, endy in @all_sequences[type]
      if cur_time < endy
        # first one that we haven't passed the *end* of yet
        if(cur_time >= start)
          return [start, endy, true]
        else
          return [start, endy, false]
        end
      end
      
    end
    return [nil, nil, :done]
end

#display_change(change) ⇒ Object


334
335
336
337
338
339
# File 'lib/overlayer.rb', line 334

def display_change change
  puts '' unless defined?($AM_IN_UNIT_TEST)
  if $VERBOSE
    puts change + ' at ' + cur_english_time
  end    
end

#get_current_stateObject

returns [true, false, next_moment_of_importance|:done] ( true, false for if it should be currently muted, blanked )


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/overlayer.rb', line 277

def get_current_state
  all = []
  time = cur_time
  for type in [:mutes, :blank_outs] do
    all << discover_state(type, time)
  end
  output = []
  # all is [[start, end, active]...] or [:done, :done]
  earliest_moment = 1_000_000
  all.each{|start, endy, active|
    if active == :done
      output << false
      next
    else
      output << active
    end
    if active
      earliest_moment = [earliest_moment, endy].min
    else
      earliest_moment = [earliest_moment, start].min
    end
  }
  if earliest_moment == 1_000_000
    output << :done
  else
    output << earliest_moment
  end
  output
end

#keyboard_input(char) ⇒ Object


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/overlayer.rb', line 189

def keyboard_input char
  delta = case char
    when 'h' then 60*60
    when 'H' then -60*60
    when 'm' then 60
    when 'M' then -60
    when 's' then 1
    when 'S' then -1
    when 't' then 0.1
    when 'q' then
      puts '','quitting'
      exit(1)        
    when 'T' then -0.1
    when 'v' then
      $VERBOSE = !$VERBOSE
      p 'set verbose to ', $VERBOSE
      return
    when 'd'
      $DEBUG = !$DEBUG
      p 'set debug to', $DEBUG
      return
    when ' ' then
      puts 'timestamp:' + cur_english_time
      return
    when 'r' then
      reload_yaml!
      puts
      return
    else nil
  end
  if delta
    set_seconds(cur_time + delta)
  else
    puts 'invalid char: [' + char + ']'
  end
end

#kill_thread!Object


254
255
256
# File 'lib/overlayer.rb', line 254

def kill_thread!
  @thread.kill
end

#mute!Object


39
40
41
42
43
# File 'lib/overlayer.rb', line 39

def mute!
  @am_muted = true
  puts 'muting!' if $VERBOSE
  Muter.mute! unless defined?($AM_IN_UNIT_TEST)
end

#muted?Boolean

Returns:

  • (Boolean)

31
32
33
# File 'lib/overlayer.rb', line 31

def muted?
  @am_muted
end

#pretty_sequencesObject


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/overlayer.rb', line 80

def pretty_sequences
  new_sequences = {}
  @all_sequences.each{|type, values|
    if values.is_a? Array
      new_sequences[type] = values.map{|s, f|
        [EdlParser.translate_time_to_human_readable(s), EdlParser.translate_time_to_human_readable(f)]
      }
    else
      new_sequences[type] = values
    end
  }
  new_sequences
end

#reload_yaml!Object


74
75
76
77
78
# File 'lib/overlayer.rb', line 74

def reload_yaml!
  @all_sequences = OverLayer.translate_file(@filename)
  puts '(re) loaded mute sequences from ' + File.basename(@filename) + ' as', pretty_sequences.pretty_inspect, "" unless defined?($AM_IN_UNIT_TEST)
  signal_change
end

#set_seconds(seconds) ⇒ Object

sets it to a new set of seconds…


227
228
229
230
231
232
233
# File 'lib/overlayer.rb', line 227

def set_seconds seconds
  seconds = [seconds, 0].max
  @mutex.synchronize {
    @start_time = Time.now_f - seconds
  }
  signal_change
end

#set_states!Object


341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/overlayer.rb', line 341

def set_states!
  should_be_muted, should_be_blank, next_point = get_current_state
  
  if should_be_muted && !muted?
    mute!
    display_change 'muted'
  end
  
  if !should_be_muted && muted?
    unmute!      
    display_change 'unmuted'
  end
  
  if should_be_blank && !blank?
    blank! "%.02f" % (next_point - cur_time)
    mute! # mute with blanks currently...
    display_change 'blanked'
  end

  if !should_be_blank && blank?
    unblank!
    display_change 'unblanked'
  end
  
end

#signal_changeObject


235
236
237
238
239
240
241
# File 'lib/overlayer.rb', line 235

def signal_change
  @mutex.synchronize {
    # tell the driver thread to wake up and re-check state. 
    # We're not super thread friendly but good enough for having two contact each other...
    @cv.signal
  }
end

#start_thread(continue_forever = false) ⇒ Object

we have a single scheduler thread, that is notified when the time may have changed like def restart new_time

@current_time = xxx
broadcast # things have changed

end


250
251
252
# File 'lib/overlayer.rb', line 250

def start_thread continue_forever = false
  @thread = Thread.new { continue_until_past_all continue_forever }
end

#statusObject


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/overlayer.rb', line 172

def status
  time = cur_english_time
  begin
    mute, blank, next_sig = get_current_state
    if next_sig == :done
      state = " no more after this point..."
    else
      state = " next will be at #{EdlParser.translate_time_to_human_readable next_sig}s "
    end
    if blank? or muted?
      state += "(" + [muted? ? "muted" : nil, blank? ? "blanked" : nil ].compact.join(' ') + ") "
    end
  end
  check_reload_yaml
  time + state + "(r [ctrl+c or q to quit]): "
end

#timestamp_changed(to_this_exact_string_might_be_nil, delta) ⇒ Object


151
152
153
154
155
156
157
158
159
160
# File 'lib/overlayer.rb', line 151

def timestamp_changed to_this_exact_string_might_be_nil, delta
  if @just_unblanked
    # ignore it, since it was probably just caused by the screen blipping
    # at worse this will put us 1s behind...hmm.
    @just_unblanked = false
    p 'ignoring timestamp update ' + to_this_exact_string_might_be_nil.to_s if $VERBOSE
  else
    set_seconds EdlParser.translate_string_to_seconds(to_this_exact_string_might_be_nil) + delta if to_this_exact_string_might_be_nil
  end
end

#unblank!Object


56
57
58
59
60
# File 'lib/overlayer.rb', line 56

def unblank!
  @just_unblanked = true
  @am_blanked = false
  Blanker.unblank_full_screen!
end

#unmute!Object


45
46
47
48
49
# File 'lib/overlayer.rb', line 45

def unmute!
  @am_muted = false
  puts 'unmuting!' if $VERBOSE
  Muter.unmute! unless defined?($AM_IN_UNIT_TEST)
end