New quark: ddwSpeedLim

Something I’ve wanted for awhile: An analog to Max’s [speedlim] object.

Use case: Say you’re getting 50 OSC messages per second but you want to respond no more often than 10 times per second.

Old way: Maybe SkipJack, but that always quantizes time and may fire the action redundantly.

New way: OSCFunc(SpeedLim(0.1, { |msg| ... do stuff ... })). No fuss, no muss – the SpeedLim substitutes in place of the action function (any action function – GUIs, MIDI, OSC, HID, …).

(
var uv, synth, resp, j = 0, speedlim;
​
w = Window("SpeedLim",
    Rect.aboutPoint(Window.screenBounds.center, 200, 200)
).front;
​
w.layout = VLayout(
    uv = UserView().background_(Color(0.1, 0.1, 0.4))
);
​
w.onClose = { synth.free; resp.free };
​
// high-rate transmission of mouse data (50x per second)
s.waitForBoot {
    synth = {
        var trig = Impulse.kr(50);
        var mouse = Latch.kr(MouseY.kr(0, 1, 0, 0), trig);
        SendReply.kr(Changed.kr(mouse), '/mouseY', mouse);
        Silent.ar(1)
    }.play;
};
​
// Receive them all, but consume them more slowly.
// SpeedLim is 'value'd 50x per second
// but fires its own action no sooner than 0.15 seconds.
// Note that the arguments passed into SpeedLim's function
// are the same as those passed to the OSCFunc function
// (so all data given to OSCFunc are available downstream).
resp = OSCFunc(
    func: SpeedLim(0.15, { |msg, time, addr|
        [msg, time, addr].postln;
        j = msg[3];
        // SpeedLim on AppClock saves you from 'defer'!
        uv.refresh;
    }, AppClock),
    path: '/mouseY',
    srcID: s.addr
);
​
uv.drawFunc = { |view|
    var b = view.bounds.moveTo(0, 0);
    Pen.color_(Color.white)
    .fillOval(
        Rect.aboutPoint(
            b.center,
            b.width * j * 0.5,
            b.height * j * 0.5
        )
    );
};
)

Currently: Quarks.install("https://github.com/jamshark70/ddwSpeedLim") – eventually that will be just Quarks.install("ddwSpeedLim") but I only just requested it to be added to the main directory.

hjh

7 Likes