Ok i was going through the book "Ruby practical Projects" and right at
the first chapter i had this problem. The first chapter is all about
calling the alsa raw midi functions in ruby to play midi from ruby. I
think i did everything right but still i get no sound. I debuged my
project but at the lines when the function call is made nothing
happens, there is not even an error. Can anybody give me any pointers
to what should i do. Here is my code ..
 1 require 'dl/import'
  2
  3 class LiveMIDI
  4         ON = 0x90
  5         OFF = 0x80
  6         PC = 0xC0
  7
  8         def initialize
  9                 open
 10         end
 11
 12         def note_on(channel, note, velocity=64)
 13                 message(ON | channel, note, velocity)
 14         end
 15
 16         def note_off(channel, note, velocity=64)
 17                 message(OFF | channel, note, velocity)
 18         end
 19
 20         def program_change(channel, preset)
 21                 message(PC | channel, preset)
 22         end
 23 end
 24
 25 if RUBY_PLATFORM.include?('mswin')
 26         class LiveMIDI
 27                 # Windows code here
 28                 module C
 29                         extend DL::Importable
 30                         dlload 'winmm'
 31
 32
 33                         extern "int midiOutOpen(HMIDIOUT*, int,
int, int, int)"
 34                         extern "int midiOutClose(int)"
 35                         extern "int midiOutShortMsg(int, int)"
 36                 end
 37                 def open
 38                         @device = DL.malloc(DL.sizeof('I'))
 39                         C.midiOutOpen(@device, -1, 0, 0, 0)
 40                 end
 41                 def close
 42                         C.midiOutClose(@device.ptr.to_i)
 43                 end
 44                 def message(one, two=0, three=0)
 45                         message = one + (two << 8) + (three << 16)
 46                         C.midiOutShortMsg(@device.ptr.to_i,
message)
 47                 end
 48         end
 49
 50 elsif RUBY_PLATFORM.include?('darwin')
 51         class LiveMIDI
 52                 # Mac code here
 53         end
 54 elsif RUBY_PLATFORM.include?('linux')
 55         class LiveMIDI
 56                 module C
 57                         extend DL::Importable
 58                         dlload '/usr/lib/libasound.so.2.0.0'
 59
 60
 61                         extern "int snd_rawmidi_open(void*, void*,
char*, int)"
 62                         extern "int snd_rawmidi_close(void*)"
 63                         extern "int snd_rawmidi_write(void*,
void*, int)"
 64                         extern "int snd_rawmidi_drain(void*)"
 65                 end
 66
 67                 def open
 68                         @output = DL::PtrData.new(nil)
 69                         C.snd_rawmidi_open(nil,
@output.ref,"virtual",0)
 70                 end
 71
 72                 def close
 73                         C.snd_rawmidi_close(@output)
 74                 end
 75
 76                 def message(*args)
 77                         format= "C" * args.size
 78                         bytes = args.pack(format).to_ptr
 79                         C.snd_rawmidi_write(@output, bytes,
args.size)
 80                         C.snd_rawmidi_drain(@output)
 81                 end
 82         end
 83 else
 84         raise "Couldn't find a LiveMIDI implementation for your
platform"
 85 end
 86
 87
 88 #require 'music'
 89 midi = LiveMIDI.new
 90 sleep(8)
 91 midi.note_on(0,60,100)
 92 sleep(1)
 93 midi.note_off(0,60)
 94 sleep(1)
 95 midi.program_change(1,40)
 96 midi.note_on(1,60,100)
 97 sleep(1)
 98 midi.note_off(1,60)
 99 puts "done"

of course you could skip the windows part coz i never tested it
anyways.