Skip to content

Instantly share code, notes, and snippets.

@borisisok
Created December 10, 2018 01:14
Show Gist options
  • Save borisisok/41f32cdec1c28c34088922b61070c9fd to your computer and use it in GitHub Desktop.
Save borisisok/41f32cdec1c28c34088922b61070c9fd to your computer and use it in GitHub Desktop.
bitwig / custom controller scripts
loadAPI(7);
host.defineController("Korg", "nanoKONTROL", "1.22", "E74ABCE1-7BA8-4526-A769-25A7E1F8212F");
host.defineMidiPorts(1, 1);
var ECHO_ID = "12";
host.addDeviceNameBasedDiscoveryPair(["nanoKONTROL"], ["nanoKONTROL"]);
var SYSEX_HEADER = "F0 42 40 00 01 04 00";
var CC =
{
LOOP : 49,
STOP : 46,
PLAY : 45,
REC : 44,
REW : 47,
FF : 48,
SLIDER1 : 1,
SLIDER9 : 9,
KNOB1 : 11,
KNOB9 : 19,
UPPER_BUTTON1 : 21,
UPPER_BUTTON9 : 29,
LOWER_BUTTON1 : 31,
LOWER_BUTTON9 : 39,
BUTTON_LEFT : 47,
BUTTON_RIGHT : 48,
BUTTON_PLAY : 45,
TOGGLE_VIEW : 31,
TOGGLE_MODE_PAGE : 41
};
var isPlay = false;
var stripnum = 9;
var stripact = 0;
var stripmax = 6;
var stripmin = 0;
function scheduleTask (f, params, delay)
{
host.scheduleTask (f, params, delay);
}
function doObject (object, f)
{
return function ()
{
f.apply (object, arguments);
};
}
function TimerTask (interval)
{
this.interval = interval;
this.args = [];
this.isRunning = false;
}
TimerTask.prototype.start = function (scope, callback, args)
{
this.scope = scope;
this.callback = callback;
this.args = args;
this.isRunning = true;
this._timer ();
};
TimerTask.prototype.stop = function ()
{
this.args = [];
this.isRunning = false;
};
TimerTask.prototype._timer = function ()
{
if (!this.isRunning)
return;
this.callback.apply (this.scope, this.args);
scheduleTask (doObject (this, this._timer), this.args, this.interval);
};
var tt = new TimerTask (30);
function init()
{
host.getMidiInPort(0).setMidiCallback(onMidi);
trackBank = host.createMainTrackBank(64, 1, 0);
println(trackBank.getSizeOfBank())
//sendSysex(SYSEX_HEADER + "00 00 01 F7"); // Enter native mode
}
function exit()
{
//sendSysex(SYSEX_HEADER + "00 00 00 F7"); // Leave native mode
}
function onMidi(status, data1, data2)
{
var cc = data1;
var val = data2;
printMidi(status, cc, val);
if (status == 176)
{
if (withinRange(data1, CC.SLIDER1, CC.SLIDER9))
{
var index = data1 - CC.SLIDER1;
tracknum = index + stripact;
println("track vol:" + index + " " + tracknum );
if ( trackBank.getTrack(tracknum) != null) {
trackBank.getTrack(tracknum).volume().set(data2, 128);
}
}
else if (withinRange(data1, CC.KNOB1, CC.KNOB9))
{
var index = data1 - CC.KNOB1;
tracknum = index + stripact;
println("track pan:" + index + " " + tracknum );
if (trackBank.getTrack(tracknum) != null ) {
trackBank.getTrack(tracknum).pan().set(data2, 128);
}
}
if (val > 0) // deal with button presses here
{
if (data1 == CC.BUTTON_LEFT) {
tt.stop ();
println("button left");
if (stripact > stripmin) {
stripact -= stripnum;
println("stripact: " + stripact);
}
}
else if (data1 == CC.BUTTON_RIGHT) {
tt.start (this,
function()
{
host.getMidiOutPort(0).sendMidi(176,21,127);
host.getMidiOutPort(0).sendMidi(21,127,127);
println("time meth");
},
[]);
println("button right");
if (stripact < (stripnum * stripmax)) {
stripact += stripnum;
println("stripact: " + stripact);
}
}
}
}
// if (val > 0) // deal with button presses here
// {
// if (withinRange(data1, CC.UPPER_BUTTON1, CC.UPPER_BUTTON9))
// {
// var index = data1 - CC.UPPER_BUTTON1;
// // trackBank.getTrack(index).getMute().toggle();
// }
// else if (withinRange(data1, CC.LOWER_BUTTON1, CC.LOWER_BUTTON9))
// {
// var index = data1 - CC.LOWER_BUTTON1;
// // trackBank.getTrack(index).getArm().toggle();
// }
}
function onSysex(data)
{
printSysex(data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment