Class: Coltrane::Note

Inherits:
Object
  • Object
show all
Includes:
Multiton
Defined in:
lib/coltrane/note.rb

Overview

Describes a musical note, independent of octave (that’d be pitch)

Constant Summary collapse

NOTES =
{
  'C'  => 0,
  'C#' => 1,
  'Db' => 1,
  'D'  => 2,
  'D#' => 3,
  'Eb' => 3,
  'E'  => 4,
  'F'  => 5,
  'F#' => 6,
  'Gb' => 6,
  'G'  => 7,
  'G#' => 8,
  'Ab' => 8,
  'A'  => 9,
  'A#' => 10,
  'Bb' => 10,
  'B'  => 11
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Note

Returns a new instance of Note.



32
33
34
35
# File 'lib/coltrane/note.rb', line 32

def initialize(name)
  @name = name
  @number = NOTES[name]
end

Instance Attribute Details

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



8
9
10
# File 'lib/coltrane/note.rb', line 8

def name
  @name
end

#numberObject (readonly) Also known as: id

Returns the value of attribute number.



8
9
10
# File 'lib/coltrane/note.rb', line 8

def number
  @number
end

Class Method Details

.[](arg) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/coltrane/note.rb', line 39

def self.[](arg)
  name =
    case arg
    when Note then return arg
    when String then find_note(arg)
    when Numeric then NOTES.key(arg % 12)
    else
      raise InvalidNoteError, "Wrong type: #{arg.class}"
    end

  new(name) || (raise InvalidNoteError, arg.to_s)
end

.allObject



52
53
54
# File 'lib/coltrane/note.rb', line 52

def self.all
  %w[C C# D D# E F F# G G# A A# B].map { |n| Note[n] }
end

.find_note(str) ⇒ Object



56
57
58
59
# File 'lib/coltrane/note.rb', line 56

def self.find_note(str)
  NOTES.each_key { |k, _v| return k if str.casecmp?(k) }
  nil
end

Instance Method Details

#+(other) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/coltrane/note.rb', line 71

def +(other)
  case other
  when Interval then Note[number + other.semitones]
  when Numeric  then Note[number + other]
  when Note     then Chord.new(number + other.number)
  end
end

#-(other) ⇒ Object



79
80
81
82
83
84
# File 'lib/coltrane/note.rb', line 79

def -(other)
  case other
  when Numeric then Note[number - other]
  when Note    then Interval[other.number - number]
  end
end

#accident?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/coltrane/note.rb', line 67

def accident?
  [1, 3, 6, 8, 10].include?(number)
end

#enharmonic?(other) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
# File 'lib/coltrane/note.rb', line 90

def enharmonic?(other)
  case other
  when String then number == Note[other].number
  when Note then number == other.number
  end
end

#interval_to(note_name) ⇒ Object



86
87
88
# File 'lib/coltrane/note.rb', line 86

def interval_to(note_name)
  Note[note_name] - self
end

#pretty_nameObject



61
62
63
# File 'lib/coltrane/note.rb', line 61

def pretty_name
  @name.tr('b', "\u266D").tr('#', "\u266F")
end