Skip to content

Instantly share code, notes, and snippets.

@Jojo-Schmitz
Forked from lasconic/gist:975969
Last active October 13, 2015 13:27
Show Gist options
  • Save Jojo-Schmitz/4202198 to your computer and use it in GitHub Desktop.
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
// 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