Class: MTK::Core::Intensity

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/mtk/core/intensity.rb

Overview

A measure of intensity, using an underlying value in the range 0.0-1.0

Constant Summary collapse

NAMES =

The names of the base intensities. See {}MTK::Lang::Intensities} for more info.

%w[ppp pp p mp mf o ff fff].freeze
VALUES_BY_NAME =
{
  'ppp' => 0.125,
  'pp' => 0.25,
  'p' => 0.375,
  'mp' => 0.5,
  'mf' => 0.625,
  'o' => 0.75,
  'ff' => 0.875,
  'fff' => 1.0
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Intensity

Returns a new instance of Intensity.



28
29
30
# File 'lib/mtk/core/intensity.rb', line 28

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

The number of beats, typically representation as a Rational



26
27
28
# File 'lib/mtk/core/intensity.rb', line 26

def value
  @value
end

Class Method Details

.[](value) ⇒ Object Also known as: from_f, from_i

Return an Intensity, only constructing a new instance when not already in the flyweight cache



33
34
35
36
# File 'lib/mtk/core/intensity.rb', line 33

def self.[](value)
  value = value.to_f
  @flyweight[value] ||= new(value)
end

.from_s(s) ⇒ Object Also known as: from_name

Lookup an intensity by name. This method supports appending ‘-’ or ‘+’ for more fine-grained values.

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mtk/core/intensity.rb', line 45

def self.from_s(s)
  return self[1.0] if s == 'fff+' # special case because "fff" is already the maximum

  name = nil
  modifier = nil
  if s =~ /^(\w+)([+-])?$/
    name = $1
    modifier = $2
  end

  value = VALUES_BY_NAME[name]
  raise ArgumentError.new("Invalid Intensity string '#{s}'") unless value

  value += 1.0/24 if modifier == '+'
  value -= 1.0/24 if modifier == '-'

  self[value]
end

Instance Method Details

#*(intensity) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/mtk/core/intensity.rb', line 123

def * intensity
  if intensity.is_a? MTK::Core::Intensity
    MTK::Core::Intensity[@value * intensity.value]
  else
    MTK::Core::Intensity[@value * intensity]
  end
end

#+(intensity) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/mtk/core/intensity.rb', line 107

def + intensity
  if intensity.is_a? MTK::Core::Intensity
    MTK::Core::Intensity[@value + intensity.value]
  else
    MTK::Core::Intensity[@value + intensity]
  end
end

#-(intensity) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/mtk/core/intensity.rb', line 115

def -intensity
  if intensity.is_a? MTK::Core::Intensity
    MTK::Core::Intensity[@value - intensity.value]
  else
    MTK::Core::Intensity[@value - intensity]
  end
end

#/(intensity) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/mtk/core/intensity.rb', line 131

def / intensity
  if intensity.is_a? MTK::Core::Intensity
    MTK::Core::Intensity[to_f / intensity.value]
  else
    MTK::Core::Intensity[to_f / intensity]
  end
end

#<=>(other) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/mtk/core/intensity.rb', line 98

def <=> other
  if other.respond_to? :value
    @value <=> other.value
  else
    @value <=> other
  end

end

#==(other) ⇒ Object



94
95
96
# File 'lib/mtk/core/intensity.rb', line 94

def ==( other )
  other.is_a? MTK::Core::Intensity and other.value == @value
end

#coerce(other) ⇒ Object



139
140
141
# File 'lib/mtk/core/intensity.rb', line 139

def coerce(other)
  return MTK::Core::Intensity[other], self
end

#inspectObject



90
91
92
# File 'lib/mtk/core/intensity.rb', line 90

def inspect
  "#<#{self.class}:#{object_id} @value=#{@value}>"
end

#to_fObject

The number of beats as a floating point number



69
70
71
# File 'lib/mtk/core/intensity.rb', line 69

def to_f
  @value.to_f
end

#to_iObject

The numerical value for the nearest whole number of beats



74
75
76
# File 'lib/mtk/core/intensity.rb', line 74

def to_i
  @value.round
end

#to_midiObject



78
79
80
# File 'lib/mtk/core/intensity.rb', line 78

def to_midi
  (to_f * 127).round
end

#to_percentObject



82
83
84
# File 'lib/mtk/core/intensity.rb', line 82

def to_percent
  (@value * 100).round
end

#to_sObject



86
87
88
# File 'lib/mtk/core/intensity.rb', line 86

def to_s
  "#{to_percent}% intensity"
end