class Music::Interval
Constants
- OCTAVE
@private
- QUALITIES
- SIZES
@private
Attributes
Indicates if the interval is positive or negative.
@return [1, -1]
@return [Integer]
@return [Symbol]
Public Class Methods
@param number [Integer] @param quality [Symbol] See the {QUALITIES} constant for the list of
possible values.
@example
Interval.new(3, :major) # a major third Interval.new(5, :perfect) # a perfect fifth
# File lib/music/interval.rb, line 52 def initialize(number, quality) if not QUALITIES.include?(quality) raise ArgumentError, "invalid interval quality: #{quality}" elsif [1, 4, 5].include?((number.abs - 1) % 7 + 1) and [:major, :minor].include?(quality) raise ArgumentError, "interval #{number} doesn't have quality \"#{quality}\"" end @number, @quality = number.abs, quality @direction = number >= 0 ? +1 : -1 end
Public Instance Methods
Returns the same inteval with changed direction.
@return [Music::Interval]
# File lib/music/interval.rb, line 111 def -@ Interval.new((-1 * direction) * number, quality) end
Compares intervals by their size.
@param other [Music::Interval] @example
Interval.new(3, :major) > Interval.new(4, :perfect) #=> true Interval.new(3, :major) == Interval.new(4, :diminished) #=> true
# File lib/music/interval.rb, line 150 def <=>(other) self.size.abs <=> other.size.abs end
Consonances are perfect and imperfect consonances together.
@return [Boolean]
@see perfect_consonance?
@see imperfect_consonance?
# File lib/music/interval.rb, line 91 def consonance? perfect_consonance? or imperfect_consonance? end
The actual diatonic difference. For example, seconds are numbered with ‘2`, but the actual diatonic difference between two notes that are in a second is `1`.
@return [Integer]
# File lib/music/interval.rb, line 161 def diff (number - 1) * direction end
Disonances are all intervals that are not consonances.
@return [Boolean]
@see consonance?
# File lib/music/interval.rb, line 102 def dissonance? not consonance? end
Imperfect consonants are minor/major thirds and sixths (and their compound intervals).
@return [Boolean]
# File lib/music/interval.rb, line 79 def imperfect_consonance? [3, 6].include?(normalized_number) and [:minor, :major].include?(quality) end
Perfect consonants are perfect unisons, fourths and fifths (and their compound intervals).
@return [Boolean]
# File lib/music/interval.rb, line 69 def perfect_consonance? [1, 4, 5].include?(normalized_number) and [:perfect].include?(quality) end
Returns the number of semitones that interval covers.
@return [Integer]
# File lib/music/interval.rb, line 120 def size result = 0 current_number = number while current_number >= OCTAVE result += SIZES[OCTAVE][0] current_number -= OCTAVE - 1 end sizes = SIZES.fetch(current_number) result += case quality when :diminished then sizes.first - 1 when :minor then sizes.first when :perfect then sizes[0] when :major then sizes.last when :augmented then sizes.last + 1 end result * direction end
Private Instance Methods
# File lib/music/interval.rb, line 167 def normalized_number (number - 1) % 7 + 1 end