home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Aplikacje_64-bitowe / Mixxx / mixxx-1.9.0-win64.exe / midi / Numark-MixTrack-scripts.js < prev    next >
Text File  |  2010-12-04  |  8KB  |  251 lines

  1. // Numark Mixtrack Mapping Script Functions
  2. // 1/11/2010 - v0.1
  3. // Matteo <matteo@magm3.com>
  4. function NumarkMixTrack() {}
  5.  
  6. NumarkMixTrack.init = function(id) {    // called when the MIDI device is opened & set up
  7.     NumarkMixTrack.id = id;    // Store the ID of this device for later use
  8.     
  9.     NumarkMixTrack.directoryMode = false;
  10.     
  11.     NumarkMixTrack.scratchMode = [false, false];
  12.     NumarkMixTrack.scratchTimer = [-1, -1];
  13.     
  14.     NumarkMixTrack.manualLooping = [false, false];
  15.     
  16.     NumarkMixTrack.leds = [
  17.         // Common
  18.         { "directory": 0x73, "file": 0x72 },
  19.         // Deck 1
  20.         { "rate": 0x70, "scratchMode": 0x48, "manualLoop": 0x61, "loopIn": 0x53, "loopOut": 0x54, "reLoop": 0x55 },
  21.         // Deck 2
  22.         { "rate": 0x71, "scratchMode": 0x50, "manualLoop": 0x62, "loopIn": 0x56, "loopOut": 0x57, "reLoop": 0x58 }
  23.     ];
  24.     NumarkMixTrack.setLED(NumarkMixTrack.leds[0]["file"], true);
  25. }
  26.  
  27. NumarkMixTrack.shutdown = function(id) {    // called when the MIDI device is closed
  28.     var lowestLED = 0x30;
  29.     var highestLED = 0x73;
  30.     for (var i=lowestLED; i<=highestLED; i++) {
  31.         NumarkMixTrack.setLED(i, false);    // Turn off all the lights
  32.     }
  33. }
  34.  
  35. NumarkMixTrack.groupToDeck = function(group) {
  36.     var matches = group.match(/^\[Channel(\d+)\]$/);
  37.     if (matches == null) {
  38.         return -1;
  39.     } else {
  40.         return matches[1];
  41.     }
  42. }
  43.  
  44. NumarkMixTrack.samplesPerBeat = function(group) {
  45.     // FIXME: Get correct samplerate and channels for current deck
  46.     var sampleRate = 44100;
  47.     var channels = 2;
  48.     var bpm = engine.getValue(group, "file_bpm");
  49.     return channels * sampleRate * 60 / bpm;
  50. }
  51.  
  52. NumarkMixTrack.setLED = function(value, status) {
  53.     if (status) {
  54.         status = 0x64;
  55.     } else {
  56.         status = 0x00;
  57.     }
  58.     midi.sendShortMsg(0x90, value, status);
  59. }
  60.  
  61. NumarkMixTrack.selectKnob = function(channel, control, value, status, group) {
  62.     if (value > 63) {
  63.         value = value - 128;
  64.     }
  65.     if (NumarkMixTrack.directoryMode) {
  66.         if (value > 0) {
  67.             for (var i = 0; i < value; i++) {
  68.                 engine.setValue(group, "SelectNextPlaylist", 1);
  69.             }
  70.         } else {
  71.             for (var i = 0; i < -value; i++) {
  72.                 engine.setValue(group, "SelectPrevPlaylist", 1);
  73.             }
  74.         }
  75.     } else {
  76.         engine.setValue(group, "SelectTrackKnob", value);
  77.     }
  78. }
  79.  
  80. NumarkMixTrack.loopIn = function(channel, control, value, status, group) {
  81.         var deck = NumarkMixTrack.groupToDeck(group);
  82.     if (value) {
  83.         if(NumarkMixTrack.manualLooping[deck-1]) {
  84.                 // Cut loop to Half
  85.                 var start = engine.getValue(group, "loop_start_position");
  86.                 var end = engine.getValue(group, "loop_end_position");
  87.                 if((start != -1) && (end != -1)) {
  88.                     var len = (end - start) / 2;
  89.                     engine.setValue(group, "loop_end_position", start + len);
  90.                 }
  91.         } else {
  92.             if (engine.getValue(group, "loop_enabled")) {
  93.                 engine.setValue(group, "reloop_exit", 1);
  94.                 NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["reLoop"],false);
  95.             }
  96.             engine.setValue(group, "loop_in", 1);
  97.             engine.setValue(group, "loop_end_position", -1);
  98.             NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["loopIn"],true);
  99.             NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["loopOut"],false);
  100.         }
  101.     }
  102. }
  103.  
  104. NumarkMixTrack.loopOut = function(channel, control, value, status, group) {
  105.         var deck = NumarkMixTrack.groupToDeck(group);
  106.     if (value) {
  107.         var start = engine.getValue(group, "loop_start_position");
  108.         var end = engine.getValue(group, "loop_end_position");
  109.         if(NumarkMixTrack.manualLooping[deck-1]) {
  110.             // Set loop to current Bar (very approximative and would need to get fixed !!!)
  111.             var bar = NumarkMixTrack.samplesPerBeat(group);
  112.             engine.setValue(group,"loop_in",1);
  113.             var start = engine.getValue(group, "loop_start_position");
  114.             engine.setValue(group,"loop_end_position", start + bar);
  115.             NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["loopIn"],true);
  116.                         NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["loopOut"],true);
  117.         } else {
  118.             if (start != -1) {
  119.                 if (end != -1) {
  120.                     // Loop In and Out set -> call Reloop/Exit
  121.                     engine.setValue(group, "reloop_exit", 1);
  122.                     NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["loopIn"],true);
  123.                     NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["loopOut"],true);
  124.                     NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["reLoop"],true);
  125.                 } else {
  126.                     // Loop In set -> call Loop Out
  127.                     engine.setValue(group, "loop_out", 1);
  128.                     NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["loopOut"],true);
  129.                 }
  130.             }
  131.         }
  132.     }
  133. }
  134.  
  135. NumarkMixTrack.reLoop = function(channel, control, value, status, group) {
  136.         var deck = NumarkMixTrack.groupToDeck(group);
  137.     if (value) {
  138.         if(NumarkMixTrack.manualLooping[deck-1]) {
  139.                 // Multiply Loop by Two 
  140.                 var start = engine.getValue(group, "loop_start_position");
  141.                 var end = engine.getValue(group, "loop_end_position");
  142.                 if((start != -1) && (end != -1)) {
  143.                     var len = (end - start) * 2;
  144.                     engine.setValue(group, "loop_end_position", start + len);
  145.                 }
  146.         } else {
  147.             if (engine.getValue(group, "loop_enabled")) {
  148.                 NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["reLoop"],false);
  149.             } else {
  150.                 var start = engine.getValue(group, "loop_start_position");
  151.                 var end = engine.getValue(group, "loop_end_position");
  152.                 if( (start != -1) && (end != -1)) {
  153.                     // Loop is set ! Light the led
  154.                     NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["reLoop"],true);
  155.                 }
  156.             }
  157.             engine.setValue(group, "reloop_exit", 1);
  158.         }
  159.     }
  160. }
  161.  
  162.  
  163. // If playing, stutters from cuepoint; otherwise jumps to cuepoint and stops
  164. NumarkMixTrack.playFromCue = function(channel, control, value, status, group) {
  165.     if (value) {
  166.         if (engine.getValue(group, "play")) {
  167.             engine.setValue(group, "play", 0);
  168.             engine.setValue(group, "cue_gotoandstop", 1);
  169.         } else {
  170.             engine.setValue(group, "cue_preview", 1);
  171.         }
  172.     } else {
  173.         engine.setValue(group, "cue_preview", 0);
  174.     }
  175. }
  176.  
  177. NumarkMixTrack.jogWheel = function(channel, control, value, status, group) {
  178.     var deck = NumarkMixTrack.groupToDeck(group);
  179.     var adjustedJog = parseFloat(value);
  180.     var posNeg = 1;
  181.     if (adjustedJog > 63) {    // Counter-clockwise
  182.         posNeg = -1;
  183.         adjustedJog = value - 128;
  184.     }
  185.     
  186.         if (NumarkMixTrack.scratchMode[deck-1]) {
  187.         if (NumarkMixTrack.scratchTimer[deck-1] == -1) {
  188.             engine.scratchEnable(deck, 128, 33+1/3, 1.0/8, (1.0/8)/32);
  189.         } else {
  190.             engine.stopTimer(NumarkMixTrack.scratchTimer[deck-1]);
  191.         }
  192.         engine.scratchTick(deck, adjustedJog);
  193.         NumarkMixTrack.scratchTimer[deck-1] = engine.beginTimer(20, "NumarkMixTrack.jogWheelStopScratch(" + deck + ")", true);
  194.     } else {
  195.         var gammaInputRange = 23;    // Max jog speed
  196.         var maxOutFraction = 0.5;    // Where on the curve it should peak; 0.5 is half-way
  197.         var sensitivity = 0.5;        // Adjustment gamma
  198.         var gammaOutputRange = 3;    // Max rate change
  199.         if (engine.getValue(group,"play")) {
  200.             adjustedJog = posNeg * gammaOutputRange * Math.pow(Math.abs(adjustedJog) / (gammaInputRange * maxOutFraction), sensitivity);
  201.         } else {
  202.             adjustedJog = gammaOutputRange * adjustedJog / (gammaInputRange * maxOutFraction);
  203.         }
  204.         engine.setValue(group, "jog", adjustedJog);
  205.     }
  206. }
  207.  
  208. NumarkMixTrack.jogWheelStopScratch = function(deck) {
  209.     NumarkMixTrack.scratchTimer[deck-1] = -1;
  210.     engine.scratchDisable(deck);
  211. }
  212.  
  213. NumarkMixTrack.toggleDirectoryMode = function(channel, control, value, status, group) {
  214.     // Toggle setting and light
  215.     if (value) {
  216.         if (NumarkMixTrack.directoryMode) {
  217.             NumarkMixTrack.directoryMode = false;
  218.         } else {
  219.             NumarkMixTrack.directoryMode = true;
  220.         }
  221.         NumarkMixTrack.setLED(NumarkMixTrack.leds[0]["directory"], NumarkMixTrack.directoryMode);
  222.         NumarkMixTrack.setLED(NumarkMixTrack.leds[0]["file"], !NumarkMixTrack.directoryMode);
  223.     }
  224. }
  225.  
  226. NumarkMixTrack.toggleScratchMode = function(channel, control, value, status, group) {
  227.     var deck = NumarkMixTrack.groupToDeck(group);
  228.     // Toggle setting and light
  229.     if (value) {
  230.         if (NumarkMixTrack.scratchMode[deck-1]) {
  231.             NumarkMixTrack.scratchMode[deck-1] = false;
  232.         } else {
  233.             NumarkMixTrack.scratchMode[deck-1] = true;
  234.         }
  235.         NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["scratchMode"], NumarkMixTrack.scratchMode[deck-1]);
  236.     }
  237. }
  238.  
  239. NumarkMixTrack.toggleManualLooping = function(channel, control, value, status, group) {
  240.     var deck = NumarkMixTrack.groupToDeck(group);
  241.     // Toggle setting and light
  242.     if (value) {
  243.         if (NumarkMixTrack.manualLooping[deck-1]) {
  244.             NumarkMixTrack.manualLooping[deck-1] = false;
  245.         } else {
  246.             NumarkMixTrack.manualLooping[deck-1] = true;
  247.         }
  248.         NumarkMixTrack.setLED(NumarkMixTrack.leds[deck]["manualLoop"], NumarkMixTrack.manualLooping[deck-1]);
  249.     }
  250. }
  251.