$LOAD_PATH[0, 0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'win32api'
require 'midilib'
 
MIDIOUTOPEN = Win32API.new("winmm","midiOutOpen","pllll","l")
MIDIOUTCLOSE = Win32API.new("winmm","midiOutClose","l","l")
MIDIOUTSHORTMSG = Win32API.new("winmm","midiOutShortMsg","ll","l")
#MCISENDSTRING = Win32API.new("winmm","mciSendString","pll","l")
hmidi = [0].pack("L")
MIDIOUTOPEN.call(hmidi,-1,0,0,0)
$hMidiOut = hmidi.unpack("L").first
 
FILE = './2012822182130.mid'
seq = MIDI::Sequence.new()
File.open(FILE, 'rb') { | file |
    seq.read(file) { | track, num_tracks, i |
    }
}
 
def MidiOutMessage (iStatus,iChannel,iData1,iData2)
        dwMessage = iStatus | iChannel | (iData1 << 8) | (iData2 << 16)
        return MIDIOUTSHORTMSG.call($hMidiOut, dwMessage)
end
 
seq.each{|track|
          track.each{|event|
            print_decimal_numbers = true
            print_note_names = true
            note = event.instance_variable_get(:@note)
            time = event.instance_variable_get(:@delta_time)
            status = event.instance_variable_get(:@status)
            channel = event.instance_variable_get(:@channel)
            velocity = event.instance_variable_get(:@velocity)
            print "#{note} #{time} #{status} #{channel} #{velocity}\n" if note != nil
            next if note == nil
            sleep(time/1000.0)
            MidiOutMessage(status, channel, note, velocity)
          }
}
 
MIDIOUTCLOSE.call
$LOAD_PATH[0, 0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'win32api'
require 'midilib'
 
MIDIOUTOPEN = Win32API.new("winmm","midiOutOpen","pllll","l")
MIDIOUTCLOSE = Win32API.new("winmm","midiOutClose","l","l")
MIDIOUTSHORTMSG = Win32API.new("winmm","midiOutShortMsg","ll","l")
#MCISENDSTRING = Win32API.new("winmm","mciSendString","pll","l")
hmidi = [0].pack("L")
MIDIOUTOPEN.call(hmidi,-1,0,0,0)
$hMidiOut = hmidi.unpack("L").first
 
FILE = './2012822182130.mid'
seq = MIDI::Sequence.new()
File.open(FILE, 'rb') { | file |
    seq.read(file) { | track, num_tracks, i |
    }
}
 
def MidiOutMessage (iStatus,iChannel,iData1,iData2)
        dwMessage = iStatus | iChannel | (iData1 << 8) | (iData2 << 16)
        return MIDIOUTSHORTMSG.call($hMidiOut, dwMessage)
end
 
seq.each{|track|
          track.each{|event|
            print_decimal_numbers = true
            print_note_names = true
            note = event.instance_variable_get(:@note)
            time = event.instance_variable_get(:@delta_time)
            status = event.instance_variable_get(:@status)
            channel = event.instance_variable_get(:@channel)
            velocity = event.instance_variable_get(:@velocity)
            print "#{note} #{time} #{status} #{channel} #{velocity}\n" if note != nil
            next if note == nil
            sleep(time/1000.0)
            MidiOutMessage(status, channel, note, velocity)
          }
}
 
MIDIOUTCLOSE.call