Last active
April 8, 2022 18:34
-
-
Save acarabott/c5190e3307e952b48a7dba2ce8e5bbb6 to your computer and use it in GitHub Desktop.
A Logic Pro X Scripter patch to map note pitch to panning
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
function mapLinear(value, inMin, inMax, outMin, outMax) { | |
if (value <= inMin) { | |
return outMin; | |
} | |
if (value >= inMax) { | |
return outMax; | |
} | |
const inRange = inMax - inMin; | |
const valueNorm = (value - inMin) / inRange; | |
const outRange = outMax - outMin; | |
const scaled = outMin + valueNorm * outRange; | |
return scaled; | |
} | |
function HandleMIDI(event) { | |
if (event instanceof NoteOn) { | |
// define the note range you want to map | |
// any note below noteMin_midi will have the same pan value as noteMin_midi | |
// any note above noteMax_midi will have the same pan value as noteMax_midi | |
const noteMin_midi = 21; // A0 on piano | |
const noteMax_midi = 108; // C8 on piano | |
// define the panning range | |
// 0.0 is full left | |
// 1.0 is full right | |
const panMin_n = 0.1; // lowest note will be almost completely left | |
const panMax_n = 0.9; // highest note will be almost completely right | |
// map the pitch range to panning range | |
const panValue_n = mapLinear(event.pitch, noteMin_midi, noteMax_midi, panMin_n, panMax_n); | |
const panEvent = new ControlChange(); | |
panEvent.number = 10; // 10 is pan | |
panEvent.value = panValue_n * 127; // multiply by 127 to get back to MIDI range | |
panEvent.send(); | |
} | |
// send the original event | |
event.send(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment