Skip to content

Instantly share code, notes, and snippets.

@dqgorelick
Last active September 20, 2023 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dqgorelick/3726d556a5200b752b811ab88155232b to your computer and use it in GitHub Desktop.
Save dqgorelick/3726d556a5200b752b811ab88155232b to your computer and use it in GitHub Desktop.
TidalCycles mouse control via SuperCollider
// Mouse controlled SuperCollider synths for TidalCycles
(
SynthDef(\mousePan, {|out, pan, accelerate, freq, attack=0.01, decay=0.0, sustain=1, release=1.5, res=0.25 |
var sig, env;
env = EnvGen.kr(Env([0,1,1,0],[attack,decay,release],\sine), timeScale:sustain, doneAction: 2);
sig = Saw.ar(freq);
sig = RLPF.ar(sig, MouseY.kr(80, 24000, 1), res);
Out.ar(out, DirtPan.ar(sig * 0.7, ~dirt.numChannels, MouseX.kr(-2,2), env))
}).add;
)
(
SynthDef(\mouse, {|out, pan, accelerate, freq, attack=0.01, decay=0.8, sustain=1, release=0.1, res=0.2 |
var sig, env;
env = EnvGen.kr(Env([0,1,1,0],[attack,decay,release],\sine), timeScale:sustain, doneAction: 2);
sig = Saw.ar(freq);
sig = RLPF.ar(sig, MouseY.kr(80, 20000, 1), res);
Out.ar(out, DirtPan.ar(sig, ~dirt.numChannels, pan, env));
}).add;
)
// mouse to MIDI CC (you will need to set up a receiver for this, on Mac I have set up IAC Driver with port "cc"
MIDIClient.init;
// add a midi channel where you want to send the mouse CC
~midiOutCC = MIDIOut.newByName("IAC Driver", "cc");
~midiOutCC.latency = 0;
(
~lastMouseY = -1;
~lastMouseX = -1;
~my = SynthDef("sendMouseY",{
// this sets the number of times / second
~trigAmount = 60;
SendTrig.kr(Impulse.kr(~trigAmount),0,MouseY.kr(0, 127));
}).add;
~mx = SynthDef("sendMouseX",{
SendTrig.kr(Impulse.kr(60),1,MouseX.kr(0, 127));
}).add;
o = OSCFunc({ arg msg;
switch (msg[2])
{0} {
if (~lastMouseY == msg[3].trunc, {}, {
("Y > " ++ msg[3].trunc).postln;
// midi cc 55
~midiOutCC.control(0, 55, msg[3].trunc);
~lastMouseY = msg[3].trunc
});
}
{1} {
if (~lastMouseX == msg[3].trunc, {}, {
("X > " + msg[3].trunc).postln;
// midi cc 56
~midiOutCC.control(0, 56, msg[3].trunc);
~lastMouseX = msg[3].trunc
});
}
},'/tr', s.addr);
)
// start
Synth("sendMouseY");
Synth("sendMouseX");
// stop each of them
~my.free;
~mx.free;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment