Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Nathan O'Neel
- Chord Player v1.0
- 6/8/19
- */
- var activeNotes = [];
- var sustainedNotes = [];
- var posActiveNotes = [];
- var lowestNotePitch = -1;
- var chordBasePitch = -1;
- var HIGH_RANGE;
- var LOW_RANGE;
- var CHORD_VELOCITY = 110;
- var SEND_ALL_NOTES_ON = true;
- var isSustained = false;
- var lastNoteVelocity = -1;
- // ex: C2 C3 G3
- var chordNotes = [0, 12, 19];
- var chordNoteTranslated = [];
- var tempPitch = -1;
- function chordOn() {
- chordOff();
- chordBasePitch = lowestNotePitch;
- for(var i = 0; i < chordNotes.length; i++) {
- var noteOn = new NoteOn();
- chordNoteTranslated[i] = chordBasePitch + chordNotes[i];
- if(chordNoteTranslated[i] > HIGH_RANGE) {
- tempPitch = chordNoteTranslated[i] - 12;
- while(tempPitch > HIGH_RANGE && !chordNoteTranslated.includes(tempPitch)) {
- tempPitch -= 12;
- }
- chordNoteTranslated[i] = tempPitch;
- }
- if(chordNoteTranslated[i] < LOW_RANGE) {
- tempPitch = chordNoteTranslated[i] + 12;
- while(tempPitch < LOW_RANGE && !chordNoteTranslated.includes(tempPitch)) {
- tempPitch += 12;
- }
- chordNoteTranslated[i] = tempPitch;
- }
- noteOn.pitch = chordNoteTranslated[i];
- if(CHORD_VELOCITY == 0) {
- noteOn.velocity = lastNoteVelocity
- } else {
- noteOn.velocity = CHORD_VELOCITY;
- }
- noteOn.send();
- }
- }
- function chordOff() {
- for(var i = 0; i < chordNotes.length; i++) {
- var noteOff = new NoteOff();
- noteOff.pitch = chordNoteTranslated[i]
- //noteOff.velocity = CHORD_VELOCITY;
- noteOff.send();
- }
- }
- function getLowestNoteFromActive() {
- lowestNotePitch = 127; // The max possible
- var susLength = sustainedNotes.length;
- var actLength = activeNotes.length;
- if(actLength == 0 && susLength == 0) {
- lowestNotePitch = -1;
- chordOff();
- return;
- }
- var amount = Math.max(actLength, susLength);
- for(var i = 0; i < amount; i++) {
- if(i < actLength) {
- if(activeNotes[i] < lowestNotePitch) {
- lowestNotePitch = activeNotes[i];
- }
- }
- if(i < susLength) {
- if(sustainedNotes[i] < lowestNotePitch) {
- lowestNotePitch = sustainedNotes[i];
- }
- }
- }
- // if I know that there is a note on, and if its not the same note as before
- if(lowestNotePitch != -1 && lowestNotePitch != chordBasePitch) {
- chordOn();
- }
- }
- function HandleMIDI (event) {
- if (event instanceof NoteOn) {
- lastNoteVelocity = event.velocity;
- if(isSustained) {
- if(sustainedNotes.indexOf(event.pitch) == -1) {
- sustainedNotes.push(event.pitch);
- }
- } else {
- if(activeNotes.indexOf(event.pitch) == -1) {
- activeNotes.push(event.pitch);
- }
- }
- if(posActiveNotes.indexOf(event.pitch) == -1 ) posActiveNotes.push(event.pitch);
- if(event.pitch < lowestNotePitch || lowestNotePitch == -1) {
- lowestNotePitch = event.pitch;
- chordOn();
- }
- if(SEND_ALL_NOTES_ON) {
- event.send();
- }
- } else if (event instanceof NoteOff) {
- var indexOfNote = activeNotes.indexOf(event.pitch);
- if(indexOfNote != -1) {
- activeNotes.splice(indexOfNote, 1);
- }
- var indexOfPossible = posActiveNotes.indexOf(event.pitch);
- if(indexOfPossible != -1) {
- posActiveNotes.splice(indexOfPossible, 1);
- }
- if(event.pitch == lowestNotePitch) {
- getLowestNoteFromActive();
- }
- event.send();
- } else if(event.number == 64) {
- if(event.value == 127) {
- isSustained = true;
- sustainedNotes = sustainedNotes.concat(activeNotes);
- activeNotes = posActiveNotes;
- } else if(event.value == 0) {
- isSustained = false;
- sustainedNotes = [];
- activeNotes = posActiveNotes;
- getLowestNoteFromActive();
- }
- event.send();
- } else {
- event.send();
- }
- }
- function ParameterChanged (param, value) {
- switch (param) {
- case 0:
- HIGH_RANGE = value;
- if (value < LOW_RANGE) {
- SetParameter(1, value);
- }
- break;
- case 1:
- LOW_RANGE = value;
- if (value > HIGH_RANGE) {
- SetParameter(0, value);
- }
- break;
- case 2:
- CHORD_VELOCITY = value;
- break;
- case 3:
- SEND_ALL_NOTES_ON = (value === 0);
- break;
- default:
- //nothing
- }
- }
- var PluginParameters = [{
- name:"High Range",
- type:"linear",
- minValue:0,
- maxValue:127,
- numberOfSteps:127,
- defaultValue:72
- }, {
- name:"Low Range",
- type:"linear",
- minValue:0,
- maxValue:127,
- numberOfSteps:127,
- defaultValue:48
- }, {
- type:"text",
- name:"When zero, velocity = last note's velocity."
- }, {
- name:"Chord Velocity",
- type:"linear",
- minValue:0,
- maxValue:127,
- numberOfSteps:127,
- defaultValue:0
- }, {
- name:"Send All Notes On",
- type:"menu",
- valueStrings:["On", "Off"],
- defaultValue:1,
- numberOfSteps:2
- }];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement