Measure splits my chord

• Dec 30, 2023 - 18:22

Hello everyone, I've developed a plugin that manages the pitches of musical elements (double notes, single notes, or rests) and adjusts their durations. However, I've encountered an issue with double notes when they span across measures. In such cases, the double note seems to split, and the entire double note starts anew in the following measure. Has anyone experienced a similar problem, and do you have any insights on how to address this issue? Your assistance would be greatly appreciated. Thank you!

Attachment Size
measure.png 31.6 KB

Comments

import QtQuick 2.0
import MuseScore 3.0
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0

MuseScore {
menuPath: "Plugins.pluginName"
description: "Description goes here"
version: "1.0"
height: 400
width: 400
pluginType: "dialog"

// Function to get a random denominator
function getRandDenominator() {
    var denominators = [1, 2, 4, 8, 16, 32];
    var randomIndex = Math.floor(Math.random() * denominators.length);
    return denominators[randomIndex];
}

// Function to display elements based on user input
function display() {
    var elementObj = [];
    var cursor = curScore.newCursor();
    cursor.rewind(2);
    var endTick = cursor.tick;
    cursor.rewind(1);

    // Clone elements from the score
    while (cursor.tick < endTick) {
        elementObj.push({
            element: cursor.element.clone()
        });
        cursor.next();
    }

    // Get the user input for the number of repetitions
    var count = parseInt(inputField.text);

    // Create a new array of elements based on user input
    var newElements = [];
    for (var i = 0; i < count; i++) {
        newElements.push(elementObj[i % elementObj.length].element);
    }

    // Add new elements with random durations to the score
    for (var i = 0; i < newElements.length; i++) {
        curScore.startCmd();
        cursor.setDuration(1, getRandDenominator());



        if (newElements[i].type === 93) {
           if (newElements[i].notes.length === 1) {
               cursor.addNote(newElements[i].notes[0].pitch);
           } else {
               for (var j = 0; j < newElements[i].notes.length; j++) {
                    if(j === 0) {
                        cursor.addNote(newElements[i].notes[j].pitch);
                    }else{
                         curScore.startCmd();
                         cursor.addNote(newElements[i].notes[j].pitch, true);
                           curScore.endCmd();
                 }

               }
           }
        } else {
            cursor.addRest();
        }

        curScore.endCmd();
    }
}

// Main content
ColumnLayout {
    anchors.fill: parent

    TextField {
        Layout.fillWidth: true
        placeholderText: "Number of repetitions"
        id: inputField
    }

    Button {
        text: "Submit"
        onClicked: {
            display()
        }
    }
}

}

In reply to by kamilio141414

how about
...

var tick=cursor.tick
for (var j = 0; j < newElements[i].notes.length; j++) {
cursor.rewindToTick(tick)

...

edit

newElements[i].notes.length could change along with addition of notes too, so better

...

var originalnotelength=newElements[i].notes.length
var tick=cursor.tick
for (var j = 0; j < originalnotelength; j++) {
cursor.rewindToTick(tick)

...

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