Class: HeadMusic::Rudiment::RhythmicUnit

Inherits:
Object
  • Object
show all
Includes:
Named
Defined in:
lib/head_music/rudiment/rhythmic_unit.rb

Overview

A rhythmic unit is a rudiment of duration consisting of doublings and divisions of a whole note.

Constant Summary collapse

MULTIPLES =
["whole", "double whole", "longa", "maxima"].freeze
FRACTIONS =
[
  "whole", "half", "quarter", "eighth", "sixteenth", "thirty-second",
  "sixty-fourth", "hundred twenty-eighth", "two hundred fifty-sixth"
].freeze
BRITISH_MULTIPLE_NAMES =
%w[semibreve breve longa maxima].freeze
BRITISH_DIVISION_NAMES =
%w[
  semibreve minim crotchet quaver semiquaver demisemiquaver
  hemidemisemiquaver semihemidemisemiquaver demisemihemidemisemiquaver
].freeze

Instance Attribute Summary collapse

Attributes included from Named

#alias_name_keys, #name_key

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Named

#ensure_localized_name, included, #localized_name, #localized_names, #name, #name=

Constructor Details

#initialize(canonical_name) ⇒ RhythmicUnit

Returns a new instance of RhythmicUnit.



30
31
32
33
34
# File 'lib/head_music/rudiment/rhythmic_unit.rb', line 30

def initialize(canonical_name)
  self.name = canonical_name
  @numerator = 2**numerator_exponent
  @denominator = 2**denominator_exponent
end

Instance Attribute Details

#denominatorObject (readonly)

Returns the value of attribute denominator.



24
25
26
# File 'lib/head_music/rudiment/rhythmic_unit.rb', line 24

def denominator
  @denominator
end

#numeratorObject (readonly)

Returns the value of attribute numerator.



24
25
26
# File 'lib/head_music/rudiment/rhythmic_unit.rb', line 24

def numerator
  @numerator
end

Class Method Details

.for_denominator_value(denominator) ⇒ Object



20
21
22
# File 'lib/head_music/rudiment/rhythmic_unit.rb', line 20

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

.get(name) ⇒ Object



26
27
28
# File 'lib/head_music/rudiment/rhythmic_unit.rb', line 26

def self.get(name)
  get_by_name(name)
end

Instance Method Details

#british_nameObject



61
62
63
64
65
66
67
68
69
# File 'lib/head_music/rudiment/rhythmic_unit.rb', line 61

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

#flagsObject



53
54
55
# File 'lib/head_music/rudiment/rhythmic_unit.rb', line 53

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

#noteheadObject



44
45
46
47
48
49
50
51
# File 'lib/head_music/rudiment/rhythmic_unit.rb', line 44

def notehead
  return :maxima if relative_value == 8
  return :longa if relative_value == 4
  return :breve if relative_value == 2
  return :open if [0.5, 1].include? relative_value

  :closed
end

#relative_valueObject



36
37
38
# File 'lib/head_music/rudiment/rhythmic_unit.rb', line 36

def relative_value
  @numerator.to_f / @denominator
end

#stemmed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/head_music/rudiment/rhythmic_unit.rb', line 57

def stemmed?
  relative_value < 1
end

#ticksObject



40
41
42
# File 'lib/head_music/rudiment/rhythmic_unit.rb', line 40

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