Sha256: 86f53291b13fc47458ab57394916a291a9463df1acfa718dde07478c31e6661d

Contents?: true

Size: 1.66 KB

Versions: 10

Compression:

Stored size: 1.66 KB

Contents

class HeadMusic::RhythmicUnit
  MULTIPLES = ['whole', 'double whole', 'longa', 'maxima']
  FRACTIONS = ['whole', 'half', 'quarter', 'eighth', 'sixteenth', 'thirty-second', 'sixty-fourth', 'hundred twenty-eighth', 'two hundred fifty-sixth']

  BRITISH_MULTIPLE_NAMES = %w[semibreve breve longa maxima]
  BRITISH_DIVISION_NAMES = %w[semibreve minim crotchet quaver semiquaver demisemiquaver hemidemisemiquaver semihemidemisemiquaver demisemihemidemisemiquaver]

  def self.get(name)
    @rhythmic_units ||= {}
    hash_key = HeadMusic::Utilities::HashKey.for(name)
    @rhythmic_units[hash_key] ||= new(name.to_s)
  end

  def self.for_denominator_value(denominator)
    get(FRACTIONS[Math.log2(denominator).to_i])
  end

  attr_reader :name, :numerator, :denominator
  delegate :to_s, to: :name

  def initialize(canonical_name)
    @name ||= canonical_name
    @numerator ||= MULTIPLES.include?(name) ? 2**MULTIPLES.index(name) : 1
    @denominator ||= FRACTIONS.include?(name) ? 2**FRACTIONS.index(name) : 1
  end

  def relative_value
    @numerator.to_f / @denominator
  end

  def ticks
    HeadMusic::Rhythm::PPQN * 4 * relative_value
  end

  def notehead
    case relative_value
    when 8
      :maxima
    when 4
      :longa
    when 2
      :breve
    when 0.5, 1
      :open
    else
      :closed
    end
  end

  def flags
    FRACTIONS.include?(name) ? [FRACTIONS.index(name) - 2, 0].max : 0
  end

  def has_stem?
    relative_value < 1
  end

  def british_name
    if MULTIPLES.include?(name)
      BRITISH_MULTIPLE_NAMES[MULTIPLES.index(name)]
    elsif FRACTIONS.include?(name)
      BRITISH_DIVISION_NAMES[FRACTIONS.index(name)]
    end
  end

  private_class_method :new
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
head_music-0.11.9 lib/head_music/rhythmic_unit.rb
head_music-0.11.8 lib/head_music/rhythmic_unit.rb
head_music-0.11.7 lib/head_music/rhythmic_unit.rb
head_music-0.11.6 lib/head_music/rhythmic_unit.rb
head_music-0.11.5 lib/head_music/rhythmic_unit.rb
head_music-0.11.4 lib/head_music/rhythmic_unit.rb
head_music-0.11.3 lib/head_music/rhythmic_unit.rb
head_music-0.11.2 lib/head_music/rhythmic_unit.rb
head_music-0.11.1 lib/head_music/rhythmic_unit.rb
head_music-0.10.0 lib/head_music/rhythmic_unit.rb