class MIDI::Utils

Utility methods.

Constants

NOTE_NAMES

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

Public Class Methods

as_var_len(val) click to toggle source

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.

# 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) click to toggle source

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

# 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