Skip to content

Instantly share code, notes, and snippets.

@lasconic
Created May 17, 2011 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lasconic/975969 to your computer and use it in GitHub Desktop.
Save lasconic/975969 to your computer and use it in GitHub Desktop.
Applying a function to each note in a selection
// Apply the given function to all notes in selection
function applyToNotesInSelection(func)
{
var cursor = new Cursor(curScore);
var selectionEnd = new Cursor(curScore);
cursor.goToSelectionStart();
selectionEnd.goToSelectionEnd();
var startStaff = cursor.staff;
var endStaff = selectionEnd.staff;
for (var staff = startStaff; staff < endStaff; ++staff) {
cursor.goToSelectionStart();
for (var v = 0; v < 4; v++) {
cursor.goToSelectionStart(); // set voice to 0
cursor.voice = v; //voice has to be set after goTo
cursor.staff = staff;
while (cursor.tick() < selectionEnd.tick()) {
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