Class: Coltrane::Theory::Note

Inherits:
PitchClass show all
Defined in:
lib/coltrane/theory/note.rb

Overview

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.

Constant Summary collapse

ALTERATIONS =
{
  'b' => -1,
  '#' => +1
}.freeze

Constants inherited from PitchClass

PitchClass::NOTATION

Instance Attribute Summary collapse

Attributes inherited from PitchClass

#integer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PitchClass

#<=>, #==, #accidental?, all, all_letters, #ascending_interval_to, #descending_interval_to, #enharmonic?, #fundamental_frequency, #letter, #pretty_name, size, #size, #true_notation

Constructor Details

#initialize(arg) ⇒ Note

Returns a new instance of Note.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

Instance Attribute Details

#alterationObject

Returns the value of attribute alteration.



17
18
19
# File 'lib/coltrane/theory/note.rb', line 17

def alteration
  @alteration
end

Class Method Details

.[](arg) ⇒ Object



42
43
44
# File 'lib/coltrane/theory/note.rb', line 42

def self.[](arg)
  new(arg)
end

Instance Method Details

#+(other) ⇒ Object



94
95
96
# 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) ⇒ Object



90
91
92
# File 'lib/coltrane/theory/note.rb', line 90

def -(other)
  super(other).yield_self { |r| r.is_a?(Note) ? r.alter(alteration) : r }
end

#accidentsObject



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

def accidents
  (@alteration > 0 ? '#' : 'b') * alteration.abs
end

#alter(x) ⇒ Object



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

def alter(x)
  Note.new(name).tap { |n| n.alteration = x }
end

#as(letter) ⇒ Object



98
99
100
101
# 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_classObject



50
51
52
# File 'lib/coltrane/theory/note.rb', line 50

def base_pitch_class
  PitchClass[integer - alteration]
end

#double_flat?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/coltrane/theory/note.rb', line 78

def double_flat?
  alteration == -2
end

#double_sharp?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/coltrane/theory/note.rb', line 70

def double_sharp?
  alteration == 2
end

#flat?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/coltrane/theory/note.rb', line 74

def flat?
  alteration == -1
end

#nameObject



46
47
48
# File 'lib/coltrane/theory/note.rb', line 46

def name
  "#{base_pitch_class}#{accidents}".gsub(/#b|b#/, '')
end

#natural?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/coltrane/theory/note.rb', line 82

def natural?
  alteration == 0
end

#pitch_classObject



54
55
56
# File 'lib/coltrane/theory/note.rb', line 54

def pitch_class
  PitchClass.new(self)
end

#sharp?Boolean

Returns:

  • (Boolean)


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

def sharp?
  alteration == 1
end