Class: HeadMusic::Content::Position
- Inherits:
-
Object
- Object
- HeadMusic::Content::Position
- Includes:
- Comparable
- Defined in:
- lib/head_music/content/position.rb
Overview
A position is a moment in time within the rhythmic framework of a composition.
Instance Attribute Summary collapse
-
#bar_number ⇒ Object
readonly
Returns the value of attribute bar_number.
-
#composition ⇒ Object
readonly
Returns the value of attribute composition.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#tick ⇒ Object
readonly
Returns the value of attribute tick.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #<=>(other) ⇒ Object
- #code ⇒ Object
- #ensure_state(composition, bar_number, count, tick = nil) ⇒ Object private
-
#initialize(composition, code_or_bar, count = nil, tick = nil) ⇒ Position
constructor
A new instance of Position.
- #meter ⇒ Object
- #roll_over_counts ⇒ Object private
- #roll_over_ticks ⇒ Object private
- #roll_over_units ⇒ Object private
- #start_of_next_bar ⇒ Object
- #strength ⇒ Object
- #strong? ⇒ Boolean
- #values ⇒ Object
- #weak? ⇒ Boolean
- #within_placement?(placement) ⇒ Boolean
Constructor Details
#initialize(composition, code_or_bar, count = nil, tick = nil) ⇒ Position
Returns a new instance of Position.
12 13 14 15 16 17 18 19 |
# File 'lib/head_music/content/position.rb', line 12 def initialize(composition, , count = nil, tick = nil) if .is_a?(String) && =~ /\D/ , count, tick = .split(/\D+/) ensure_state(composition, , count, tick) else ensure_state(composition, , count, tick) end end |
Instance Attribute Details
#bar_number ⇒ Object (readonly)
Returns the value of attribute bar_number.
8 9 10 |
# File 'lib/head_music/content/position.rb', line 8 def @bar_number end |
#composition ⇒ Object (readonly)
Returns the value of attribute composition.
8 9 10 |
# File 'lib/head_music/content/position.rb', line 8 def composition @composition end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
8 9 10 |
# File 'lib/head_music/content/position.rb', line 8 def count @count end |
#tick ⇒ Object (readonly)
Returns the value of attribute tick.
8 9 10 |
# File 'lib/head_music/content/position.rb', line 8 def tick @tick end |
Instance Method Details
#+(other) ⇒ Object
55 56 57 58 |
# File 'lib/head_music/content/position.rb', line 55 def +(other) other = HeadMusic::Rudiment::RhythmicValue.new(other) if [HeadMusic::Rudiment::RhythmicUnit, Symbol, String].include?(other.class) self.class.new(composition, , count, tick + other.ticks) end |
#<=>(other) ⇒ Object
38 39 40 41 |
# File 'lib/head_music/content/position.rb', line 38 def <=>(other) other = self.class.new(composition, other) if other.is_a?(String) && other =~ /\D/ values <=> other.values end |
#code ⇒ Object
25 26 27 28 |
# File 'lib/head_music/content/position.rb', line 25 def code tick_string = tick.to_s.rjust(3, "0") [, count, tick_string].join(":") end |
#ensure_state(composition, bar_number, count, tick = nil) ⇒ Object (private)
66 67 68 69 70 71 72 |
# File 'lib/head_music/content/position.rb', line 66 def ensure_state(composition, , count, tick = nil) @composition = composition @bar_number = .to_i @count = (count || 1).to_i @tick = (tick || 0).to_i roll_over_units end |
#meter ⇒ Object
21 22 23 |
# File 'lib/head_music/content/position.rb', line 21 def meter composition.meter_at() end |
#roll_over_counts ⇒ Object (private)
86 87 88 89 90 91 |
# File 'lib/head_music/content/position.rb', line 86 def roll_over_counts while @count > meter. @count -= meter. @bar_number += 1 end end |
#roll_over_ticks ⇒ Object (private)
79 80 81 82 83 84 |
# File 'lib/head_music/content/position.rb', line 79 def roll_over_ticks while @tick >= meter.ticks_per_count @tick -= meter.ticks_per_count.to_i @count += 1 end end |
#roll_over_units ⇒ Object (private)
74 75 76 77 |
# File 'lib/head_music/content/position.rb', line 74 def roll_over_units roll_over_ticks roll_over_counts end |
#start_of_next_bar ⇒ Object
60 61 62 |
# File 'lib/head_music/content/position.rb', line 60 def self.class.new(composition, + 1, 1, 0) end |
#strength ⇒ Object
43 44 45 |
# File 'lib/head_music/content/position.rb', line 43 def strength meter.beat_strength(count, tick: tick) end |
#strong? ⇒ Boolean
47 48 49 |
# File 'lib/head_music/content/position.rb', line 47 def strong? strength >= 80 end |
#values ⇒ Object
30 31 32 |
# File 'lib/head_music/content/position.rb', line 30 def values [, count, tick] end |
#weak? ⇒ Boolean
51 52 53 |
# File 'lib/head_music/content/position.rb', line 51 def weak? !strong? end |
#within_placement?(placement) ⇒ Boolean
34 35 36 |
# File 'lib/head_music/content/position.rb', line 34 def within_placement?(placement) placement.position <= self && placement.next_position > self end |