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.



14
15
16
17
18
19
20
21
# File 'lib/head_music/content/position.rb', line 14

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.



10
11
12
# File 'lib/head_music/content/position.rb', line 10

def bar_number
  @bar_number
end

#compositionObject (readonly)

Returns the value of attribute composition.



10
11
12
# File 'lib/head_music/content/position.rb', line 10

def composition
  @composition
end

#countObject (readonly)

Returns the value of attribute count.



10
11
12
# File 'lib/head_music/content/position.rb', line 10

def count
  @count
end

#tickObject (readonly)

Returns the value of attribute tick.



10
11
12
# File 'lib/head_music/content/position.rb', line 10

def tick
  @tick
end

Instance Method Details

#+(other) ⇒ Object



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

def +(other)
  other = HeadMusic::Content::RhythmicValue.new(other) if [HeadMusic::RhythmicUnit, Symbol, String].include?(other.class)
  self.class.new(composition, bar_number, count, tick + other.ticks)
end

#<=>(other) ⇒ Object



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

def <=>(other)
  other = self.class.new(composition, other) if other.is_a?(String) && other =~ /\D/
  values <=> other.values
end

#codeObject



27
28
29
30
# File 'lib/head_music/content/position.rb', line 27

def code
  tick_string = tick.to_s.rjust(3, "0")
  [bar_number, count, tick_string].join(":")
end

#meterObject



23
24
25
# File 'lib/head_music/content/position.rb', line 23

def meter
  composition.meter_at(bar_number)
end

#start_of_next_barObject



66
67
68
# File 'lib/head_music/content/position.rb', line 66

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

#stateObject



32
33
34
# File 'lib/head_music/content/position.rb', line 32

def state
  [composition.name, code].join(" ")
end

#strengthObject



49
50
51
# File 'lib/head_music/content/position.rb', line 49

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

#strong?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/head_music/content/position.rb', line 53

def strong?
  strength >= 80
end

#valuesObject



36
37
38
# File 'lib/head_music/content/position.rb', line 36

def values
  [bar_number, count, tick]
end

#weak?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/head_music/content/position.rb', line 57

def weak?
  !strong?
end

#within_placement?(placement) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/head_music/content/position.rb', line 40

def within_placement?(placement)
  placement.position <= self && placement.next_position > self
end