Adding a TimeSignature in the middle of the score crashes MuseScore

• Aug 27, 2021 - 15:13

I'm trying to create different time signatures in a score.
Setting the time signature at the very beginning of the score works fine.
But adding a new time signature later-on in the score (but still at the beginning of a new measure) simply crashes MuseScore.

This is a little code to reproduce it:

import QtQuick 2.0
import MuseScore 3.0

MuseScore {
    menuPath: "Plugins.pluginName"
    description: "Description goes here"
    requiresScore: false
    version: "1.0"
    onRun: {
        // 1) create a score with one measure
        var score = newScore("McveTimeSig", "bass-flute", 1);

        var cursor = score.newCursor();
        cursor.track = 0;

        score.startCmd();

        cursor.rewind(0);

        // 2) setting the original time signature (OK)
        // (here 5/4)
        var beatsByMeasure = 5;
        var ts = newElement(Element.TIMESIG);
        ts.timesig = fraction(beatsByMeasure, 4);
        cursor.add(ts);
        console.log("Adapting measure to " + beatsByMeasure + "/4");


        // 3) adding notes (more notes than available in the unique 5/4 measure)
        cursor.rewind(0);
        var cur_time = cursor.segment.tick;
        var counter = 0;

        for (var i = 0; i < (beatsByMeasure + 2); i++, counter++) {
            if (counter > 0) {
                // 3.1) moving to the next position
                cursor.rewindToTick(cur_time); // be sure to move to the next rest, as now defined
                var success = cursor.next();
                if (!success) {
                    // We couldn't move to the next position == we are at the end of the score
                    // 3.2) A new measure
                    console.log("Adding A measure");
                    score.appendMeasures(1);
                    cursor.rewindToTick(cur_time);
                    cursor.next();

                    // START OF FEATURE .............
                    // 3.3) Changing the signature of that new measure
                    var ts = newElement(Element.TIMESIG);
                    ts.timesig = fraction(++beatsByMeasure, 4); // going for a 6/4
                    cursor.add(ts); // <--- BREAK MuseScore
                    //break; // attempt to isolate the issue
                    cursor.rewindToTick(cur_time); // be sure to move to the next rest, as now defined
                    cursor.next();
                    // ............... END OF FEATURE
                }
            }
            // 3.4) Converting the current rest to a note of 1/4
            cursor.setDuration(1, 4); // quarter
            var note = cursor.element;
            console.log("Adding note (" + i + ") at " + note.parent.tick);
            restToNote(note, 60 + i);

            cur_time = cursor.segment.tick;
        }

        score.endCmd();
    }

    function restToNote(rest, pitch) {
        if (rest.type != Element.REST)
            return;

        //console.log("==ON A REST==");
        var cur_time = rest.parent.tick; // getting rest's segment's tick
        var duration = rest.duration;
        var oCursor = curScore.newCursor()
            oCursor.rewindToTick(cur_time);
        oCursor.addNote(pitch);
        oCursor.rewindToTick(cur_time);
        var chord = oCursor.element;
        var note = chord.notes[0];
        return note;
    }

}

Anyone has an idea how to have this working ?

Thanks,


Comments

It seems to work. You even dont need to be at the begining of measure to add time signature.


import QtQuick 2.0
import MuseScore 3.0

MuseScore {
      version:  "3.0"
      description: "This demo plugin creates a new score."
      menuPath: "Plugins.createscore"
      requiresScore: false

      onRun: {
            console.log("hello createscore");
            var score = newScore("Test-Score", "piano", 5);

            var cursor = score.newCursor();
            cursor.track = 0;

            cursor.rewind(0);
            var ts = newElement(Element.TIMESIG);
            ts.timesig = fraction(5, 4);
            cursor.add(ts);

            cursor.rewind(0);
            //cursor.setDuration(1, 4);
            //cursor.addNote(60);

            cursor.nextMeasure();

            cursor.setDuration(1, 4);
            cursor.addNote(60);

            cursor.next();
            //cursor.nextMeasure();


            var ts = newElement(Element.TIMESIG);
            ts.timesig = fraction(6, 8);
            cursor.add(ts);


            Qt.quit();
            }
      }

In reply to by sammik

Hi, I elaborated from your example, and I made crash MuseScore again.
This is weird. I think the order of instructions is really important.

In your example, in the new measure, if you add first the new Signature before adding the note, MS crashes.
In your example, in the new measure, if you add the new Signature after adding the note, MS doesn't crash (your example).

This one is OK:

import QtQuick 2.0
import MuseScore 3.0

MuseScore {
      version:  "3.0"
      description: "This demo plugin creates a new score."
      menuPath: "Plugins.createscore"
      requiresScore: false

      onRun: {
            console.log("hello createscore");
            var score = newScore("Test-Score", "piano", 1);

            var cursor = score.newCursor();
            cursor.track = 0;

            cursor.rewind(0);
            var ts = newElement(Element.TIMESIG);
            ts.timesig = fraction(5, 4);
            cursor.add(ts);

            cursor.rewind(0);
            cursor.setDuration(1, 4);
            cursor.addNote(60);

            var t=cursor.segment.tick;

            score.appendMeasures(1);

            cursor.rewind(t);
            cursor.nextMeasure();

            cursor.setDuration(1, 4);
            cursor.addNote(60);

            var ts = newElement(Element.TIMESIG);
            ts.timesig = fraction(6, 8);
            cursor.add(ts);

            Qt.quit();
            }
      }

This one is KO:

import QtQuick 2.0
import MuseScore 3.0

MuseScore {
      version:  "3.0"
      description: "This demo plugin creates a new score."
      menuPath: "Plugins.createscore"
      requiresScore: false

      onRun: {
            console.log("hello createscore");
            var score = newScore("Test-Score", "piano", 1);

            var cursor = score.newCursor();
            cursor.track = 0;

            cursor.rewind(0);
            var ts = newElement(Element.TIMESIG);
            ts.timesig = fraction(5, 4);
            cursor.add(ts);

            cursor.rewind(0);
            cursor.setDuration(1, 4);
            cursor.addNote(60);

            var t=cursor.segment.tick;

            score.appendMeasures(1);

            cursor.rewind(t);
            cursor.nextMeasure();

            var ts = newElement(Element.TIMESIG);
            ts.timesig = fraction(6, 8);
            cursor.add(ts);

            cursor.setDuration(1, 4);
            cursor.addNote(60);

            Qt.quit();
            }
      }

Any way, thanks. I'll adapt my code knowing this strange behaviour.

In reply to by lvr124

I am not sure, what really is doing crash, as adding time signature without adding notes works for me as well.
onRun: {
console.log("hello createscore");
var score = newScore("Test-Score", "piano", 5);

        var cursor = score.newCursor();
        cursor.track = 0;

        cursor.rewind(0);
        var ts = newElement(Element.TIMESIG);
        ts.timesig = fraction(5, 4);
        cursor.add(ts);

        cursor.rewind(0);
        cursor.nextMeasure();

        var ts = newElement(Element.TIMESIG);
        ts.timesig = fraction(6, 4);
        cursor.add(ts);


        Qt.quit();
        }
  }

It seems, that problem is addig time signature to measure added by Plugin API.

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