Class: Fet::MidiNote

Inherits:
Object
  • Object
show all
Defined in:
lib/fet/midi_note.rb

Overview

Class that handles MIDI values that represent notes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(midi_value) ⇒ MidiNote

Returns a new instance of MidiNote.



10
11
12
13
# File 'lib/fet/midi_note.rb', line 10

def initialize(midi_value)
  self.midi_value = midi_value
  validate_midi_value!
end

Instance Attribute Details

#midi_valueObject

Returns the value of attribute midi_value.



8
9
10
# File 'lib/fet/midi_note.rb', line 8

def midi_value
  @midi_value
end

Class Method Details

.from_note(note_name, octave_number) ⇒ Object



15
16
17
18
19
# File 'lib/fet/midi_note.rb', line 15

def self.from_note(note_name, octave_number)
  midi_value_for_c = 12 * (1 + octave_number)
  midi_value = midi_value_for_c + MusicTheory.semitones_from_c(note_name)
  return new(midi_value)
end

Instance Method Details

#degree(root_midi_value) ⇒ Object

NOTE: This is not strictly correct because e.g. the midi value of 63 can be D#4 or Eb4, which affects what degree it actually is. However, without additional information, this is good enough.



27
28
29
# File 'lib/fet/midi_note.rb', line 27

def degree(root_midi_value)
  return (midi_value - root_midi_value) % 12
end

#octave_numberObject



21
22
23
# File 'lib/fet/midi_note.rb', line 21

def octave_number
  return (midi_value - 12) / 12
end