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

See Also:

Constant Summary collapse

NAMES =

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

%w[ppp pp p mp mf f ff fff].freeze
VALUES_BY_NAME =
{
  'ppp' => 0.125,
  'pp' => 0.25,
  'p' => 0.375,
  'mp' => 0.5,
  'mf' => 0.625,
  'f' => 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.



30
31
32
# File 'lib/mtk/core/intensity.rb', line 30

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

The number of beats, typically representation as a Rational



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

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



35
36
37
38
# File 'lib/mtk/core/intensity.rb', line 35

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)


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

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



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

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



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

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



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

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



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

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



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

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

end

#==(other) ⇒ Object



96
97
98
# File 'lib/mtk/core/intensity.rb', line 96

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

#coerce(other) ⇒ Object



141
142
143
# File 'lib/mtk/core/intensity.rb', line 141

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

#inspectObject



92
93
94
# File 'lib/mtk/core/intensity.rb', line 92

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

#to_fObject

The number of beats as a floating point number



71
72
73
# File 'lib/mtk/core/intensity.rb', line 71

def to_f
  @value.to_f
end

#to_iObject

The numerical value for the nearest whole number of beats



76
77
78
# File 'lib/mtk/core/intensity.rb', line 76

def to_i
  @value.round
end

#to_midiObject



80
81
82
# File 'lib/mtk/core/intensity.rb', line 80

def to_midi
  (to_f * 127).round
end

#to_percentObject



84
85
86
# File 'lib/mtk/core/intensity.rb', line 84

def to_percent
  (@value * 100).round
end

#to_sObject



88
89
90
# File 'lib/mtk/core/intensity.rb', line 88

def to_s
  "#{to_percent}% intensity"
end