Class: HeadMusic::RhythmicUnit

Inherits:
Object
  • Object
show all
Includes:
Named
Defined in:
lib/head_music/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.



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

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.



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

def denominator
  @denominator
end

#numeratorObject (readonly)

Returns the value of attribute numerator.



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

def numerator
  @numerator
end

Class Method Details

.for_denominator_value(denominator) ⇒ Object



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

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

.get(name) ⇒ Object



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

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

Instance Method Details

#british_nameObject



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

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



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

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

#noteheadObject



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

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



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

def relative_value
  @numerator.to_f / @denominator
end

#stemmed?Boolean

Returns:

  • (Boolean)


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

def stemmed?
  relative_value < 1
end

#ticksObject



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

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