Expected behavior: middle C played on one midi instrunment, then another. Actual behavior: a DL deprecation warning and no sound. Running Windows 7.
The code:
require "dl/import"
class LiveMIDI
ON = 0x90
OFF =0x80
PC = 0xc0
def initialize
open
end
def note_on(channel, note, velocity=64)
message(ON | channel, note, velocity)
end
def note_off(channel, note, velocity=64)
message(OFF | channel, note, velocity)
end
def program_change(channel, preset)
message(PC | channel, preset)
end
module C
extend DL::Importer
dlload "winmm"
extern "int midiOutOpen(HMIDIOUT*, int, int, int, int)"
extern "int midiOutClose(int)"
extern "int midiOutShortMsg(int, int)"
end
def open
@device = DL.malloc(DL::Importer.sizeof("int"))
C.midiOutOpen(@device, -1, 0, 0, 0)
end
def close
C.midiOutClose(@device.ptr.to_i)
end
def message(one, two=0, three=0)
message = one + (two << 8) + (three << 16)
C.midiOutShortMsg(DL::CPtr.to_ptr(@device).to_i, message)
end
end
midi = LiveMIDI.new
midi.note_on(0, 60, 100)
sleep(1)
midi.note_off(0, 60)
midi.program_change(1, 40)
midi.note_on(1, 60, 100)
sleep(1)
midi.note_off(1, 60)
Taken from the book Practical Ruby Projects. By the numbers on the page, 11-15, in chapter 2. The code is slightly modified to handle changes to the Ruby DL in Ruby 1.9.