Class: Stave::Theory::Interval

Inherits:
Core::Lookup show all
Defined in:
lib/stave/theory/interval.rb

Defined Under Namespace

Classes: Number, Quality

Instance Attribute Summary

Attributes inherited from Core::Lookup

#variant

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Lookup

#==, each_key, find_by, #initialize, keys, string_keys, variant, variant?, variant_lookup, variants, where, with_options

Constructor Details

This class inherits a constructor from Stave::Core::Lookup

Class Method Details

.between(note, other_note) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/stave/theory/interval.rb', line 176

def self.between(note, other_note)
  number = Number.between(note, other_note)
  to_i = other_note.to_i - note.to_i
  to_i += 12 if to_i.negative?

  Interval.find_by(number:, to_i:)
end

Instance Method Details

#+(other) ⇒ Object



161
162
163
164
165
166
# File 'lib/stave/theory/interval.rb', line 161

def +(other)
  target_number = number + other.number
  target_i = (to_i + other.to_i) % 12

  Interval.find_by(number: target_number, to_i: target_i)
end

#-(other) ⇒ Object



168
169
170
# File 'lib/stave/theory/interval.rb', line 168

def -(other)
  self + other.invert!
end

#invert!Object



172
173
174
# File 'lib/stave/theory/interval.rb', line 172

def invert!
  Interval.find_by(number: number.invert!, quality: quality.invert!)
end

#symbolObject



157
158
159
# File 'lib/stave/theory/interval.rb', line 157

def symbol
  "#{quality.symbol}#{number.symbol}"
end

#to_iObject



151
152
153
154
155
# File 'lib/stave/theory/interval.rb', line 151

def to_i
  transform_key = number.perfect? ? :perfect : :major

  number.size + quality.transform[transform_key]
end