Class: HeadMusic::Content::Position

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, 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

Instance Attribute Details

#bar_numberObject (readonly)

Returns the value of attribute bar_number.



8
9
10
# File 'lib/head_music/content/position.rb', line 8

def bar_number
  @bar_number
end

#compositionObject (readonly)

Returns the value of attribute composition.



8
9
10
# File 'lib/head_music/content/position.rb', line 8

def composition
  @composition
end

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/head_music/content/position.rb', line 8

def count
  @count
end

#tickObject (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, bar_number, 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

#codeObject



25
26
27
28
# File 'lib/head_music/content/position.rb', line 25

def code
  tick_string = tick.to_s.rjust(3, "0")
  [bar_number, 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, 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

#meterObject



21
22
23
# File 'lib/head_music/content/position.rb', line 21

def meter
  composition.meter_at(bar_number)
end

#roll_over_countsObject (private)



86
87
88
89
90
91
# File 'lib/head_music/content/position.rb', line 86

def roll_over_counts
  while @count > meter.counts_per_bar
    @count -= meter.counts_per_bar
    @bar_number += 1
  end
end

#roll_over_ticksObject (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_unitsObject (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_barObject



60
61
62
# File 'lib/head_music/content/position.rb', line 60

def start_of_next_bar
  self.class.new(composition, bar_number + 1, 1, 0)
end

#strengthObject



43
44
45
# File 'lib/head_music/content/position.rb', line 43

def strength
  meter.beat_strength(count, tick: tick)
end

#strong?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/head_music/content/position.rb', line 47

def strong?
  strength >= 80
end

#valuesObject



30
31
32
# File 'lib/head_music/content/position.rb', line 30

def values
  [bar_number, count, tick]
end

#weak?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/head_music/content/position.rb', line 51

def weak?
  !strong?
end

#within_placement?(placement) ⇒ Boolean

Returns:

  • (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