Class: GlimmerMetronome::Model::Rhythm

Inherits:
Object
  • Object
show all
Defined in:
app/glimmer_metronome/model/rhythm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(beat_count) ⇒ Rhythm

Returns a new instance of Rhythm.



32
33
34
35
# File 'app/glimmer_metronome/model/rhythm.rb', line 32

def initialize(beat_count)
  self.beat_count = beat_count
  @tempo = 120
end

Instance Attribute Details

#beat_countObject

Returns the value of attribute beat_count.



29
30
31
# File 'app/glimmer_metronome/model/rhythm.rb', line 29

def beat_count
  @beat_count
end

#beatsObject

Returns the value of attribute beats.



30
31
32
# File 'app/glimmer_metronome/model/rhythm.rb', line 30

def beats
  @beats
end

#tempoObject

Returns the value of attribute tempo.



30
31
32
# File 'app/glimmer_metronome/model/rhythm.rb', line 30

def tempo
  @tempo
end

Instance Method Details

#off!Object



47
48
49
# File 'app/glimmer_metronome/model/rhythm.rb', line 47

def off!
  beats.select(&:on).each(&:off!)
end

#on_beat!(beat_index) ⇒ Object



42
43
44
45
# File 'app/glimmer_metronome/model/rhythm.rb', line 42

def on_beat!(beat_index)
  off!
  beats[beat_index].on!
end

#reset_beats!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/glimmer_metronome/model/rhythm.rb', line 51

def reset_beats!
  if @beats
    off!
    if beat_count > @beats.count
      @beats += (beat_count - @beats.count).times.map {Beat.new(false)}
    elsif beat_count < @beats.count
      @beats = @beats[0, @beats.count]
    end
  else
    @beats = beat_count.times.map {|i| Beat.new(i == 0)}
  end
  on_beat!(0)
end

#tap!Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/glimmer_metronome/model/rhythm.rb', line 65

def tap!
  new_tap_time = Time.now
  @tap_time ||= []
  time_difference = nil
  if @tap_time.any?
    if @tap_time[-2]
      time_difference1 = new_tap_time - @tap_time[-1]
      time_difference2 = @tap_time[-1] - @tap_time[-2]
      time_difference = BigDecimal((time_difference1 + time_difference2).to_s) / 2.0
    else
      time_difference = BigDecimal((new_tap_time - @tap_time[-1]).to_s)
    end
  end
  if time_difference
    if time_difference < 2
      self.tempo = (BigDecimal('60.0') / time_difference)
    else
      @tap_time = []
    end
  end
  @tap_time << Time.now
end