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



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

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



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

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

#meterObject



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

def meter
  composition.meter_at(bar_number)
end

#start_of_next_barObject



64
65
66
# File 'lib/head_music/content/position.rb', line 64

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

#stateObject



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

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

#strengthObject



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

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

#strong?Boolean

Returns:

  • (Boolean)


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

def strong?
  strength >= 80
end

#valuesObject



34
35
36
# File 'lib/head_music/content/position.rb', line 34

def values
  [bar_number, count, tick]
end

#weak?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/head_music/content/position.rb', line 55

def weak?
  !strong?
end

#within_placement?(placement) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/head_music/content/position.rb', line 38

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