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.



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

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

.note_to_s(num) ⇒ Object

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



12
13
14
15
16
# File 'lib/midilib/utils.rb', line 12

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