Class: Coltrane::Note

Inherits:
PitchClass show all
Defined in:
lib/coltrane/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, #enharmonic?, #fundamental_frequency, #pretty_name, size, #size, #true_notation

Constructor Details

#initialize(arg) ⇒ Note

Returns a new instance of Note.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/coltrane/note.rb', line 23

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.



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

def alteration
  @alteration
end

Class Method Details

.[](arg) ⇒ Object



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

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

Instance Method Details

#+(other) ⇒ Object



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

def +(other)
  result = super(other)
  result.is_a?(Note) ? result.alter(alteration) : result
end

#-(other) ⇒ Object



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

def -(other)
  result = super(other)
  result.is_a?(Note) ? result.alter(alteration) : result
end

#accidentsObject



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

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

#alter(x) ⇒ Object



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

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

#as(letter) ⇒ Object



99
100
101
102
# File 'lib/coltrane/note.rb', line 99

def as(letter)
  a = (self - Note[letter])
  alter([a.semitones, -((-a).semitones)].min_by(&:abs))
end

#base_pitch_classObject



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

def base_pitch_class
  PitchClass[integer - alteration]
end

#double_flat?Boolean

Returns:

  • (Boolean)


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

def double_flat?
  alteration == -2
end

#double_sharp?Boolean

Returns:

  • (Boolean)


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

def double_sharp?
  alteration == 2
end

#flat?Boolean

Returns:

  • (Boolean)


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

def flat?
  alteration == -1
end

#interval_to(note_name) ⇒ Object



95
96
97
# File 'lib/coltrane/note.rb', line 95

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

#nameObject



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

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

#natural?Boolean

Returns:

  • (Boolean)


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

def natural?
  alteration == 0
end

#sharp?Boolean

Returns:

  • (Boolean)


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

def sharp?
  alteration == 1
end