Mapping linux input events to OSC

Nikolaus Gradwohl2009-09-06T04:48:00+00:00

In linux every input device registers under /dev/input/eventX and sends events in 16 byte blocks. these data chunks contain a timestamp the event type, the event code and a value depending on the event type the semantic of value changes. for a event type 1 (key event) value 1 means pressed and value 0 mean released.

here is a little ruby program that reacts to key press events and sends osc messages. which could trigger drum sounds, start play back, ...

key press events are not only sent by keyboards but also by mice, joystics, gamepads, a DDR mat, a buzz buzzer, ...

the code example is used to map keys of my PS3 Controller to drum sounds.

require 'osc';

c = OSC::UDPSocket.new

hh = OSC::Message.new('/drum', 's', "hh" )
bd = OSC::Message.new('/drum', 's', "bd" )
sn = OSC::Message.new('/drum', 's', "sn" )

File.open("/dev/input/event7") do |f|
  while true
    event = f.read(16).unpack("llSSl")
    time = event[0] + event[1] / 1000000.0
    type = event[2]
    code = event[3]
    value = event[4]

    puts "Ti: #{time} T: #{type} C: #{code} V: #{value} " if type == 1

    c.send( bd, 0, 'localhost', 3334 ) if type == 1 && code == 298 && value == 1
    c.send( sn, 0, 'localhost', 3334 ) if type == 1 && code == 293 && value == 1
    c.send( hh, 0, 'localhost', 3334 ) if type == 1 && code == 303 && value == 1
  end
end

The code was inspired by 2 articles i found recently the first one is about how to use a DDR-mat to trigger Drum Sounds and the second one shows how to read the accelerometer data from a openmonoko phone

Tweet This! submit to reddit Digg! Tags: | 3 comments | no trackbacks

See also:

ruby osc seqencer version2
osc sequencer in ruby
sketch experiment 7 - osc events
Ronin experiment 5 - osc
Sonic Pi beatslicing livecoding session

Trackbacks

Comments

Leave a response

  1. fake 2009-09-09T01:02:58+00:00

    very cool - this reminds me of: http://www.youtube.com/watch?v=mcz5VbxhDTc

  2. gleahe 2011-12-22T10:12:50+00:00

    Hi, just wanting to say that you made several good points there. I did a search on the issue and found that nearly all people go along with with your blog. keep the updates going!

  3. wHorseo 2012-02-16T06:52:35+00:00

    another solid post here. is it possible to cite some of your sources though? i would love to know exactly where you get your facts. anyway, excellent stuff.

Leave a comment