Module: MIDIOps::Note
- Defined in:
- lib/midiops/note.rb
Constant Summary collapse
- KEY_OFFSETS =
{ C: 0, Csharp: 1, Dflat: 1, D: 2, Dsharp: 3, Eflat: 3, E: 4, F: 5, Fsharp: 6, Gflat: 6, G: 7, Gsharp: 8, Aflat: 8, A: 9, Asharp: 10, Bflat: 10, B: 11 }
Class Method Summary collapse
Class Method Details
.key_to_code(key, octave) ⇒ Object
35 36 37 |
# File 'lib/midiops/note.rb', line 35 def key_to_code key, octave KEY_OFFSETS[key] + 12 + (12 * octave) end |
.note_to_code(note) ⇒ Object
57 58 59 60 |
# File 'lib/midiops/note.rb', line 57 def note_to_code note res = parse_note note key_to_code res[:key], res[:octave] end |
.parse_note(note) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/midiops/note.rb', line 39 def parse_note note # formats to accept: "C3" / "B-1" / "C#4" (sharp) / "Db5" (flat) case note when /^([A-Ga-g])(-?[0-9]{1,2})$/ m = $~.captures {key: m[0].upcase.to_sym, octave: m[1].to_i} when /^([CDFGAcdfga])\#(-?[0-9]{1,2})$/ m = $~.captures {key: "#{m[0].upcase}sharp".to_sym, octave: m[1].to_i} when /^([DEGABdegab])b(-?[0-9]{1,2})$/ m = $~.captures {key: "#{m[0].upcase}flat".to_sym, octave: m[1].to_i} else raise RuntimeError, "Invalid note format: #{note}" end end |