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

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
  %w( P1 p1 ),   #   0   #  unison
  %w( m2 min2 ), #   1   #  minor second
  %w( M2 maj2 ), #   2   #  major second
  %w( m3 min3 ), #   3   #  minor third
  %w( M3 maj3 ), #   4   #  major third
  %w( P4 p4 ),   #   5   #  perfect fourth
  %w( TT tt ),   #   6   #  tritone (AKA augmented fourth, diminished fifth)
  %w( P5 p5 ),   #   7   #  perfect fifth
  %w( m6 min6 ), #   8   #  minor sixth
  %w( M6 maj6 ), #   9   #  major sixth
  %w( m7 min7 ), #  10   #  minor seventh
  %w( M7 maj7 ),  #  11   #  major seventh
  %w( P8 p8 )    #  12   #  octave
].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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Interval

Returns a new instance of Interval.



46
47
48
# File 'lib/mtk/core/interval.rb', line 46

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

The number of semitones represented by this interval



44
45
46
# File 'lib/mtk/core/interval.rb', line 44

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



51
52
53
54
# File 'lib/mtk/core/interval.rb', line 51

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)


62
63
64
65
66
# File 'lib/mtk/core/interval.rb', line 62

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



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

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



102
103
104
105
106
107
108
# File 'lib/mtk/core/interval.rb', line 102

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



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

def -interval
  if interval.is_a? MTK::Core::Interval
    MTK::Core::Interval[@value - interval.value]
  else
    MTK::Core::Interval[@value - interval]
  end
end

#-@Object



134
135
136
# File 'lib/mtk/core/interval.rb', line 134

def -@
  MTK::Core::Interval[@value * -1]
end

#/(interval) ⇒ Object



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

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



94
95
96
97
98
99
100
# File 'lib/mtk/core/interval.rb', line 94

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

#==(other) ⇒ Object



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

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

#coerce(other) ⇒ Object



138
139
140
# File 'lib/mtk/core/interval.rb', line 138

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

#inspectObject



86
87
88
# File 'lib/mtk/core/interval.rb', line 86

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

#to_fObject

The number of semitones as a floating point number



73
74
75
# File 'lib/mtk/core/interval.rb', line 73

def to_f
  @value.to_f
end

#to_iObject

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



78
79
80
# File 'lib/mtk/core/interval.rb', line 78

def to_i
  @value.round
end

#to_sObject



82
83
84
# File 'lib/mtk/core/interval.rb', line 82

def to_s
  @value.to_s
end