Class: MIDI::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/midilib/utils.rb

Overview

Utility methods.

Constant Summary collapse

NOTE_NAMES =

MIDI note names. NOTE_NAMES is ‘C’, NOTE_NAMES is ‘C#’, etc.

[
  'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'
]

Class Method Summary collapse

Class Method Details

.as_var_len(val) ⇒ Object

Given an integer, returns it as a variable length array of bytes (the format used by MIDI files).

The converse operation–converting a var len into a number–requires input from a stream of bytes. Therefore we don’t supply it here. That is a part of the MIDIFile class.



22
23
24
25
26
27
28
29
30
31
# File 'lib/midilib/utils.rb', line 22

def self.as_var_len(val)
  buffer = []
  buffer << (val & 0x7f)
  val = (val >> 7)
  while val > 0
    buffer << (0x80 + (val & 0x7f))
    val = (val >> 7)
  end
  buffer.reverse!
end

.note_to_s(num) ⇒ Object

Given a MIDI note number, return the name and octave as a string.



10
11
12
13
14
# File 'lib/midilib/utils.rb', line 10

def self.note_to_s(num)
  note = num % 12
  octave = num / 12
  "#{NOTE_NAMES[note]}#{octave - 1}"
end