Sha256: bcac6ea282de3f488783d910dc83ff84e23281ad6a08c8101815d63b89f98d4a

Contents?: true

Size: 1.1 KB

Versions: 20

Compression:

Stored size: 1.1 KB

Contents

class HeadMusic::PitchClass
  attr_reader :number

  SHARP_SPELLINGS = %w[C C# D D# E F F# G G# A A# B]
  FLAT_SPELLINGS = %w[C Db D Eb E F Gb G Ab A Bb B]

  def self.get(identifier)
    @pitch_classes ||= {}
    number = HeadMusic::Spelling.get(identifier).pitch_class.to_i if HeadMusic::Spelling.match(identifier)
    number ||= identifier.to_i % 12
    @pitch_classes[number] ||= new(number)
  end

  class << self
    alias_method :[], :get
  end

  def initialize(pitch_class_or_midi_number)
    @number = pitch_class_or_midi_number.to_i % 12
  end

  def to_i
    number
  end

  def +(semitones)
    HeadMusic::PitchClass.get(to_i + semitones.to_i)
  end

  def -(semitones)
    HeadMusic::PitchClass.get(to_i - semitones.to_i)
  end

  def ==(value)
    to_i == value.to_i
  end
  alias_method :enharmonic?, :==

  def intervals_to(other)
    delta = other.to_i - to_i
    inverse = delta > 0 ? delta - 12 : delta + 12
    [delta, inverse].sort_by(&:abs).map { |interval| HeadMusic::Interval.get(interval) }
  end

  def smallest_interval_to(other)
    intervals_to(other).first
  end

  private_class_method :new
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
head_music-0.17.0 lib/head_music/pitch_class.rb
head_music-0.16.2 lib/head_music/pitch_class.rb
head_music-0.16.0 lib/head_music/pitch_class.rb
head_music-0.14.8 lib/head_music/pitch_class.rb
head_music-0.14.7 lib/head_music/pitch_class.rb
head_music-0.14.5 lib/head_music/pitch_class.rb
head_music-0.14.4 lib/head_music/pitch_class.rb
head_music-0.14.2 lib/head_music/pitch_class.rb
head_music-0.14.1 lib/head_music/pitch_class.rb
head_music-0.13.2 lib/head_music/pitch_class.rb
head_music-0.11.9 lib/head_music/pitch_class.rb
head_music-0.11.8 lib/head_music/pitch_class.rb
head_music-0.11.7 lib/head_music/pitch_class.rb
head_music-0.11.6 lib/head_music/pitch_class.rb
head_music-0.11.5 lib/head_music/pitch_class.rb
head_music-0.11.4 lib/head_music/pitch_class.rb
head_music-0.11.3 lib/head_music/pitch_class.rb
head_music-0.11.2 lib/head_music/pitch_class.rb
head_music-0.11.1 lib/head_music/pitch_class.rb
head_music-0.10.0 lib/head_music/pitch_class.rb