osc sequencer in ruby

Nikolaus Gradwohl2009-01-20T05:29:00+01:00

I wrote a simple osc sequencer in ruby using rosc. The script triggers the drum-kit and the bass i implemented last week.

the pattern may also be of different length. This allows very interesting polyrythmic loops.

require 'osc'
Host = 'localhost'
Port = 3334

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" )

ba = OSC::Message.new('/bass', 'i', 64 )


#c.send sn, 0, Host, Port
bpm = 120
step = 1.0/8;
s = bpm * step / 60.0 ;

snd = [ bd, sn, hh ]
pattern = [
[1, 0, 0, 1, 0, 1, 0, 0 ],
[0, 0, 1, 0, 0, 0, 1, 1 ],
[1, 1, 1, 1, 1, 1, 1, 1 ]
]
bass = [
40, 40, 0, 40, 40, 0, 43, 0,
40, 40, 0, 40, 40, 0, 38, 0 ]

count = 0;
while(true)
    (0..2).each { |i|
        c.send(snd[i], 0, Host, Port) if pattern[i][count % pattern[i].length] == 1
    }

    ba.args = [bass[ count % bass.length]]
    c.send( ba, 0, Host, Port ) if bass[ count % bass.length ] != 0

    sleep s
    count+=1
end

The main loop of the chuck part initializes an osc listener and the instruments and mapps the osc-events to the sounds. The instrument is started with

chuck Bass.ck DrumSet.ck osclisten.ck

(be sure the classfiles from last week are in the same directory, or adapt the paths).

osclisten.ck:

DrumSet drm;
drm.connect( dac );

Bass bass;
bass.connect( dac );

// OSC LISTENER
OscRecv orec;
3334 => orec.port;
orec.listen();

spork ~ drumevent();
bassevent();

fun void drumevent() {
    orec.event("/drum,s") @=> OscEvent e;
    while ( true ) {
        e => now;
        while( e.nextMsg() != 0 ) {
            e.getString() => string msg;
            if ( msg == "hh" ) { drm.hh(); }
            if ( msg == "bd" ) { drm.bd(); }
            if ( msg == "sn" ) { drm.sn(); }
        }
    }
}

fun void bassevent() {
    orec.event( "/bass,i") @=> OscEvent e;
    while( true ) {
        e => now;
        while( e.nextMsg() != 0 ) {
           e.getInt() => int tone;
           spork ~ bass.bass( tone );
        }
    }
}
Tweet This! submit to reddit Digg!   Tags: | 1 comments | no trackbacks

See also:

Mapping linux input events to OSC
ruby osc seqencer version2
Chuck iControl Projectpage
chuck icontrol
Calculating Euclidean Rhythmns using the Bresenham Algorithm

Trackbacks

Comments

Leave a response

  1. Matthew 2009-07-15T23:03:32+02:00

    I'm looking into generating music based on changes (time-scoped) to a subversion repository. Your post(s) quickly got me running chuck and helped me understand the basic technology.

    Thank-you.

    If my "project" interests you feel free to e-mail me.

    BTW - I like the WTF recorder too

Leave a comment