SHARE
TWEET

Untitled

a guest Jun 19th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Nathan O'Neel
  3. Chord Player v1.0
  4. 6/8/19
  5. */
  6.  
  7. var activeNotes = [];
  8. var sustainedNotes = [];
  9. var posActiveNotes = [];
  10. var lowestNotePitch = -1;
  11. var chordBasePitch = -1;
  12. var HIGH_RANGE;
  13. var LOW_RANGE;
  14. var CHORD_VELOCITY = 110;
  15. var SEND_ALL_NOTES_ON = true;
  16.  
  17. var isSustained = false;
  18. var lastNoteVelocity = -1;
  19.  
  20. // ex:           C2  C3  G3
  21. var chordNotes = [0, 12, 19];
  22. var chordNoteTranslated = [];
  23. var tempPitch = -1;
  24.  
  25. function chordOn() {   
  26.   chordOff();
  27.     chordBasePitch = lowestNotePitch;
  28.         for(var i = 0; i < chordNotes.length; i++) {
  29.             var noteOn = new NoteOn();
  30.            
  31.             chordNoteTranslated[i] = chordBasePitch + chordNotes[i];
  32.                 if(chordNoteTranslated[i] > HIGH_RANGE) {
  33.                     tempPitch = chordNoteTranslated[i] - 12;
  34.                 while(tempPitch > HIGH_RANGE && !chordNoteTranslated.includes(tempPitch)) {
  35.                   tempPitch -= 12;
  36.                 }
  37.                     chordNoteTranslated[i] = tempPitch;
  38.                 }
  39.             if(chordNoteTranslated[i] < LOW_RANGE) {
  40.                 tempPitch = chordNoteTranslated[i] + 12;
  41.                 while(tempPitch < LOW_RANGE && !chordNoteTranslated.includes(tempPitch)) {
  42.                     tempPitch += 12;
  43.                 }
  44.                 chordNoteTranslated[i] = tempPitch;
  45.             }
  46.        
  47.             noteOn.pitch = chordNoteTranslated[i];
  48.             if(CHORD_VELOCITY == 0) {
  49.                 noteOn.velocity = lastNoteVelocity
  50.             } else {
  51.                 noteOn.velocity = CHORD_VELOCITY;
  52.             }
  53.             noteOn.send();
  54.         }
  55. }
  56. function chordOff() {
  57.     for(var i = 0; i < chordNotes.length; i++) {
  58.       var noteOff = new NoteOff();
  59.         noteOff.pitch = chordNoteTranslated[i]
  60.         //noteOff.velocity = CHORD_VELOCITY;
  61.         noteOff.send();
  62.     }
  63. }
  64.  
  65.  
  66. function getLowestNoteFromActive() {
  67.     lowestNotePitch = 127; // The max possible
  68.     var susLength = sustainedNotes.length;
  69.     var actLength = activeNotes.length;
  70.    
  71.     if(actLength == 0 && susLength == 0) {
  72.         lowestNotePitch = -1;
  73.         chordOff();
  74.         return;
  75.     }
  76.     var amount = Math.max(actLength, susLength);
  77.     for(var i = 0; i < amount; i++) {
  78.         if(i < actLength) {
  79.             if(activeNotes[i] < lowestNotePitch) {
  80.               lowestNotePitch = activeNotes[i];
  81.             }
  82.         }
  83.         if(i < susLength) {
  84.             if(sustainedNotes[i] < lowestNotePitch) {
  85.                 lowestNotePitch = sustainedNotes[i];
  86.             }
  87.         }
  88.     }
  89.    
  90.     // if I know that there is a note on, and if its not the same note as before
  91.     if(lowestNotePitch != -1 && lowestNotePitch != chordBasePitch) {
  92.         chordOn();
  93.     }
  94. }
  95.  
  96. function HandleMIDI (event) {  
  97.         if (event instanceof NoteOn) {
  98.         lastNoteVelocity = event.velocity;
  99.         if(isSustained) {
  100.             if(sustainedNotes.indexOf(event.pitch) == -1) {
  101.                 sustainedNotes.push(event.pitch);
  102.             }
  103.         } else {
  104.             if(activeNotes.indexOf(event.pitch) == -1) {
  105.                 activeNotes.push(event.pitch);
  106.             }
  107.         }
  108.         if(posActiveNotes.indexOf(event.pitch) == -1 ) posActiveNotes.push(event.pitch);
  109.         if(event.pitch < lowestNotePitch || lowestNotePitch == -1) {
  110.             lowestNotePitch = event.pitch;
  111.             chordOn();
  112.         }
  113.         if(SEND_ALL_NOTES_ON) {
  114.             event.send();
  115.         }
  116.        
  117.         } else if (event instanceof NoteOff) {
  118.           var indexOfNote = activeNotes.indexOf(event.pitch);
  119.           if(indexOfNote != -1) {
  120.               activeNotes.splice(indexOfNote, 1);
  121.           }
  122.             var indexOfPossible = posActiveNotes.indexOf(event.pitch);
  123.             if(indexOfPossible != -1) {
  124.                     posActiveNotes.splice(indexOfPossible, 1);
  125.             }
  126.           if(event.pitch == lowestNotePitch) {
  127.               getLowestNoteFromActive();
  128.           }
  129.             event.send();
  130.         } else if(event.number == 64) {
  131.           if(event.value == 127) {
  132.               isSustained = true;
  133.               sustainedNotes = sustainedNotes.concat(activeNotes);
  134.               activeNotes = posActiveNotes;
  135.           } else if(event.value == 0) {
  136.               isSustained = false;
  137.               sustainedNotes = [];
  138.               activeNotes = posActiveNotes;
  139.               getLowestNoteFromActive();
  140.           }
  141.         event.send();
  142.     } else {
  143.             event.send();
  144.     }
  145. }
  146.  
  147. function ParameterChanged (param, value) {
  148.         switch (param) {
  149.                 case 0:
  150.                     HIGH_RANGE = value;
  151.                     if (value < LOW_RANGE) {
  152.                             SetParameter(1, value);
  153.                     }
  154.                     break;
  155.                 case 1:
  156.                     LOW_RANGE = value;
  157.                     if (value > HIGH_RANGE) {
  158.                             SetParameter(0, value);
  159.                     }
  160.                     break;
  161.                 case 2:
  162.                     CHORD_VELOCITY = value;
  163.                     break;
  164.                 case 3:
  165.                     SEND_ALL_NOTES_ON = (value === 0);             
  166.                 break;
  167.                     default:
  168.                         //nothing
  169.         }
  170.    
  171. }
  172.  
  173. var PluginParameters = [{
  174.         name:"High Range",
  175.         type:"linear",
  176.         minValue:0,
  177.         maxValue:127,
  178.         numberOfSteps:127,
  179.         defaultValue:72
  180. }, {
  181.         name:"Low Range",
  182.         type:"linear",
  183.         minValue:0,
  184.         maxValue:127,
  185.         numberOfSteps:127,
  186.         defaultValue:48
  187. }, {
  188.     type:"text",
  189.     name:"When zero, velocity = last note's velocity."
  190. }, {
  191.         name:"Chord Velocity",
  192.         type:"linear",
  193.         minValue:0,
  194.         maxValue:127,
  195.         numberOfSteps:127,
  196.         defaultValue:0
  197. }, {
  198.     name:"Send All Notes On",
  199.         type:"menu",
  200.         valueStrings:["On", "Off"],
  201.         defaultValue:1,
  202.         numberOfSteps:2
  203. }];
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top