Advertisement
Guest User

Untitled

a guest
Aug 25th, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.85 KB | None | 0 0
  1. loadAPI(1);
  2.  
  3. host.defineController("Cakewalk", "A-300 PRO", "1.0", "e504e660-2b27-11e4-8c21-0800200c9a66");
  4. host.defineMidiPorts(1, 1);
  5. host.addDeviceNameBasedDiscoveryPair(["A-PRO 1"], ["A-PRO"]);
  6.  
  7. // CC 0 and CCs 120+ are reserved
  8. var LOWEST_CC = 1;
  9. var HIGHEST_CC = 119;
  10.  
  11. // Two array-variables to hold the values of all the CCs and to check if they have changed
  12. var ccValue = initArray(0, ((HIGHEST_CC - LOWEST_CC + 1)*16));
  13. var ccValueOld = initArray(0, ((HIGHEST_CC - LOWEST_CC + 1)*16));
  14.  
  15. var EDIT_MODE =
  16. {
  17. SELECTED_TRACK : 0,
  18. MIXER : 1
  19. };
  20.  
  21.  
  22. var CONTROL =
  23. {
  24. REW : 22, // <<
  25. FWD : 23, // >>
  26. STOP : 25, // [ ]
  27. PLAY : 26, // [>]
  28. REC : 28 // [R]
  29.  
  30. };
  31.  
  32. var isPlay = false;
  33. var callinc = 0;
  34.  
  35.  
  36.  
  37. // A function to create an indexed function for the Observers
  38. function getValueObserverFunc(index, varToStore)
  39. {
  40. return function(value)
  41. {
  42. varToStore[index] = value;
  43. }
  44. }
  45.  
  46. function init()
  47. {
  48. initMidi();
  49. initPrimaryDeviceMacro();
  50. initControls();
  51.  
  52. transport = host.createTransport();
  53.  
  54. // Create 16 NoteInputs + Omni.
  55. // Verbose to allow commenting out unneeded channels
  56. // To do so, put "//" in front of the lines containing channels you don't want to use
  57. // Be sure to do it for the "createNoteInput" lines as well as the corresponding
  58. // "setShouldConsumeEvents" and "assignPolyphonicAftertouchToExpression" lines below
  59. MultiBi = host.getMidiInPort(0).createNoteInput("MultiBi - Omni", "??????");
  60. MultiBi1 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 1", "?0????");
  61. MultiBi2 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 2", "?1????");
  62. MultiBi3 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 3", "?2????");
  63. MultiBi4 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 4", "?3????");
  64. MultiBi5 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 5", "?4????");
  65. MultiBi6 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 6", "?5????");
  66. MultiBi7 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 7", "?6????");
  67. MultiBi8 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 8", "?7????");
  68. MultiBi9 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 9", "?8????");
  69. MultiBi10 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 10", "?9????");
  70. MultiBi11 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 11", "?A????");
  71. MultiBi12 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 12", "?B????");
  72. MultiBi13 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 13", "?C????");
  73. MultiBi14 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 14", "?D????");
  74. MultiBi15 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 15", "?E????");
  75. MultiBi16 = host.getMidiInPort(0).createNoteInput("MultiBi - Ch 16", "?F????");
  76.  
  77. // Disable the consuming of events by the NoteInputs, so they are also available for mapping
  78. MultiBi.setShouldConsumeEvents(false);
  79. MultiBi1.setShouldConsumeEvents(false);
  80. MultiBi2.setShouldConsumeEvents(false);
  81. MultiBi3.setShouldConsumeEvents(false);
  82. MultiBi4.setShouldConsumeEvents(false);
  83. MultiBi5.setShouldConsumeEvents(false);
  84. MultiBi6.setShouldConsumeEvents(false);
  85. MultiBi7.setShouldConsumeEvents(false);
  86. MultiBi8.setShouldConsumeEvents(false);
  87. MultiBi9.setShouldConsumeEvents(false);
  88. MultiBi10.setShouldConsumeEvents(false);
  89. MultiBi11.setShouldConsumeEvents(false);
  90. MultiBi12.setShouldConsumeEvents(false);
  91. MultiBi13.setShouldConsumeEvents(false);
  92. MultiBi14.setShouldConsumeEvents(false);
  93. MultiBi15.setShouldConsumeEvents(false);
  94. MultiBi16.setShouldConsumeEvents(false);
  95.  
  96. // Enable Poly AT translation into Timbre for the internal BWS instruments
  97. MultiBi.assignPolyphonicAftertouchToExpression(0, NoteExpression.TIMBRE_UP, 5);
  98. MultiBi1.assignPolyphonicAftertouchToExpression(0, NoteExpression.TIMBRE_UP, 5);
  99. MultiBi2.assignPolyphonicAftertouchToExpression(1, NoteExpression.TIMBRE_UP, 5);
  100. MultiBi3.assignPolyphonicAftertouchToExpression(2, NoteExpression.TIMBRE_UP, 5);
  101. MultiBi4.assignPolyphonicAftertouchToExpression(3, NoteExpression.TIMBRE_UP, 5);
  102. MultiBi5.assignPolyphonicAftertouchToExpression(4, NoteExpression.TIMBRE_UP, 5);
  103. MultiBi6.assignPolyphonicAftertouchToExpression(5, NoteExpression.TIMBRE_UP, 5);
  104. MultiBi7.assignPolyphonicAftertouchToExpression(6, NoteExpression.TIMBRE_UP, 5);
  105. MultiBi8.assignPolyphonicAftertouchToExpression(7, NoteExpression.TIMBRE_UP, 5);
  106. MultiBi9.assignPolyphonicAftertouchToExpression(8, NoteExpression.TIMBRE_UP, 5);
  107. MultiBi10.assignPolyphonicAftertouchToExpression(9, NoteExpression.TIMBRE_UP, 5);
  108. MultiBi11.assignPolyphonicAftertouchToExpression(10, NoteExpression.TIMBRE_UP, 5);
  109. MultiBi12.assignPolyphonicAftertouchToExpression(11, NoteExpression.TIMBRE_UP, 5);
  110. MultiBi13.assignPolyphonicAftertouchToExpression(12, NoteExpression.TIMBRE_UP, 5);
  111. MultiBi14.assignPolyphonicAftertouchToExpression(13, NoteExpression.TIMBRE_UP, 5);
  112. MultiBi15.assignPolyphonicAftertouchToExpression(14, NoteExpression.TIMBRE_UP, 5);
  113. MultiBi16.assignPolyphonicAftertouchToExpression(15, NoteExpression.TIMBRE_UP, 5);
  114.  
  115. // Enable Midi Beat Clock. Comment out if you don't want that
  116. host.getMidiOutPort(0).setShouldSendMidiBeatClock;
  117.  
  118. // Setting Callbacks for Midi and Sysex
  119. host.getMidiInPort(0).setMidiCallback(onMidi);
  120. host.getMidiInPort(0).setSysexCallback(onSysex);
  121.  
  122. // Make CCs 1-119 freely mappable for all 16 Channels
  123. userControls = host.createUserControls((HIGHEST_CC - LOWEST_CC + 1)*16);
  124.  
  125. for(var i=LOWEST_CC; i<=HIGHEST_CC; i++)
  126. {
  127. for (var j=1; j<=16; j++) {
  128. // Create the index variable c
  129. var c = i - LOWEST_CC + (j-1) * (HIGHEST_CC-LOWEST_CC+1);
  130. // Set a label/name for each userControl
  131. userControls.getControl(c).setLabel("CC " + i + " - Channel " + j);
  132. // Add a ValueObserver for each userControl
  133. userControls.getControl(c).addValueObserver(127, getValueObserverFunc(c, ccValue));
  134. }
  135. }
  136. }
  137.  
  138. // Updates the controller in an orderly manner when needed
  139. // so that LEDs, Motors etc. react to changes in the Software
  140. // without drowning the Controller with data
  141. function flush()
  142. {
  143. for(var i=LOWEST_CC; i<=HIGHEST_CC; i++)
  144. {
  145. for (var j=1; j<=16; j++) {
  146. var c = i - LOWEST_CC + (j-1) * (HIGHEST_CC-LOWEST_CC+1);
  147. // Check if something has changed
  148. if (ccValue[c] != ccValueOld[c]) {
  149. // If yes, send the updated value
  150. sendChannelController(j-1, i, ccValue[c]);
  151. // And update the value for the next check
  152. ccValueOld[c] = ccValue[c];
  153. }
  154. }
  155. }
  156. }
  157.  
  158.  
  159.  
  160. function initMidi()
  161. {
  162. host.getMidiInPort(0).createNoteInput("A-Pro 1");
  163. // host.getMidiInPort(0).setMidiCallback(onMidi);
  164. // host.getMidiInPort(0).setSysexCallback(onSysex);
  165. }
  166. function initControls()
  167. {
  168. trackBank = host.createTrackBank(8, 1, 99);
  169. transport = host.createTransport();
  170.  
  171. // PLAY
  172. transport.addIsPlayingObserver(
  173. function(on)
  174. {
  175. isPlay = on;
  176. sendNoteOn(0, CONTROL.PLAY, on ? 127 : 0);
  177. sendNoteOn(0, CONTROL.STOP, on ? 0 : 127);
  178. });
  179.  
  180. // REC
  181. transport.addIsRecordingObserver(
  182. function(on)
  183. {
  184. sendNoteOn(0, CONTROL.RECORD, on ? 127 : 0);
  185. });
  186. }
  187.  
  188. function MidiChannel(status)
  189. {
  190. var c = status - 175;
  191. return (c > 0 && c < 17) ? c : 0;
  192. }
  193.  
  194.  
  195. function exit()
  196. {
  197. // nothing to do here ;-)
  198. }
  199.  
  200. function initPrimaryDeviceMacro()
  201. {
  202. // Setup Primary Instrument Macro Controls
  203. cursorDevice = host.createCursorDevice();
  204. cursorTrack = host.createCursorTrack(8, 0);
  205. primaryInstrument = cursorTrack.getPrimaryInstrument();
  206. for ( var i = 0; i < 8; i++)
  207. {
  208. var p = primaryInstrument.getMacro(i).getAmount();
  209. p.setIndication(true);
  210. }
  211. }
  212.  
  213.  
  214. // Update the UserControls when Midi Data is received
  215. function onMidi(status, data1, data2)
  216. {
  217.  
  218. printMidi(status, data1, data2);
  219.  
  220. callinc ++;
  221.  
  222. if (!isChannelController(status)) return;
  223.  
  224. var chan = MidiChannel(status);
  225. var editMode = (chan < 9) ? EDIT_MODE.SELECTED_TRACK : EDIT_MODE.MIXER;
  226. var cIndex = (chan - 1) % 8;
  227.  
  228. //logf("MIDI : status {0} --- chan {1} --- CC {2} --- value {3}", status, chan, data1, data2);
  229.  
  230. if (transportButtons(data1, data2)) return;
  231.  
  232. switch (editMode)
  233. {
  234. case EDIT_MODE.SELECTED_TRACK:
  235. controlSelectedTrack(cIndex, data1, data2);
  236. break;
  237.  
  238. case EDIT_MODE.MIXER:
  239. controlMixer(cIndex, data1, data2);
  240. break;
  241. }
  242.  
  243. if (data1 >= LOWEST_CC && data1 <= HIGHEST_CC)
  244. {
  245. var index = data1 - LOWEST_CC + ((HIGHEST_CC-LOWEST_CC+1) * MIDIChannel(status));
  246. userControls.getControl(index).set(data2, 128);
  247. }
  248.  
  249.  
  250.  
  251. function transportButtons(midiCC, value)
  252. {
  253. var buttonPressed = true;
  254.  
  255. switch (midiCC)
  256. {
  257. // TRANSPORT
  258. case CONTROL.PLAY:
  259. transport.play();
  260. break;
  261.  
  262. case CONTROL.STOP:
  263. transport.stop();
  264. break;
  265.  
  266. case CONTROL.REC:
  267. transport.record();
  268. break;
  269.  
  270. case CONTROL.REW:
  271. cursorTrack.selectPrevious();
  272. break;
  273.  
  274. case CONTROL.FWD:
  275. cursorTrack.selectNext();
  276. break;
  277.  
  278. default:
  279. buttonPressed = false;
  280. break;
  281. }
  282.  
  283. return buttonPressed;
  284. }
  285.  
  286. }
  287. function onSysex(data)
  288. {
  289. //printSysex(data);
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement