# Ruby midi problem

**URL:** <https://www.ruby-forum.com/t/ruby-midi-problem/156088>\
**Category:** Ruby\
**Created:** [December 20, 2008, 8:56am UTC](https://www.ruby-forum.com/t/ruby-midi-problem/156088 "2008-12-20T08:56:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rick\_2047](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecd19e/32.png) [@rick\_2047](https://www.ruby-forum.com/u/rick_2047)\
**Post date:** [December 20, 2008, 8:56am UTC](https://www.ruby-forum.com/t/ruby-midi-problem/156088/1 "2008-12-20T08:56:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rick\_2047](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecd19e/32.png) [@rick\_2047](https://www.ruby-forum.com/u/rick_2047)\
**Post date:** [December 21, 2008, 5:22am UTC](https://www.ruby-forum.com/t/ruby-midi-problem/156088/2 "2008-12-21T05:22:59Z")

</div>

On Sat, Dec 20, 2008, rick\_2047 wrote:

> 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 …

A couple of things!

1. Did you follow the steps to patch the MIDI output to your  
synthesizer? I’ve had good luck with qjackctl.

2. If you’re just doing this as an exercise, cool… if not, you should  
check out MIDIator, which just packages up the Practical Ruby Projects  
code in a gem with some extra niceness 🙂 But, if you’re trying to  
learn please do continue with the book.

Ben

---

<div class="post-metadata">

**Author:** ![rick\_2047](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecd19e/32.png) [@rick\_2047](https://www.ruby-forum.com/u/rick_2047)\
**Post date:** [December 21, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/ruby-midi-problem/156088/3 "2008-12-21T20:42:59Z")

</div>

I have tried qjackctl but i still don’t get any sound, everything goes  
fine i start timidity and qjackctl the connection is fine but i get no  
audio.

---

<div class="post-metadata">

**Author:** ![rick\_2047](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecd19e/32.png) [@rick\_2047](https://www.ruby-forum.com/u/rick_2047)\
**Post date:** [December 23, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/ruby-midi-problem/156088/4 "2008-12-23T22:48:57Z")

</div>

On Mon, Dec 22, 2008, rick\_2047 wrote:

> I have tried qjackctl but i still don’t get any sound, everything goes  
> fine i start timidity and qjackctl the connection is fine but i get no  
> audio.

Don’t know what to tell you then, sorry. If you have access to a  
Windows or OSX machine, you might want to try your code there to try to  
isolate it to being a linux problem.

Ben
