lib/head_music/meter.rb in head_music-0.27.0 vs lib/head_music/meter.rb in head_music-0.28.0

- old
+ new

@@ -3,24 +3,24 @@ # Meter is the rhythmic size of a measure, such as 4/4 or 6/8 class HeadMusic::Meter attr_reader :top_number, :bottom_number NAMED = { - common_time: '4/4', - cut_time: '2/2', + common_time: "4/4", + cut_time: "2/2" }.freeze def self.get(identifier) identifier = identifier.to_s hash_key = HeadMusic::Utilities::HashKey.for(identifier) time_signature_string = NAMED[hash_key] || identifier @meters ||= {} - @meters[hash_key] ||= new(*time_signature_string.split('/').map(&:to_i)) + @meters[hash_key] ||= new(*time_signature_string.split("/").map(&:to_i)) end def self.default - get('4/4') + get("4/4") end def self.common_time get(:common_time) end @@ -37,11 +37,11 @@ def simple? !compound? end def compound? - top_number > 3 && top_number / 3 == top_number / 3.0 + top_number > 3 && (top_number % 3).zero? end def duple? top_number == 2 end @@ -87,22 +87,20 @@ HeadMusic::RhythmicValue.new(count_unit) end end def to_s - [top_number, bottom_number].join('/') + [top_number, bottom_number].join("/") end def ==(other) to_s == other.to_s end def strong_counts - @strong_counts ||= begin - (1..counts_per_bar).select do |count| - downbeat?(count) || strong_beat_in_duple?(count) || strong_beat_in_triple?(count) - end + @strong_counts ||= (1..counts_per_bar).select do |count| + downbeat?(count) || strong_beat_in_duple?(count) || strong_beat_in_triple?(count) end end def strong_ticks @strong_ticks ||= [2, 3, 4].map { |sixths| ticks_per_count * (sixths / 6.0) } @@ -117,14 +115,21 @@ def strong_beat?(count, tick = 0) beat?(tick) && (strong_beat_in_duple?(count, tick) || strong_beat_in_triple?(count, tick)) end def strong_beat_in_duple?(count, tick = 0) - beat?(tick) && (count == counts_per_bar / 2.0 + 1) + return false unless beat?(tick) + return false unless counts_per_bar.even? + + (count - 1) == counts_per_bar / 2 end def strong_beat_in_triple?(count, tick = 0) - beat?(tick) && (counts_per_bar % 3).zero? && counts_per_bar > 6 && count % 3 == 1 + return false unless beat?(tick) + return false unless (counts_per_bar % 3).zero? + return false if counts_per_bar < 6 + + count % 3 == 1 end def beat?(tick) tick.zero? end