Class: Zgomot::Midi::Clock

Inherits:
Object show all
Defined in:
lib/zgomot/midi/clock.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClock

Returns a new instance of Clock.



65
66
67
# File 'lib/zgomot/midi/clock.rb', line 65

def initialize
  @current_time, @created_at = Time.new, ::Time.now
end

Class Attribute Details

.beat_noteObject

Returns the value of attribute beat_note.



48
49
50
# File 'lib/zgomot/midi/clock.rb', line 48

def beat_note
  @beat_note
end

.beat_secObject

Returns the value of attribute beat_sec.



48
49
50
# File 'lib/zgomot/midi/clock.rb', line 48

def beat_sec
  @beat_sec
end

.beats_per_measureObject

Returns the value of attribute beats_per_measure.



48
49
50
# File 'lib/zgomot/midi/clock.rb', line 48

def beats_per_measure
  @beats_per_measure
end

.beats_per_minuteObject

Returns the value of attribute beats_per_minute.



48
49
50
# File 'lib/zgomot/midi/clock.rb', line 48

def beats_per_minute
  @beats_per_minute
end

.measure_secObject

Returns the value of attribute measure_sec.



48
49
50
# File 'lib/zgomot/midi/clock.rb', line 48

def measure_sec
  @measure_sec
end

.resolutionObject

Returns the value of attribute resolution.



48
49
50
# File 'lib/zgomot/midi/clock.rb', line 48

def resolution
  @resolution
end

.tick_secObject

Returns the value of attribute tick_sec.



48
49
50
# File 'lib/zgomot/midi/clock.rb', line 48

def tick_sec
  @tick_sec
end

.ticks_per_beatObject

Returns the value of attribute ticks_per_beat.



48
49
50
# File 'lib/zgomot/midi/clock.rb', line 48

def ticks_per_beat
  @ticks_per_beat
end

.time_signatureObject

Returns the value of attribute time_signature.



48
49
50
# File 'lib/zgomot/midi/clock.rb', line 48

def time_signature
  @time_signature
end

.whole_note_secObject

Returns the value of attribute whole_note_sec.



48
49
50
# File 'lib/zgomot/midi/clock.rb', line 48

def whole_note_sec
  @whole_note_sec
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



64
65
66
# File 'lib/zgomot/midi/clock.rb', line 64

def created_at
  @created_at
end

#current_timeObject (readonly)

Returns the value of attribute current_time.



64
65
66
# File 'lib/zgomot/midi/clock.rb', line 64

def current_time
  @current_time
end

Class Method Details

.set_config(config) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/zgomot/midi/clock.rb', line 51

def set_config(config)
  @time_signature = config[:time_signature] || '4/4'
  @beats_per_minute = (config[:beats_per_minute] || '120').to_f
  @resolution = (config[:resolution] || '1/32').split('/').last.to_f
  @beats_per_measure, @beat_note = @time_signature.split('/').map{|v| v.to_f}
  @ticks_per_beat = @resolution/@beats_per_measure
  @beat_sec= 60.0/@beats_per_minute
  @whole_note_sec = @beat_sec*@beat_note
  @measure_sec = @beat_sec*@beats_per_measure
  @tick_sec = @whole_note_sec/(@resolution);nil
end

Instance Method Details

#+(add_clock) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/zgomot/midi/clock.rb', line 71

def +(add_clock)
  @current_time + if add_clock.knd_of(Float) or add_time.kind_of?(Zgomot::Midi::Time)
                    add_clock
                  elsif add_time.kind_of?(Zgomot::Midi::Clock)
                    add_clock.current_time
                  else
                    raise(Zgomot::Error, "#{add_clock.class.name} is invalid. Must be Float, Zgomot::Midi::Time or Zgomot::Midi::Clock")
                  end
end

#absolute_secObject



80
81
82
# File 'lib/zgomot/midi/clock.rb', line 80

def absolute_sec
  @current_time + created_at
end

#ceilObject



83
84
85
86
87
# File 'lib/zgomot/midi/clock.rb', line 83

def ceil
  Time.new(:measure => @current_time.measure + 1,
           :beat    => 0,
           :tick    => 0)
end

#to_sObject



68
69
70
# File 'lib/zgomot/midi/clock.rb', line 68

def to_s
  @current_time.to_s
end

#update(time = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/zgomot/midi/clock.rb', line 88

def update(time=nil)
  csecs = if time.kind_of?(Float)
            current_time.to_f + time
          elsif time.kind_of?(Zgomot::Midi::Time)
            current_time.to_f + time.to_f
          elsif time.nil?
            current_time.to_f + Clock.tick_sec
          else
            raise(Zgomot::Error, "argument must by of type Float or Zgomot::Midi::Time")
          end
  @current_time = Time.new(csecs)
end