Class: MTK::Core::Interval

Inherits:
Object
  • Object
show all
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

See Also:

Constant Summary collapse

NAMES =

The preferred names of all pre-defined intervals

See Also:

%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

See Also:

NAMES_BY_VALUE.flatten.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#valueObject (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.

Raises:

  • (ArgumentError)


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

#inspectObject



93
94
95
# File 'lib/mtk/core/interval.rb', line 93

def inspect
  "#{self.class}<#{to_s} semitones>"
end

#to_fObject

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_iObject

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_sObject



89
90
91
# File 'lib/mtk/core/interval.rb', line 89

def to_s
  @value.to_s
end