{
   "author" : "meunier.fabien",
   "name" : "midi soft takeover",
   "ancestor_list" : [],
   "description" : "it avoids jump of value when the value on your GUI and the value of your midi controller are differents. In other words your midi controller take control only when it reaches the value of your GUI. Thanks to sludgefree and alberto de campo for their\r\nhelp",
   "labels" : [
      "midi control change cc soft takeover gui"
   ],
   "code" : "(\r\nMIDIIn.connectAll;\r\nSynthDef(\\SimpleSynth, { arg out = 0, freq = 1000, t_trig = 0, level =\r\n0.2;\r\n       var sig, env;\r\n    env = EnvGen.kr(Env.adsr(releaseTime: 240), t_trig, doneAction: 0);\r\n       sig = SinOsc.ar(freq, 0, env * level);\r\n       Out.ar(out, sig!2);\r\n}).add;\r\n)\r\n\r\n(\r\nvar knob, active;\r\nactive = false;\r\n\r\n~a = Synth(\\SimpleSynth);\r\n\r\nw = Window(\"Test\",Rect(0, 0, 200, 200), scroll: true);\r\nw.front;\r\n\r\nknob = EZKnob(w, 112@70, 'freq', ControlSpec(20, 20000, step: 1,\r\ndefault: 20, units: 'hz'), { arg knob; ~a.set(\\freq, knob.value); active = false; },\r\nmargin: 39@0);\r\n\r\nMIDIFunc.cc({ arg val, num, chan, src;\r\n    var guiControl, midiControl, treshold;\r\n    midiControl = val.linlin(0, 127, 20, 20000);\r\n    guiControl = knob.value;\r\n    treshold = 500; // adapt this with your needs, it depends on the values of step in ControlSpec and the value of step of your midi controller\r\n    if ( // soft takeover\r\n        (active or: ((midiControl > (guiControl - treshold)) and: (midiControl < (guiControl + treshold)))),\r\n        {\r\n            active = true;\r\n            {\r\n                ~a.set(\\freq, midiControl);\r\n                knob.value_(midiControl);\r\n            }.defer;\r\n        }\r\n    );\r\n    (midiControl).debug(\"Ext Contoller value\");\r\n    (guiControl).debug(\"GUI value\");\r\n}, 21, 0, 0); // cc number 21 on channel 1\r\n)\r\n\r\n~a.set(\\t_trig, 1);",
   "id" : "1-5b2",
   "is_private" : null
}
