class MTK::Core::Interval

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

@see Lang::Intervals

Constants

ALL_NAMES

All valid interval names @see NAMES_BY_VALUE

NAMES

The preferred names of all pre-defined intervals @see ALL_NAMES

NAMES_BY_VALUE

All valid names of pre-defined intervals, indexed by their value. @see ALL_NAMES @see NAMES @see en.wikipedia.org/wiki/Interval_(music)#Main_intervals

VALUES_BY_NAME

A mapping from intervals names to their value

Attributes

value[R]

The number of semitones represented by this interval

Public Class Methods

[](value) click to toggle source

Return an {Interval}, only constructing a new instance when not already in the flyweight cache

# File lib/mtk/core/interval.rb, line 58
def self.[](value)
  value = value.to_f unless value.is_a? Fixnum
  @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 interval duration by name.

# File lib/mtk/core/interval.rb, line 69
def self.from_s(s)
  value = VALUES_BY_NAME[s.to_s]
  raise ArgumentError.new("Invalid Interval string '#{s}'") unless value
  self[value]
end
Also aliased as: from_name
new(value) click to toggle source
# File lib/mtk/core/interval.rb, line 53
def initialize(value)
  @value = value
end

Public Instance Methods

*(interval) click to toggle source
# File lib/mtk/core/interval.rb, line 125
def * interval
  if interval.is_a? MTK::Core::Interval
    MTK::Core::Interval[@value * interval.value]
  else
    MTK::Core::Interval[@value * interval]
  end
end
+(interval) click to toggle source
# File lib/mtk/core/interval.rb, line 109
def + interval
  if interval.is_a? MTK::Core::Interval
    MTK::Core::Interval[@value + interval.value]
  else
    MTK::Core::Interval[@value + interval]
  end
end
-(interval) click to toggle source
# File lib/mtk/core/interval.rb, line 117
def -interval
  if interval.is_a? MTK::Core::Interval
    MTK::Core::Interval[@value - interval.value]
  else
    MTK::Core::Interval[@value - interval]
  end
end
-@() click to toggle source
# File lib/mtk/core/interval.rb, line 141
def -@
  MTK::Core::Interval[@value * -1]
end
/(interval) click to toggle source
# File lib/mtk/core/interval.rb, line 133
def / interval
  if interval.is_a? MTK::Core::Interval
    MTK::Core::Interval[to_f / interval.value]
  else
    MTK::Core::Interval[to_f / interval]
  end
end
<=>(other) click to toggle source
# File lib/mtk/core/interval.rb, line 101
def <=> other
  if other.respond_to? :value
    @value <=> other.value
  else
    @value <=> other
  end
end
==( other ) click to toggle source
# File lib/mtk/core/interval.rb, line 97
def ==( other )
  other.is_a? MTK::Core::Interval and other.value == @value
end
coerce(other) click to toggle source
# File lib/mtk/core/interval.rb, line 145
def coerce(other)
  return MTK::Core::Interval[other], self
end
inspect() click to toggle source
# File lib/mtk/core/interval.rb, line 93
def inspect
  "#{self.class}<#{to_s} semitones>"
end
to_f() click to toggle source

The number of semitones as a floating point number

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

The numerical value for the nearest whole number of semitones in this interval

# File lib/mtk/core/interval.rb, line 85
def to_i
  @value.round
end
to_s() click to toggle source
# File lib/mtk/core/interval.rb, line 89
def to_s
  @value.to_s
end