Class: HeadMusic::Content::RhythmicValue

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/content/rhythmic_value.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit, dots: nil, tied_value: nil) ⇒ RhythmicValue

Returns a new instance of RhythmicValue.



45
46
47
48
49
# File 'lib/head_music/content/rhythmic_value.rb', line 45

def initialize(unit, dots: nil, tied_value: nil)
  @unit = HeadMusic::Rudiment::RhythmicUnit.get(unit)
  @dots = [0, 1, 2, 3].include?(dots) ? dots : 0
  @tied_value = tied_value
end

Instance Attribute Details

#dotsObject (readonly)

Returns the value of attribute dots.



6
7
8
# File 'lib/head_music/content/rhythmic_value.rb', line 6

def dots
  @dots
end

#tied_valueObject (readonly)

Returns the value of attribute tied_value.



6
7
8
# File 'lib/head_music/content/rhythmic_value.rb', line 6

def tied_value
  @tied_value
end

#unitObject (readonly)

Returns the value of attribute unit.



6
7
8
# File 'lib/head_music/content/rhythmic_value.rb', line 6

def unit
  @unit
end

Class Method Details

.dots_from_words(identifier) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/head_music/content/rhythmic_value.rb', line 31

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) ⇒ Object



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

def self.from_words(identifier)
  new(unit_from_words(identifier), dots: dots_from_words(identifier))
end

.get(identifier) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/head_music/content/rhythmic_value.rb', line 11

def self.get(identifier)
  case identifier
  when HeadMusic::Content::RhythmicValue
    identifier
  when HeadMusic::Rudiment::RhythmicUnit
    new(identifier)
  when Symbol, String
    identifier = identifier.to_s.downcase.strip.gsub(/\W+/, "_")
    from_words(identifier)
  end
end

.unit_from_words(identifier) ⇒ Object



27
28
29
# File 'lib/head_music/content/rhythmic_value.rb', line 27

def self.unit_from_words(identifier)
  identifier.gsub(/^\w*dotted_/, "")
end

Instance Method Details

#==(other) ⇒ Object



98
99
100
# File 'lib/head_music/content/rhythmic_value.rb', line 98

def ==(other)
  to_s == other.to_s
end

#multiplierObject



63
64
65
# File 'lib/head_music/content/rhythmic_value.rb', line 63

def multiplier
  (0..dots).reduce(0) { |sum, i| sum + 0.5**i }
end

#nameObject



90
91
92
93
94
95
96
# File 'lib/head_music/content/rhythmic_value.rb', line 90

def name
  if tied_value
    [single_value_name, tied_value.name].compact.join(" tied to ")
  else
    single_value_name
  end
end

#name_modifier_prefixObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/head_music/content/rhythmic_value.rb', line 75

def name_modifier_prefix
  case dots
  when 1
    "dotted"
  when 2
    "double-dotted"
  when 3
    "triple-dotted"
  end
end

#per_wholeObject



71
72
73
# File 'lib/head_music/content/rhythmic_value.rb', line 71

def per_whole
  1.0 / relative_value
end

#relative_valueObject



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

def relative_value
  unit_value * multiplier
end

#single_value_nameObject



86
87
88
# File 'lib/head_music/content/rhythmic_value.rb', line 86

def single_value_name
  [name_modifier_prefix, unit_name].compact.join(" ")
end

#ticksObject



67
68
69
# File 'lib/head_music/content/rhythmic_value.rb', line 67

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

#to_sObject



102
103
104
# File 'lib/head_music/content/rhythmic_value.rb', line 102

def to_s
  name.tr("_", "-")
end

#total_valueObject



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

def total_value
  relative_value + (tied_value ? tied_value.total_value : 0)
end

#unit_valueObject



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

def unit_value
  unit.relative_value
end