Skip to content

Instantly share code, notes, and snippets.

@carltesta
Last active January 23, 2023 17:15
Show Gist options
  • Save carltesta/bb5065a7b92bab7673237e9cc1c9a612 to your computer and use it in GitHub Desktop.
Save carltesta/bb5065a7b92bab7673237e9cc1c9a612 to your computer and use it in GitHub Desktop.
14-bit MIDI Handler for SuperCollider
FourteenBitCC {
/*
//Use Case:
MIDIIn.connectAll;
~x = FourteenBitCC.new("x", 72, 104);
~x.func = {|val| ("x: "++val).postln};
//MPE Example with Sensel Morph
MIDIIn.connectAll
~sensel = 4.collect({|n|
FourteenBitCC.new(("x"++n), 72, 104, n)
.func = {|val,chan| ("x: "++val).postln; ~sounds[n].set(\freq, val.linexp(0,16383,20,2000))};
FourteenBitCC.new(("y"++n), 74, 106, n)
.func = {|val| ("y: "++val).postln;~sounds[n].set(\width, val.linlin(0,16383,0.01,0.99))};
FourteenBitCC.new(("z"++n), 76, 108, n)
.func = {|val| ("z: "++val).postln;~sounds[n].set(\amp, val.linlin(0,16383,0,1.0))};
});
SynthDef(\pulse, {|freq=440, width=0.5, amp=0, out=0|
var sound = Pulse.ar(freq, width, amp);
Out.ar(out, sound);
}).add;
SynthDef(\reverb, {|in, mix=0.5|
var sound = FreeVerb.ar(In.ar(in), mix);
Out.ar(0, sound!2);
}).add;
~reverbBus = Bus.audio(s, 1);
~osc = Group.new(s, \addToHead);
~verb = Group.new(s, \addToTail);
~sounds = 4.collect({Synth.new(\pulse, [\out, ~reverbBus], ~osc)});
~reverb = Synth.new(\reverb, [\in, ~reverbBus], ~verb);
*/
var <>label;
var <>cc1;
var <>cc2;
var <>chan;
var <>func;
var msb;
var lsb;
var value;
*new {arg label = "test", cc1 = nil, cc2 = nil, chan = nil;
^super.new.init(label, cc1, cc2, chan)
}
init { arg aLabel, aCc1, aCc2, aChan;
label = aLabel;
cc1 = aCc1;
cc2 = aCc2;
chan = aChan;
MIDIdef.cc(label.asSymbol, {
|val,num,chan,src|
case
{num==cc1}{this.msbSet(val)}
{num==cc2}{this.lsbSet(val)}
}, [cc1,cc2],chan);
}
msbSet { arg byte;
msb = byte;
this.check;
}
lsbSet { arg byte;
lsb = byte;
this.check;
}
check {
if(lsb.notNil and: { msb.notNil }) {
value = (msb << 7 + lsb);
func.(value,chan);
msb = nil;
lsb = nil;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment