Sha256: d6ce18a8f361bf56a82d4f4fc8205ce6dfb1d48cb757da430248540d019b82ab

Contents?: true

Size: 855 Bytes

Versions: 1

Compression:

Stored size: 855 Bytes

Contents

module Clef

  class Note

    NOTES = %w(C- C# D- D# E- F- F# G- G# A- A# B-)

    attr_reader :octave

    def initialize(key, accent, octave)
      @key, @accent, @octave = key, accent, octave
    end

    def to_s
      "#{@key}#{@accent}#{@octave}"
    end

    def +(rhs)
      Clef::Note.from_i(to_i + rhs)
    end

    def -(rhs)
      Clef::Note.from_i(to_i - rhs)
    end

    def self.from_i(integer)
      key = NOTES[integer % 12]
      octave = (integer / 12).to_i
      new(key[0..0], key[1..1], octave)
    end

    def to_i
      NOTES.index("#{@key.upcase}#{@accent}") + (octave * 12)
    end

    def sharp?
      @accent == '#'
    end

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

    def ==(rhs)
      if rhs === Note
        to_s == rhs.to_s
      elsif rhs.is_a?(String)
        to_s == rhs
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
teejayvanslyke-clef-0.0.0 lib/clef/note.rb