class HeadMusic::Position

A position is a moment in time within the rhythmic framework of a composition.

Attributes

bar_number[R]
composition[R]
count[R]
tick[R]

Public Class Methods

new(composition, code_or_bar, count = nil, tick = nil) click to toggle source
# File lib/head_music/content/position.rb, line 11
def initialize(composition, code_or_bar, count = nil, tick = nil)
  if code_or_bar.is_a?(String) && code_or_bar =~ /\D/
    bar_number, count, tick = code_or_bar.split(/\D+/)
    ensure_state(composition, bar_number, count, tick)
  else
    ensure_state(composition, code_or_bar, count, tick)
  end
end

Public Instance Methods

+(other) click to toggle source
# File lib/head_music/content/position.rb, line 58
def +(other)
  other = HeadMusic::RhythmicValue.new(other) if [HeadMusic::RhythmicUnit, Symbol, String].include?(other.class)
  self.class.new(composition, bar_number, count, tick + other.ticks)
end
<=>(other) click to toggle source
# File lib/head_music/content/position.rb, line 41
def <=>(other)
  other = self.class.new(composition, other) if other.is_a?(String) && other =~ /\D/
  values <=> other.values
end
code() click to toggle source
# File lib/head_music/content/position.rb, line 24
def code
  tick_string = tick.to_s.rjust(3, '0')
  [bar_number, count, tick_string].join(':')
end
meter() click to toggle source
# File lib/head_music/content/position.rb, line 20
def meter
  composition.meter_at(bar_number)
end
start_of_next_bar() click to toggle source
# File lib/head_music/content/position.rb, line 63
def start_of_next_bar
  self.class.new(composition, bar_number + 1, 1, 0)
end
state() click to toggle source
# File lib/head_music/content/position.rb, line 29
def state
  [composition.name, code].join(' ')
end
strength() click to toggle source
# File lib/head_music/content/position.rb, line 46
def strength
  meter.beat_strength(count, tick: tick)
end
strong?() click to toggle source
# File lib/head_music/content/position.rb, line 50
def strong?
  strength >= 80
end
values() click to toggle source
# File lib/head_music/content/position.rb, line 33
def values
  [bar_number, count, tick]
end
weak?() click to toggle source
# File lib/head_music/content/position.rb, line 54
def weak?
  !strong?
end
within_placement?(placement) click to toggle source
# File lib/head_music/content/position.rb, line 37
def within_placement?(placement)
  placement.position <= self && placement.next_position > self
end

Private Instance Methods

ensure_state(composition, bar_number, count, tick = nil) click to toggle source
# File lib/head_music/content/position.rb, line 69
def ensure_state(composition, bar_number, count, tick = nil)
  @composition = composition
  @bar_number = bar_number.to_i
  @count = (count || 1).to_i
  @tick = (tick || 0).to_i
  roll_over_units
end
roll_over_counts() click to toggle source
# File lib/head_music/content/position.rb, line 89
def roll_over_counts
  while @count > meter.counts_per_bar
    @count -= meter.counts_per_bar
    @bar_number += 1
  end
end
roll_over_ticks() click to toggle source
# File lib/head_music/content/position.rb, line 82
def roll_over_ticks
  while @tick >= meter.ticks_per_count
    @tick -= meter.ticks_per_count.to_i
    @count += 1
  end
end
roll_over_units() click to toggle source
# File lib/head_music/content/position.rb, line 77
def roll_over_units
  roll_over_ticks
  roll_over_counts
end