-
-
Save Jojo-Schmitz/4202198 to your computer and use it in GitHub Desktop.
Applying a function to each note in a selection or the entire score
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
// Apply the given function to all notes in selection | |
// or, if nothing is selected, in the entire score | |
function applyToNotesInSelection(func) { | |
if (typeof curScore === 'undefined') | |
return; | |
var cursor = new Cursor(curScore); | |
cursor.goToSelectionStart(); | |
var startStaff = cursor.staff; | |
cursor.goToSelectionEnd(); | |
var endStaff = cursor.staff; | |
var endTick = cursor.tick() // if no selection, end of score | |
if (cursor.eos()) { // no selection | |
startStaff = 0; // start with 1st staff | |
endStaff = curScore.staves; // and end with last | |
} | |
for (var staff = startStaff; staff < endStaff; ++staff) { | |
for (var voice = 0; voice < 4; voice++) { | |
cursor.goToSelectionStart(); // sets voice to 0 | |
cursor.voice = voice; //voice has to be set after goTo | |
cursor.staff = staff; | |
if (cursor.eos()) | |
cursor.rewind() // if no selection, beginning of score | |
while (cursor.tick() < endTick) { | |
if (cursor.isChord()) { | |
var chord = cursor.chord(); | |
var n = chord.notes; | |
for (var i = 0; i < n; i++) { | |
var note = chord.note(i); | |
func(note); | |
} | |
} | |
cursor.next(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment