Knowing which staff/clef a Note is on ?

• Feb 22, 2022 - 00:16

I'm iterating over a score and I want to separate the staff/clef (not sure what the nomenclature is for musescore) into different elements that I'm going to be pasting into another program (see my other post for details). I can work with stitching them together, but it would be cleaner if I could separate them.

Is there a way I can tell which staff a Note object is on ?

Likewise, in a similar vein - given a note, is it possible to tell:
1) What the key signature is for the given Note ?
2) What the time signature is for the given Note ?

The last two are just gravy, but would be great.


How are you iterating over the score?

If you use a Cursor, you start by setting it's track, which is a voice within a certain staff; so everything after that is then pre-filtered for you. You can also set it's filter to include key and time signatures instead of the default ChordRests only.
Or you can access it's .keySignature property if you don't keep track of those yourself.

Paste the below into a new plugin in onRun and you'll see it logging out what you're looking for:

if (curScore) {
    var c = curScore.newCursor();
    c.track = 0; // first voice of first staff
    c.filter = Segment.KeySig | Segment.TimeSig | Segment.ChordRest;
    do {
        console.log(c.tick, c.segment.segmentType);
    } while(;

In reply to by mrproxxxy1

Since a Clef can change throughout a Staff, the Staff object indeed can't give you a simple answer to that question; it'd need a time reference and then search backward from there to give you that answer.
Something you can do with a Cursor instead (go to selection start, filter for HeaderClefs (? not sure, try All and see how it is identified), test selection start, then move backwards until you find a Clef).

But that'd still won't tell you whether you're on the upper or lower staff of a Grand Staff instrument.

In reply to by sammik

This doesn't make sense to me. Isn't posY a position relative to the parent (in this case the chord?) (as per…).
This seems like a bad approach to determine a clef; making it likely hard to account for transposing instruments and transposing clefs.

If you want to figure out the clef (again, which has no determinate relation to identifying a staff) then why not navigate backwards until you find an actual Clef element?

But as mentioned before; the clef does not define which staff your on at all.

Do you still have an unanswered question? Please log in first to post your question.