Cris_

Arturia Minilab Mk2 Bitwig Controller Script

Sep 20th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Extended version from Nettsu v1.0 - https://github.com/Nettsu/minilab-mkII-bitwig
  2. // by nojoe v1.2, at the moment the controller settings are my test defaults!
  3. // .. Pad 1&2 scroll now between tracks; Pad 5 Overdub toggle; Knob 9 hold for Metronome
  4. //
  5. //
  6. // ToDo:
  7. //
  8. // 1. [Shift + Pad 1/2] scroll between devices ( deviceCursor.selectPrevious & deviceCursor.selectNext )
  9. // 2. Knob1click - Resets macro values. Possible to resets to macro preset defaults then to 0?
  10. // 3. Knob9click - Metronome on/off. Needs a fix: At the moment holding the Knob activates the Click,
  11. //    release the Knob deactivates the Click!
  12. // 4. Knob7 - Song pointer position
  13. //    In Controller Setting: Independent Knob speed for adjusting to fit personal preference
  14. // 5. Knob8 - Panning of the selected track
  15. // 5. Knob15 - Global data up/down like arrow up/down on keyboard?
  16. //    In Controller Setting: Independent Knob speed for adjusting to fit personal preference
  17. // 6. Knob16 - Volume of the selected track
  18. // 7. Pad 9-16 - Delete Note/Control toggle in controller setting and use Pad buttons to switch between Control and Note playing!
  19.  
  20. loadAPI(6);
  21.  
  22. host.defineController("Arturia", "MiniLab MKII", "1.2 beta", "9c891939-9cb5-488d-a447-266f543516f3", "Netsu (TEST by nojoe)");
  23. host.defineMidiPorts(1, 1);
  24. host.addDeviceNameBasedDiscoveryPair(["Arturia MINILAB MKII"], ["Arturia MINILAB MKII"]);
  25.  
  26. STATUS_PAD_ON = 153;
  27. STATUS_PAD_OFF = 137;
  28. STATUS_KNOB = 176;
  29. FIRST_PAD_MIDI = 36;
  30.  
  31. var MACRO_NAMES = [
  32.     "Macro 1",
  33.     "Macro 2",
  34.     "Macro 3",
  35.     "Macro 4",
  36.     "Macro 5",
  37.     "Macro 6",
  38.     "Macro 7",
  39.     "Macro 8"
  40. ];
  41.  
  42. var MACRO_MAP =
  43. {
  44.     "Macro 1":0,
  45.     "Macro 2":1,
  46.     "Macro 3":2,
  47.     "Macro 4":3,
  48.     "Macro 5":4,
  49.     "Macro 6":5,
  50.     "Macro 7":6,
  51.     "Macro 8":7
  52. };
  53.  
  54. var COLOR =
  55. {
  56.     BLACK   :"00",
  57.     RED     :"01",
  58.     BLUE    :"10",
  59.     GREEN   :"04",
  60.     CYAN    :"14",
  61.     PURPLE  :"11",
  62.     YELLOW  :"05",
  63.     WHITE   :"7F"
  64. };
  65.  
  66. var PAD_COLORS =
  67. [
  68.     COLOR.YELLOW,
  69.     COLOR.YELLOW,
  70.     COLOR.PURPLE,
  71.     COLOR.PURPLE,
  72.     COLOR.CYAN,
  73.     COLOR.GREEN,
  74.     COLOR.BLUE,
  75.     COLOR.RED,
  76.     COLOR.YELLOW,
  77.     COLOR.YELLOW,
  78.     COLOR.PURPLE,
  79.     COLOR.PURPLE,
  80.     COLOR.CYAN,
  81.     COLOR.GREEN,
  82.     COLOR.BLUE,
  83.     COLOR.RED,
  84. ];
  85.  
  86. var BOTTOM_FUNC =
  87. {
  88.     CURRENT:0,
  89.     MASTER:1
  90. };
  91.  
  92. var PAD_FUNCTION =
  93. {
  94.     NOTES:0,
  95.     CONTROL:1
  96. };
  97.  
  98.  
  99. var KnobsLeft = [112, 74, 71, 76, 114, 18, 19, 16];
  100. var KnobsRight = [77, 93, 17, 91];
  101.  
  102.  
  103. // RESET MACRO VALUES TO 0 (read ToDo!)
  104. var Knob1click = 113;
  105.  
  106. // METRONOME (read ToDo!)
  107. var Knob9click = 115;
  108.  
  109. // SONG POINTER POSITION (read ToDo!)
  110. var Knob7 = 73;
  111.  
  112. // PAN (read ToDo!)
  113. var Knob8 = 75;
  114.  
  115. // SONG POINTER POSITION (read ToDo!)
  116. var Knob15 = 79;
  117.  
  118. // VOLUME (read ToDo!)
  119. var Knob16 = 72;
  120.  
  121.  
  122. var modWheel        = 1;
  123. var modWheelMacro   = 7;
  124. var macroKnobSpeed  = 1.0;
  125. var rainbowColors   = true;
  126. var padFunction     = PAD_FUNCTION.NOTES;
  127.  
  128. var identityMap     = makeArray(128, -1);
  129. var emptyMap        = makeArray(128, -1);
  130.  
  131. function makeArray(x, init)
  132. {
  133.     var table = new Array(x);
  134.     for (var i = 0; i < x; i++)
  135.     {
  136.         table[i] = init;
  137.     }
  138.     return table;
  139. }
  140.  
  141. function init()
  142. {
  143.     for (var i = 0; i < 128; i++)
  144.         identityMap[i] = i;
  145.    
  146.     // Create the Note Inputs and their Settings
  147.     MiniLabKeys = host.getMidiInPort(0).createNoteInput("MiniLab Keys", "80????", "90????", "B001??", "B002??", "B007??", "B00B??", "B040??", "C0????", "D0????", "E0????");
  148.     MiniLabKeys.setShouldConsumeEvents(false);
  149.     MiniLabPads = host.getMidiInPort(0).createNoteInput("MiniLab Pads", "?9????");
  150.     MiniLabPads.setShouldConsumeEvents(false);
  151.     MiniLabPads.assignPolyphonicAftertouchToExpression(0, NoteExpression.TIMBRE_UP, 2);
  152.  
  153.     host.getMidiInPort(0).setMidiCallback(onMidi);
  154.  
  155.     transport = host.createTransport();
  156.     cTrack = host.createCursorTrack(3, 0);
  157.     uControl = host.createUserControls(8);
  158.     prefs = host.getPreferences();    
  159.  
  160.     primaryDevice = cTrack.getPrimaryDevice();
  161.  
  162.     deviceCursor = cTrack.createCursorDevice();
  163.     controlPageCursor = deviceCursor.createCursorRemoteControlsPage(8);
  164.  
  165.     for (var i = 0; i < 8; i++)
  166.     {
  167.         uControl.getControl(i).setLabel("CC " + KnobsRight[i])
  168.     }
  169.  
  170.     var modWheelSetting = prefs.getEnumSetting("Modwheel macro", "Modwheel", MACRO_NAMES, "Macro 1");
  171.     modWheelSetting.addValueObserver(function (value)
  172.     {
  173.         modWheelMacro = MACRO_MAP[value];
  174.     });
  175.  
  176.     var macroSpeedSetting = prefs.getNumberSetting("Macro knob speed", "Knobs", -10, 10, 0.1, "", 2.0);
  177.     macroSpeedSetting.addRawValueObserver(function(value)
  178.     {
  179.         macroKnobSpeed = value;
  180.     });
  181.  
  182.     var rainbowSetting = prefs.getEnumSetting("Rainbow colors", "Pads", ["ON", "OFF"], "ON");
  183.     rainbowSetting.addValueObserver(function (value)
  184.     {
  185.         rainbowColors = (value == "ON");
  186.         if (rainbowColors == true)
  187.         {
  188.             makeRainbow();
  189.         }
  190.     });
  191.    
  192.     var padSetting = prefs.getEnumSetting("Pad function", "Pads", ["Notes", "Control"], "Control");
  193.     padSetting.addValueObserver(function (value)
  194.     {
  195.         if (value == "Notes")
  196.         {
  197.             padFunction = PAD_FUNCTION.NOTES;
  198.             MiniLabPads.setKeyTranslationTable(identityMap);
  199.         }
  200.         else
  201.         {
  202.             padFunction = PAD_FUNCTION.CONTROL;
  203.             MiniLabPads.setKeyTranslationTable(emptyMap);
  204.         }
  205.     });
  206.  
  207.     setIndications();
  208. }
  209.  
  210. function makeRainbow()
  211. {
  212.     for (var i = 0; i < 16; i++)
  213.     {
  214.         setPadColor(i, PAD_COLORS[i]);
  215.     }
  216. }
  217.  
  218. function MidiData(status, data1, data2)
  219. {
  220.    this.status = status;
  221.    this.data1 = data1;
  222.    this.data2 = data2;
  223. }
  224.  
  225. function onPad(midi)
  226. {
  227.     if (midi.status == STATUS_PAD_ON)
  228.     {
  229.         var padNum = midi.data1 - FIRST_PAD_MIDI;
  230.        
  231.         if (padFunction == PAD_FUNCTION.CONTROL)
  232.         {
  233.             if (padNum == 0)
  234.             {
  235.                 cTrack.selectNext();
  236.             }
  237.             else if (padNum == 1)
  238.             {
  239.                 cTrack.selectPrevious();
  240.             }
  241.             else if (padNum == 2)
  242.             {
  243.                 controlPageCursor.selectPreviousPage(true);
  244.             }
  245.             else if (padNum == 3)
  246.             {
  247.                 controlPageCursor.selectNextPage(true);
  248.             }
  249.             else if (padNum == 4)
  250.             {
  251.                 transport.toggleOverdub();
  252.             }
  253.             else if (padNum == 5)
  254.             {
  255.                 transport.togglePlay();
  256.             }
  257.             else if (padNum == 6)
  258.             {
  259.                 transport.stop();
  260.             }
  261.             else if (padNum == 7)
  262.             {
  263.                 transport.record();
  264.             }
  265.         }
  266.        
  267.         if (rainbowColors == true)
  268.         {
  269.             setPadColor(padNum, COLOR.WHITE);
  270.         }
  271.     }
  272.     else if (midi.status == STATUS_PAD_OFF)
  273.     {
  274.         var padNum = midi.data1 - FIRST_PAD_MIDI;
  275.        
  276.         if (rainbowColors == true)
  277.         {
  278.             setPadColor(padNum, PAD_COLORS[padNum]);
  279.         }
  280.     }
  281. }
  282.  
  283. function onMidi(status, data1, data2)
  284. {
  285.     // Instantiate the MidiData Object for convenience:
  286.     var midi = new MidiData(status, data1, data2);
  287.  
  288.     //println(midi.status + ":" + midi.data1 + ":" + midi.data2);
  289.     if (midi.status == STATUS_PAD_ON || midi.status == STATUS_PAD_OFF)
  290.     {
  291.         onPad(midi);
  292.     }
  293.  
  294.     if (midi.status == STATUS_KNOB)
  295.     {
  296.         var inc = (midi.data2 - 64) * macroKnobSpeed;
  297.  
  298.         for (var i = 0; i < 8; i++)
  299.         {
  300.             if (midi.data1 === KnobsLeft[i])
  301.             {
  302.                 controlPageCursor.getParameter(i).inc(inc, 128);
  303.             }
  304.             else if (midi.data1 === KnobsRight[i])
  305.             {
  306.                 uControl.getControl(i).inc(inc, 128);
  307.             }
  308.         }
  309.  
  310.         if (midi.data1 == Knob1click)
  311.         {
  312.             resetMacros();
  313.         }
  314.         else if (midi.data1 == Knob9click)
  315.         {
  316.             transport.toggleClick();
  317.         }
  318.         else if (midi.data1 == modWheel)
  319.         {
  320.             modWheelFunc(midi);
  321.         }
  322.     }
  323. }
  324.  
  325. function resetMacros()
  326. {
  327.     for (var i = 0; i < 8; i++)
  328.     {
  329.         controlPageCursor.getParameter(i).reset();
  330.     }
  331. }
  332.  
  333. function modWheelFunc(midi)
  334. {
  335.     controlPageCursor.getParameter(modWheelMacro).set(midi.data2, 128);
  336. }
  337.  
  338. function setPadColor(pad, color)
  339. {
  340.     var padHex = (112 + pad).toString(16);
  341.     sendSysex("F0 00 20 6B 7F 42 02 00 10 " + padHex + " " + color + " F7");
  342. }
  343.  
  344. function setIndications()
  345. {
  346.     for (var i = 0; i < 8; i++)
  347.     {
  348.         controlPageCursor.getParameter(i).setIndication(true);
  349.         uControl.getControl(i).setIndication(true);
  350.     }
  351. }
  352.  
  353. function getSign(x)
  354. {
  355.     return typeof x === 'number' ? x ? x < 0 ? -1 : 1 : x === x ? 0 : NaN : NaN;
  356. }
  357.  
  358. function exit()
  359. {
  360. }
Advertisement
Add Comment
Please, Sign In to add comment