Created
June 4, 2018 14:33
-
-
Save edrd-f/f61f701cd3fdbd7e60cd8a0c9912a8a0 to your computer and use it in GitHub Desktop.
Apple Logic MIDI script to toggle notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var notesOn = []; | |
function isNotePlaying(event) { | |
for (var i = 0; i < notesOn.length; i++) { | |
if (notesOn[i] && notesOn[i].pitch == event.pitch) { | |
return i; | |
} | |
} | |
return false; | |
} | |
function sendNoteOff(event) { | |
var noteOffEvent = new NoteOff(); | |
noteOffEvent.pitch = event.pitch; | |
noteOffEvent.channel = event.channel; | |
noteOffEvent.velocity = 0; | |
noteOffEvent.send(); | |
} | |
function HandleMIDI(event) | |
{ | |
//event.trace(); | |
if (event instanceof NoteOn) { | |
var noteIndex = isNotePlaying(event) | |
if (typeof noteIndex == 'number') { | |
delete notesOn[noteIndex]; | |
sendNoteOff(event); | |
} else { | |
notesOn.push(event); | |
event.send(); | |
} | |
return; | |
} | |
if (event instanceof NoteOff) { | |
return; | |
} | |
event.send(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment