class MTK::Core::Intensity

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

@see Lang::Intensities

Constants

NAMES

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

VALUES_BY_NAME

Attributes

value[R]

The number of beats, typically representation as a Rational

Public Class Methods

[](value) click to toggle source

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

# File lib/mtk/core/intensity.rb, line 35
def self.[](value)
  value = value.to_f
  @flyweight[value] ||= new(value)
end
Also aliased as: from_f, from_i
from_f(value)
Alias for: []
from_i(value)
Alias for: []
from_name(s)
Alias for: from_s
from_s(s) click to toggle source

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

# 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
Also aliased as: from_name
new(value) click to toggle source
# File lib/mtk/core/intensity.rb, line 30
def initialize(value)
  @value = value
end

Public Instance Methods

*(intensity) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/mtk/core/intensity.rb, line 100
def <=> other
  if other.respond_to? :value
    @value <=> other.value
  else
    @value <=> other
  end

end
==( other ) click to toggle source
# File lib/mtk/core/intensity.rb, line 96
def ==( other )
  other.is_a? MTK::Core::Intensity and other.value == @value
end
coerce(other) click to toggle source
# File lib/mtk/core/intensity.rb, line 141
def coerce(other)
  return MTK::Core::Intensity[other], self
end
inspect() click to toggle source
# File lib/mtk/core/intensity.rb, line 92
def inspect
  "#<#{self.class}:#{object_id} @value=#{@value}>"
end
to_f() click to toggle source

The number of beats as a floating point number

# File lib/mtk/core/intensity.rb, line 71
def to_f
  @value.to_f
end
to_i() click to toggle source

The numerical value for the nearest whole number of beats

# File lib/mtk/core/intensity.rb, line 76
def to_i
  @value.round
end
to_midi() click to toggle source
# File lib/mtk/core/intensity.rb, line 80
def to_midi
  (to_f * 127).round
end
to_percent() click to toggle source
# File lib/mtk/core/intensity.rb, line 84
def to_percent
  (@value * 100).round
end
to_s() click to toggle source
# File lib/mtk/core/intensity.rb, line 88
def to_s
  "#{to_percent}% intensity"
end