Sha256: d14cab22b5e7b4528c4a61b517580f6915380f33fc8466b097a62a60b773a479

Contents?: true

Size: 962 Bytes

Versions: 3

Compression:

Stored size: 962 Bytes

Contents

class HeadMusic::Interval
  include Comparable

  private_class_method :new

  NAMES = %w{perfect_unison minor_second major_second minor_third major_third perfect_fourth tritone perfect_fifth minor_sixth major_sixth minor_seventh major_seventh perfect_octave}

  attr_reader :semitones

  def self.get(identifier)
    @intervals ||= {}
    candidate = identifier.to_s.downcase.gsub(/\W+/, '_')
    semitones = NAMES.index(candidate) || identifier.to_i
    @intervals[semitones] ||= new(semitones.to_i)
  end

  def initialize(semitones)
    @semitones = semitones
  end

  def simple
    HeadMusic::Interval.get(semitones % 12)
  end

  def simple?
    (0..12).include?(semitones)
  end

  def compound?
    semitones > 12
  end

  def to_i
    semitones
  end

  def +(value)
    HeadMusic::Interval.get(to_i + value.to_i)
  end

  def -(value)
    HeadMusic::Interval.get((to_i - value.to_i).abs)
  end

  def <=>(other)
    self.to_i <=> other.to_i
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
head_music-0.17.0 lib/head_music/interval.rb
head_music-0.16.2 lib/head_music/interval.rb
head_music-0.16.0 lib/head_music/interval.rb