Class: HeadMusic::Content::RhythmicValue
- Inherits:
-
Object
- Object
- HeadMusic::Content::RhythmicValue
- 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
-
#dots ⇒ Object
readonly
Returns the value of attribute dots.
-
#tied_value ⇒ Object
readonly
Returns the value of attribute tied_value.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Class Method Summary collapse
- .dots_from_words(identifier) ⇒ Object
- .from_words(identifier) ⇒ Object
- .get(identifier) ⇒ Object
- .unit_from_words(identifier) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(unit, dots: nil, tied_value: nil) ⇒ RhythmicValue
constructor
A new instance of RhythmicValue.
- #multiplier ⇒ Object
- #name ⇒ Object
- #name_modifier_prefix ⇒ Object
- #per_whole ⇒ Object
- #relative_value ⇒ Object
- #single_value_name ⇒ Object
- #ticks ⇒ Object
- #to_s ⇒ Object
- #total_value ⇒ Object
- #unit_value ⇒ Object
Constructor Details
#initialize(unit, dots: nil, tied_value: nil) ⇒ RhythmicValue
Returns a new instance of RhythmicValue.
47 48 49 50 51 |
# File 'lib/head_music/content/rhythmic_value.rb', line 47 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 |
Instance Attribute Details
#dots ⇒ Object (readonly)
Returns the value of attribute dots.
8 9 10 |
# File 'lib/head_music/content/rhythmic_value.rb', line 8 def dots @dots end |
#tied_value ⇒ Object (readonly)
Returns the value of attribute tied_value.
8 9 10 |
# File 'lib/head_music/content/rhythmic_value.rb', line 8 def tied_value @tied_value end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
8 9 10 |
# File 'lib/head_music/content/rhythmic_value.rb', line 8 def unit @unit end |
Class Method Details
.dots_from_words(identifier) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/head_music/content/rhythmic_value.rb', line 33 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
25 26 27 |
# File 'lib/head_music/content/rhythmic_value.rb', line 25 def self.from_words(identifier) new(unit_from_words(identifier), dots: dots_from_words(identifier)) end |
.get(identifier) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/head_music/content/rhythmic_value.rb', line 13 def self.get(identifier) case identifier when HeadMusic::Content::RhythmicValue identifier when HeadMusic::RhythmicUnit new(identifier) when Symbol, String identifier = identifier.to_s.downcase.strip.gsub(/\W+/, "_") from_words(identifier) end end |
.unit_from_words(identifier) ⇒ Object
29 30 31 |
# File 'lib/head_music/content/rhythmic_value.rb', line 29 def self.unit_from_words(identifier) identifier.gsub(/^\w*dotted_/, "") end |
Instance Method Details
#==(other) ⇒ Object
100 101 102 |
# File 'lib/head_music/content/rhythmic_value.rb', line 100 def ==(other) to_s == other.to_s end |
#multiplier ⇒ Object
65 66 67 |
# File 'lib/head_music/content/rhythmic_value.rb', line 65 def multiplier (0..dots).reduce(0) { |sum, i| sum + 0.5**i } end |
#name ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/head_music/content/rhythmic_value.rb', line 92 def name if tied_value [single_value_name, tied_value.name].compact.join(" tied to ") else single_value_name end end |
#name_modifier_prefix ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/head_music/content/rhythmic_value.rb', line 77 def name_modifier_prefix case dots when 1 "dotted" when 2 "double-dotted" when 3 "triple-dotted" end end |
#per_whole ⇒ Object
73 74 75 |
# File 'lib/head_music/content/rhythmic_value.rb', line 73 def per_whole 1.0 / relative_value end |
#relative_value ⇒ Object
57 58 59 |
# File 'lib/head_music/content/rhythmic_value.rb', line 57 def relative_value unit_value * multiplier end |
#single_value_name ⇒ Object
88 89 90 |
# File 'lib/head_music/content/rhythmic_value.rb', line 88 def single_value_name [name_modifier_prefix, unit_name].compact.join(" ") end |
#ticks ⇒ Object
69 70 71 |
# File 'lib/head_music/content/rhythmic_value.rb', line 69 def ticks HeadMusic::Rhythm::PPQN * 4 * total_value end |
#to_s ⇒ Object
104 105 106 |
# File 'lib/head_music/content/rhythmic_value.rb', line 104 def to_s name.tr("_", "-") end |
#total_value ⇒ Object
61 62 63 |
# File 'lib/head_music/content/rhythmic_value.rb', line 61 def total_value relative_value + (tied_value ? tied_value.total_value : 0) end |
#unit_value ⇒ Object
53 54 55 |
# File 'lib/head_music/content/rhythmic_value.rb', line 53 def unit_value unit.relative_value end |