Class: MTK::Core::Interval
- Inherits:
-
Object
- Object
- MTK::Core::Interval
- Includes:
- Comparable
- Defined in:
- lib/mtk/core/interval.rb
Overview
A measure of intensity, using an underlying value in the range 0.0-1.0
Constant Summary collapse
- NAMES =
The preferred names of all pre-defined intervals
%w[P1 m2 M2 m3 M3 P4 TT P5 m6 M6 m7 M7 P8].freeze
- NAMES_BY_VALUE =
All valid names of pre-defined intervals, indexed by their value.
[ # names # value # description # enharmonic equivalents %w( P1 d2 ), # 0 # unison # diminished second %w( m2 a1 ), # 1 # minor second # augmented unison %w( M2 d3 ), # 2 # major second # diminished third %w( m3 a2 ), # 3 # minor third # augmented second %w( M3 d4 ), # 4 # major third # diminished fourth %w( P4 a3 ), # 5 # perfect fourth # augmented third %w( TT a4 d5 ),# 6 # tritone # augmented fourth, diminished fifth %w( P5 d6 ), # 7 # perfect fifth # diminished sixth %w( m6 a5 ), # 8 # minor sixth # augmented fifth %w( M6 d7 ), # 9 # major sixth # diminished seventh %w( m7 a6 ), # 10 # minor seventh # augmented sixth %w( M7 d8 ), # 11 # major seventh # diminished octave %w( P8 a7 ) # 12 # octave # augmented seventh ].freeze
- VALUES_BY_NAME =
A mapping from intervals names to their value
Hash[ # a map from a list of name,value pairs NAMES_BY_VALUE.map.with_index do |names,value| names.map{|name| [name,value] } end.flatten(1) ].freeze
- ALL_NAMES =
All valid interval names
NAMES_BY_VALUE.flatten.freeze
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
The number of semitones represented by this interval.
Class Method Summary collapse
-
.[](value) ⇒ Object
(also: from_f, from_i)
Return an Interval, only constructing a new instance when not already in the flyweight cache.
-
.from_s(s) ⇒ Object
(also: from_name)
Lookup an interval duration by name.
Instance Method Summary collapse
- #*(interval) ⇒ Object
- #+(interval) ⇒ Object
- #-(interval) ⇒ Object
- #-@ ⇒ Object
- #/(interval) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #coerce(other) ⇒ Object
-
#initialize(value) ⇒ Interval
constructor
A new instance of Interval.
- #inspect ⇒ Object
-
#to_f ⇒ Object
The number of semitones as a floating point number.
-
#to_i ⇒ Object
The numerical value for the nearest whole number of semitones in this interval.
- #to_s ⇒ Object
Constructor Details
#initialize(value) ⇒ Interval
Returns a new instance of Interval.
53 54 55 |
# File 'lib/mtk/core/interval.rb', line 53 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Object (readonly)
The number of semitones represented by this interval
51 52 53 |
# File 'lib/mtk/core/interval.rb', line 51 def value @value end |
Class Method Details
.[](value) ⇒ Object Also known as: from_f, from_i
Return an MTK::Core::Interval, only constructing a new instance when not already in the flyweight cache
58 59 60 61 |
# 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 |
.from_s(s) ⇒ Object Also known as: from_name
Lookup an interval duration by name.
69 70 71 72 73 |
# 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 |
Instance Method Details
#*(interval) ⇒ Object
125 126 127 128 129 130 131 |
# 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) ⇒ Object
109 110 111 112 113 114 115 |
# 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) ⇒ Object
117 118 119 120 121 122 123 |
# 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 |
#-@ ⇒ Object
141 142 143 |
# File 'lib/mtk/core/interval.rb', line 141 def -@ MTK::Core::Interval[@value * -1] end |
#/(interval) ⇒ Object
133 134 135 136 137 138 139 |
# 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) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/mtk/core/interval.rb', line 101 def <=> other if other.respond_to? :value @value <=> other.value else @value <=> other end end |
#==(other) ⇒ Object
97 98 99 |
# File 'lib/mtk/core/interval.rb', line 97 def ==( other ) other.is_a? MTK::Core::Interval and other.value == @value end |
#coerce(other) ⇒ Object
145 146 147 |
# File 'lib/mtk/core/interval.rb', line 145 def coerce(other) return MTK::Core::Interval[other], self end |
#inspect ⇒ Object
93 94 95 |
# File 'lib/mtk/core/interval.rb', line 93 def inspect "#{self.class}<#{to_s} semitones>" end |
#to_f ⇒ Object
The number of semitones as a floating point number
80 81 82 |
# File 'lib/mtk/core/interval.rb', line 80 def to_f @value.to_f end |
#to_i ⇒ Object
The numerical value for the nearest whole number of semitones in this interval
85 86 87 |
# File 'lib/mtk/core/interval.rb', line 85 def to_i @value.round end |
#to_s ⇒ Object
89 90 91 |
# File 'lib/mtk/core/interval.rb', line 89 def to_s @value.to_s end |