Sha256: dd48b3d6376991f90ff2416b5fed92a8a30946e1157c2720d10fb704efebca27

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

class HeadMusic::Pitch
  include Comparable

  attr_reader :spelling
  attr_reader :octave

  delegate :letter, :accidental, :pitch_class, to: :spelling

  def self.get(value)
    @pitches ||= {}
    @pitches[value] ||= from_name(value) || from_number(value)
  end

  def self.from_name(name)
    return nil unless name == name.to_s
    spelling = HeadMusic::Spelling.get(name)
    octave = name.scan(/-?\d+$/).first.to_i
    new(spelling, octave) if spelling && (-1..9).include?(octave)
  end

  def self.from_number(number)
    return nil unless number == number.to_i
    spelling = HeadMusic::Spelling.from_number(number)
    octave = (number.to_i / 12) - 1
    new(spelling, octave) if spelling && (-1..9).include?(octave)
  end

  def initialize(spelling, octave)
    @spelling = HeadMusic::Spelling.get(spelling.to_s)
    @octave = octave.to_i
  end

  def name
    [spelling, octave].join
  end

  def midi_note_number
    (octave + 1) * 12 + pitch_class.to_i
  end

  def to_s
    name
  end

  def to_i
    midi_note_number
  end

  def enharmonic?(other)
    self.midi_note_number == other.midi_note_number
  end

  def ==(value)
    to_s == value.to_s
  end

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

  private_class_method :new
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
head_music-0.1.1 lib/head_music/pitch.rb
head_music-0.1.0 lib/head_music/pitch.rb