class Coltrane::Theory::Note

Notes are different ways of calling pitch classes. In the context of equal tempered scales, they're more like a conceptual subject for matters of convention than an actual thing.

Take for example A# and Bb. Those are different notes. Nevertheless, in the context of equal tempered scales they represent pretty much the same frequency.

The theory of notes have changed too much in the course of time, which lead us with a lot of conventions and strategies when dealing with music. That's what this class is for.

Constants

ALTERATIONS

Attributes

alteration[R]

Public Class Methods

[](arg) click to toggle source
# File lib/coltrane/theory/note.rb, line 42
def self.[](arg)
  new(arg)
end
new(arg) click to toggle source
Calls superclass method
# File lib/coltrane/theory/note.rb, line 24
def initialize(arg)
  note_name = case arg
              when String then arg
              when PitchClass then arg.true_notation
              when Numeric, Frequency then PitchClass.new(arg).true_notation
              else raise(WrongArgumentsError, arg)
              end

  chars  = note_name.chars
  letter = chars.shift
  raise InvalidNoteLetterError, arg unless ('A'..'G').cover?(letter)
  @alteration = chars.reduce(0) do |alt, symbol|
    raise InvalidNoteLetterError, arg unless ALTERATIONS.include?(symbol)
    alt + ALTERATIONS[symbol]
  end
  super((PitchClass[letter].integer + @alteration) % PitchClass.size)
end

Public Instance Methods

+(other) click to toggle source
Calls superclass method
# File lib/coltrane/theory/note.rb, line 94
def +(other)
  super(other).yield_self { |r| r.is_a?(Note) ? r.alter(alteration) : r }
end
-(other) click to toggle source
Calls superclass method
# File lib/coltrane/theory/note.rb, line 90
def -(other)
  super(other).yield_self { |r| r.is_a?(Note) ? r.alter(alteration) : r }
end
accidents() click to toggle source
# File lib/coltrane/theory/note.rb, line 86
def accidents
  (@alteration > 0 ? '#' : 'b') * alteration.abs
end
alter(x) click to toggle source
# File lib/coltrane/theory/note.rb, line 62
def alter(x)
  Note.new(name).tap { |n| n.alteration = x }
end
alteration=(a) click to toggle source
# File lib/coltrane/theory/note.rb, line 58
def alteration=(a)
  @alteration = a unless PitchClass[integer - a].accidental?
end
as(letter) click to toggle source
# File lib/coltrane/theory/note.rb, line 98
def as(letter)
  a = (Note[letter] - self)
  alter([a.semitones, -(-a).semitones].min_by(&:abs))
end
base_pitch_class() click to toggle source
# File lib/coltrane/theory/note.rb, line 50
def base_pitch_class
  PitchClass[integer - alteration]
end
double_flat?() click to toggle source
# File lib/coltrane/theory/note.rb, line 78
def double_flat?
  alteration == -2
end
double_sharp?() click to toggle source
# File lib/coltrane/theory/note.rb, line 70
def double_sharp?
  alteration == 2
end
flat?() click to toggle source
# File lib/coltrane/theory/note.rb, line 74
def flat?
  alteration == -1
end
name() click to toggle source
# File lib/coltrane/theory/note.rb, line 46
def name
  "#{base_pitch_class}#{accidents}".gsub(/#b|b#/, '')
end
natural?() click to toggle source
# File lib/coltrane/theory/note.rb, line 82
def natural?
  alteration == 0
end
pitch_class() click to toggle source
# File lib/coltrane/theory/note.rb, line 54
def pitch_class
  PitchClass.new(self)
end
sharp?() click to toggle source
# File lib/coltrane/theory/note.rb, line 66
def sharp?
  alteration == 1
end