Class: Coltrane::Note
- Inherits:
-
PitchClass
- Object
- PitchClass
- Coltrane::Note
- 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
Instance Attribute Summary collapse
-
#alteration ⇒ Object
Returns the value of attribute alteration.
Attributes inherited from PitchClass
Class Method Summary collapse
Instance Method Summary collapse
- #accidents ⇒ Object
- #alter(x) ⇒ Object
- #base_pitch_class ⇒ Object
-
#initialize(arg) ⇒ Note
constructor
A new instance of Note.
- #interval_to(note_name) ⇒ Object
- #name ⇒ Object
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 InvalidNoteError, arg unless ('A'..'G').cover?(letter) @alteration = chars.reduce(0) do |alt, symbol| raise InvalidNoteError, arg unless ALTERATIONS.include?(symbol) alt + ALTERATIONS[symbol] end super((PitchClass[letter].integer + @alteration) % PitchClass.size) end |
Instance Attribute Details
#alteration ⇒ Object
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
#accidents ⇒ Object
61 62 63 |
# File 'lib/coltrane/note.rb', line 61 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 |
#base_pitch_class ⇒ Object
49 50 51 |
# File 'lib/coltrane/note.rb', line 49 def base_pitch_class PitchClass[integer - alteration] end |
#interval_to(note_name) ⇒ Object
65 66 67 |
# File 'lib/coltrane/note.rb', line 65 def interval_to(note_name) Note[note_name] - self end |
#name ⇒ Object
45 46 47 |
# File 'lib/coltrane/note.rb', line 45 def name "#{base_pitch_class}#{accidents}".gsub(/#b|b#/, '') end |