class HeadMusic::RhythmicValue

A rhythmic value is a duration composed of a rhythmic unit, any number of dots, and a tied value.

Attributes

dots[R]
tied_value[R]
unit[R]

Public Class Methods

dots_from_words(identifier) click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 30
def self.dots_from_words(identifier)
  return 0 unless identifier.match?(/dotted/)

  modifier, = identifier.split(/_*dotted_*/)
  case modifier
  when /tripl\w/
    3
  when /doubl\w/
    2
  else
    1
  end
end
from_words(identifier) click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 22
def self.from_words(identifier)
  new(unit_from_words(identifier), dots: dots_from_words(identifier))
end
get(identifier) click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 10
def self.get(identifier)
  case identifier
  when HeadMusic::RhythmicValue
    identifier
  when HeadMusic::RhythmicUnit
    new(identifier)
  when Symbol, String
    identifier = identifier.to_s.downcase.strip.gsub(/\W+/, '_')
    from_words(identifier)
  end
end
new(unit, dots: nil, tied_value: nil) click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 44
def initialize(unit, dots: nil, tied_value: nil)
  @unit = HeadMusic::RhythmicUnit.get(unit)
  @dots = [0, 1, 2, 3].include?(dots) ? dots : 0
  @tied_value = tied_value
end
unit_from_words(identifier) click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 26
def self.unit_from_words(identifier)
  identifier.gsub(/^\w*dotted_/, '')
end

Public Instance Methods

==(other) click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 97
def ==(other)
  to_s == other.to_s
end
multiplier() click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 62
def multiplier
  (0..dots).reduce(0) { |sum, i| sum + 0.5**i }
end
name() click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 89
def name
  if tied_value
    [single_value_name, tied_value.name].compact.join(' tied to ')
  else
    single_value_name
  end
end
name_modifier_prefix() click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 74
def name_modifier_prefix
  case dots
  when 1
    'dotted'
  when 2
    'double-dotted'
  when 3
    'triple-dotted'
  end
end
per_whole() click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 70
def per_whole
  1.0 / relative_value
end
relative_value() click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 54
def relative_value
  unit_value * multiplier
end
single_value_name() click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 85
def single_value_name
  [name_modifier_prefix, unit_name].compact.join(' ')
end
ticks() click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 66
def ticks
  HeadMusic::Rhythm::PPQN * 4 * total_value
end
to_s() click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 101
def to_s
  name.tr('_', '-')
end
total_value() click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 58
def total_value
  relative_value + (tied_value ? tied_value.total_value : 0)
end
unit_value() click to toggle source
# File lib/head_music/content/rhythmic_value.rb, line 50
def unit_value
  unit.relative_value
end