Class: Stave::Theory::Note

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

Overview

rubocop:disable Metrics/ClassLength

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



241
242
243
# File 'lib/stave/theory/note.rb', line 241

def self.circle_of_fifths
  where(circle_of_fifths?: true)
end

.double_flatsObject



217
218
219
# File 'lib/stave/theory/note.rb', line 217

def self.double_flats
  where(accidental: Accidental.double_flat)
end

.double_sharpsObject



233
234
235
# File 'lib/stave/theory/note.rb', line 233

def self.double_sharps
  where(accidental: Accidental.double_sharp)
end

.flatsObject



221
222
223
# File 'lib/stave/theory/note.rb', line 221

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

.naturalsObject



225
226
227
# File 'lib/stave/theory/note.rb', line 225

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

.sharpsObject



229
230
231
# File 'lib/stave/theory/note.rb', line 229

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

.single_accidentalObject



237
238
239
# File 'lib/stave/theory/note.rb', line 237

def self.single_accidental
  flats + naturals + sharps
end

Instance Method Details

#+(other) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/stave/theory/note.rb', line 182

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



190
191
192
193
194
195
196
# File 'lib/stave/theory/note.rb', line 190

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



206
207
208
209
210
211
# File 'lib/stave/theory/note.rb', line 206

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



213
214
215
# File 'lib/stave/theory/note.rb', line 213

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

#symbolObject



198
199
200
# File 'lib/stave/theory/note.rb', line 198

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

#to_iObject



202
203
204
# File 'lib/stave/theory/note.rb', line 202

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