Skip to content

Instantly share code, notes, and snippets.

@umbrellaprocess
Created August 16, 2014 04:38
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save umbrellaprocess/7a4ffe994765fc195d95 to your computer and use it in GitHub Desktop.
Just intonation MIDI keyboard with SuperCollider
/**
* Just intonation MIDI keyboard with SuperCollider
*/
MIDIIn.connect;
s.boot;
(
SynthDef("umbSimpleFM",{
arg freq=440, gate=1, amp=1, pan=0;
var m_amp, mod, car;
m_amp = 100;
mod = SinOsc.ar(freq*2, 0, m_amp);
car = SinOsc.ar(freq + mod, 0, amp);
car = EnvGen.kr(Env.adsr(0.01,0.3,0.5,1,0.6,-4),gate,doneAction: 2) * car;
Out.ar(0, Pan2.ar(car,pan));
}).add;
)
(
var keys, scale;
keys = Array.newClear(128);
scale = Scale.chromatic.postln;
scale.tuning_(\just); // Just intonation
//scale.tuning_(\et12); // Twelve-tone equal temperament
//scale.tuning_(\mean4); // Meantone, 1/4 Syntonic Comma
//scale.tuning_(\pythagorean); // Pythagorean
~noteOnFunc = {arg src, chan, num, vel;
var node, freq;
node = keys.at(num);
if (node.notNil, {
node.release;
keys.put(num, nil);
});
node = Synth.tail(
nil, "umbSimpleFM",[
\freq, {freq = num.keyToDegree(scale, 12).degreeToKey(scale).midicps},
\amp, vel/127
]);
keys.put(num, node);
[chan,num,freq,vel/127].postln;
};
MIDIIn.addFuncTo(\noteOn, ~noteOnFunc);
~noteOffFunc = {arg src, chan, num, vel;
var node;
node = keys.at(num);
if (node.notNil, {
node.release;
keys.put(num, nil);
});
};
MIDIIn.addFuncTo(\noteOff, ~noteOffFunc);
)
// cleanup
(
MIDIIn.removeFuncFrom(\noteOn, ~noteOnFunc);
MIDIIn.removeFuncFrom(\noteOff, ~noteOffFunc);
)
@Engid
Copy link

Engid commented Feb 20, 2018

Thank you! I've been trying to figure this out over the last week!

@Nancy2020CN
Copy link

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment