Class: Stave::Theory::Note

Inherits:
Core::Lookup show all
Defined in:
lib/stave/theory/note.rb

Defined Under Namespace

Classes: PitchClass

Instance Attribute Summary

Attributes inherited from Core::Lookup

#variant

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Lookup

#==, each_key, find_by, #initialize, keys, string_keys, variant, variant?, variant_lookup, variants, where, with_options

Constructor Details

This class inherits a constructor from Stave::Core::Lookup

Class Method Details

.circle_of_fifthsObject



135
136
137
# File 'lib/stave/theory/note.rb', line 135

def self.circle_of_fifths
  Circle.new(type: CircleType.fifths, root: Note.c_natural).notes
end

.flatsObject



119
120
121
# File 'lib/stave/theory/note.rb', line 119

def self.flats
  where(accidental: Accidental.flat)
end

.naturalsObject



123
124
125
# File 'lib/stave/theory/note.rb', line 123

def self.naturals
  where(accidental: Accidental.natural)
end

.sharpsObject



127
128
129
# File 'lib/stave/theory/note.rb', line 127

def self.sharps
  where(accidental: Accidental.sharp)
end

.single_accidentalObject



131
132
133
# File 'lib/stave/theory/note.rb', line 131

def self.single_accidental
  flats + naturals + sharps
end

Instance Method Details

#+(other) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/stave/theory/note.rb', line 84

def +(other)
  case other
  when Interval then note_above(other)
  when Integer then Note.find_by(to_i: (to_i + other) % 12)
  else raise
  end
end

#-(other) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/stave/theory/note.rb', line 92

def -(other)
  case other
  when Interval then note_below(other)
  when Integer then Note.find_by(to_i: (to_i - other) % 12)
  else raise
  end
end

#note_above(interval) ⇒ Object



108
109
110
111
112
113
# File 'lib/stave/theory/note.rb', line 108

def note_above(interval)
  target_pitch_class = pitch_class + interval.number.offset
  target_integer = (to_i + interval.to_i) % 12

  Note.find_by(pitch_class: target_pitch_class, to_i: target_integer)
end

#note_below(interval) ⇒ Object



115
116
117
# File 'lib/stave/theory/note.rb', line 115

def note_below(interval)
  note_above(interval.invert!)
end

#symbolObject



100
101
102
# File 'lib/stave/theory/note.rb', line 100

def symbol
  "#{pitch_class.symbol}#{accidental.symbol}"
end

#to_iObject



104
105
106
# File 'lib/stave/theory/note.rb', line 104

def to_i
  (pitch_class.to_i + accidental.transform) % 12
end