Class: HeadMusic::Meter
- Inherits:
-
Object
- Object
- HeadMusic::Meter
- Defined in:
- lib/head_music/meter.rb
Overview
Meter is the rhythmic size of a measure, such as 4/4 or 6/8
Constant Summary collapse
- NAMED =
{ common_time: "4/4", cut_time: "2/2" }.freeze
Instance Attribute Summary collapse
-
#bottom_number ⇒ Object
readonly
Returns the value of attribute bottom_number.
-
#top_number ⇒ Object
readonly
Returns the value of attribute top_number.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #beat_strength(count, tick: 0) ⇒ Object
- #beat_unit ⇒ Object
- #beats_per_bar ⇒ Object
- #compound? ⇒ Boolean
- #count_unit ⇒ Object
- #counts_per_bar ⇒ Object
- #duple? ⇒ Boolean
-
#initialize(top_number, bottom_number) ⇒ Meter
constructor
A new instance of Meter.
- #quadruple? ⇒ Boolean
- #simple? ⇒ Boolean
- #strong_counts ⇒ Object
- #strong_ticks ⇒ Object
- #ticks_per_count ⇒ Object
- #to_s ⇒ Object
- #triple? ⇒ Boolean
Constructor Details
#initialize(top_number, bottom_number) ⇒ Meter
Returns a new instance of Meter.
32 33 34 35 |
# File 'lib/head_music/meter.rb', line 32 def initialize(top_number, bottom_number) @top_number = top_number @bottom_number = bottom_number end |
Instance Attribute Details
#bottom_number ⇒ Object (readonly)
Returns the value of attribute bottom_number.
5 6 7 |
# File 'lib/head_music/meter.rb', line 5 def bottom_number @bottom_number end |
#top_number ⇒ Object (readonly)
Returns the value of attribute top_number.
5 6 7 |
# File 'lib/head_music/meter.rb', line 5 def top_number @top_number end |
Class Method Details
.common_time ⇒ Object
24 25 26 |
# File 'lib/head_music/meter.rb', line 24 def self.common_time get(:common_time) end |
.cut_time ⇒ Object
28 29 30 |
# File 'lib/head_music/meter.rb', line 28 def self.cut_time get(:cut_time) end |
.default ⇒ Object
20 21 22 |
# File 'lib/head_music/meter.rb', line 20 def self.default get("4/4") end |
.get(identifier) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/head_music/meter.rb', line 12 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)) end |
Instance Method Details
#==(other) ⇒ Object
95 96 97 |
# File 'lib/head_music/meter.rb', line 95 def ==(other) to_s == other.to_s end |
#beat_strength(count, tick: 0) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/head_music/meter.rb', line 65 def beat_strength(count, tick: 0) return 100 if downbeat?(count, tick) return 80 if strong_beat?(count, tick) return 60 if beat?(tick) return 40 if strong_ticks.include?(tick) 20 end |
#beat_unit ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/head_music/meter.rb', line 82 def beat_unit @beat_unit ||= if compound? HeadMusic::RhythmicValue.new(HeadMusic::RhythmicUnit.for_denominator_value(bottom_number / 2), dots: 1) else HeadMusic::RhythmicValue.new(count_unit) end end |
#beats_per_bar ⇒ Object
57 58 59 |
# File 'lib/head_music/meter.rb', line 57 def compound? ? top_number / 3 : top_number end |
#compound? ⇒ Boolean
41 42 43 |
# File 'lib/head_music/meter.rb', line 41 def compound? top_number > 3 && (top_number % 3).zero? end |
#count_unit ⇒ Object
78 79 80 |
# File 'lib/head_music/meter.rb', line 78 def count_unit HeadMusic::RhythmicUnit.for_denominator_value(bottom_number) end |
#counts_per_bar ⇒ Object
61 62 63 |
# File 'lib/head_music/meter.rb', line 61 def top_number end |
#duple? ⇒ Boolean
45 46 47 |
# File 'lib/head_music/meter.rb', line 45 def duple? top_number == 2 end |
#quadruple? ⇒ Boolean
53 54 55 |
# File 'lib/head_music/meter.rb', line 53 def quadruple? top_number == 4 end |
#simple? ⇒ Boolean
37 38 39 |
# File 'lib/head_music/meter.rb', line 37 def simple? !compound? end |
#strong_counts ⇒ Object
99 100 101 102 103 |
# File 'lib/head_music/meter.rb', line 99 def strong_counts @strong_counts ||= (1..).select do |count| downbeat?(count) || strong_beat_in_duple?(count) || strong_beat_in_triple?(count) end end |
#strong_ticks ⇒ Object
105 106 107 |
# File 'lib/head_music/meter.rb', line 105 def strong_ticks @strong_ticks ||= [2, 3, 4].map { |sixths| ticks_per_count * (sixths / 6.0) } end |
#ticks_per_count ⇒ Object
74 75 76 |
# File 'lib/head_music/meter.rb', line 74 def ticks_per_count @ticks_per_count ||= count_unit.ticks end |
#to_s ⇒ Object
91 92 93 |
# File 'lib/head_music/meter.rb', line 91 def to_s [top_number, bottom_number].join("/") end |
#triple? ⇒ Boolean
49 50 51 |
# File 'lib/head_music/meter.rb', line 49 def triple? (top_number % 3).zero? end |