Problem getting time signatures

• Nov 17, 2024 - 01:44

Hi all — I'm writing a plug-in to catch redundant time signatures — i.e. where the composer has entered the same time signature in succession, and the second one is unnecessary/redundant. I made a test score with 10 measures, containing the following written time signatures:
m. 1: 4/4
m. 2: 3/4
m. 3: 3/4 (redundant time sig)
m. 4: 4/4
m. 5: 4/4 (redundant time sig)

In my plugin, I wrote the code at the bottom of this post to get the time signatures and work out which ones are redundant. It's a basic Cursor loop. However, I can't understand the loop's output — it finds the time signatures at the end of each measure, and also it misses the last 4/4 time signature:
M. 1: FOUND TIME SIG 4/4 at tick 1920
M. 2: FOUND TIME SIG 3/4 at tick 3360
M. 3: FOUND TIME SIG 3/4 at tick 4800
M. 4: FOUND TIME SIG 4/4 at tick 6720

Any thoughts about this?
——————— CODE SNIPPET ————————

var cursor = curScore.newCursor();
cursor.rewind(Cursor.SCORE_START);
var currentBar = cursor.measure;
var lastBarNum = curScore.nmeasures;
for (currentBarNum = 1; currentBarNum <= lastBarNum; currentBarNum ++) {
    var currentBarTick = currentBar.firstSegment.tick;
    cursor.filter = Segment.All;
    cursor.track = 0;
    cursor.rewindToTick(barStartTick);
    var processingThisBar = cursor.element;
    while (processingThisBar) {
        var elem = cursor.element;
        var eType = elem.type;
        var eName = elem.name;
        if (eType === Element.TIMESIG && currentStaffNum == 0) {
            dialog.msg += "\nM. "+currentBarNum+": FOUND TIME SIG "+elem.timesigActual.str+" at tick "+cursor.segment.tick;
        }
        if (cursor.next()) {
            processingThisBar = cursor.measure.is(currentBar);
        } else {
            processingThisBar = false;
        }
    }
    currentBar = currentBar.nextMeasure;
}

Comments

For anyone reading this, I solved it by not using Cursor, which seems to spit out these spurious results. I'm not sure if this is a bug with Cursor, or something else? Anyway, instead, I use the following code (similar to the Big Time Signatures plugin code).

SAMPLE CODE:

   function checkTimeSignatures () {
        var segment = curScore.firstSegment();
        prevTimeSig = "";
        while (segment) {
            if (segment.segmentType == Segment.TimeSig) {
                var theTimeSig = segment.elementAt(0);
                if (theTimeSig.type == Element.TIMESIG) {
                    var theTimeSigStr = theTimeSig.timesig.str;
                    dialog.msg += "\n found time sig "+theTimeSigStr;
                    if (theTimeSigStr === prevTimeSig) {
                        addError("This time signature appears to be redundant (was already "+prevTimeSig+")\nIt can be safely deleted.",theTimeSig);
                    }
                    prevTimeSig = theTimeSigStr;
                }
            }
            segment = segment.next;
        }
    }

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