app/glimmer_metronome/model/rhythm.rb in glimmer_metronome-1.0.0 vs app/glimmer_metronome/model/rhythm.rb in glimmer_metronome-1.1.0

- old
+ new

@@ -17,30 +17,73 @@ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +require 'bigdecimal' + require_relative 'beat' class GlimmerMetronome module Model class Rhythm attr_reader :beat_count - attr_accessor :beats, :bpm + attr_accessor :beats, :tempo def initialize(beat_count) self.beat_count = beat_count - @bpm = 120 + @tempo = 120 end def beat_count=(value) @beat_count = value reset_beats! end + def on_beat!(beat_index) + off! + beats[beat_index].on! + end + + def off! + beats.select(&:on).each(&:off!) + end + def reset_beats! - @beats = beat_count.times.map {Beat.new} - @beats.first.on! + 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 + + 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 end end end