Problem finding end of a tick

• Jan 19, 2024 - 16:00

Hi guys my problem is that my plugin sometimes doesnt correctly find end of selected notes but other times is correct

As picure show I selected first measure only and it should paste notes with same rythm but diferent pitch right after first measure but it starts on third.

Do you guys see the problem in the code ? Sometimes when I select notes it works properly but other times not. Thanks

import QtQuick 2.0
import MuseScore 3.0

MuseScore {
menuPath: "Plugins.pluginName"
description: "Description goes here"
version: "1.0"

function getRandNum(range) {
    return Math.floor(Math.random() * range);
}

function getNextElementDuration(cursor) {
    cursor.next();
    return cursor.element.duration.numerator;
}

function getRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

onRun: {
    var objData = [];
    var cursor = curScore.newCursor();
    cursor.rewind(2);
    var endTick = cursor.tick;
    console.log(endTick);

    cursor.rewind(1);
    console.log(cursor.tick);

    while (cursor.tick < endTick) {
        if (cursor.element.type === 93) {
            if (cursor.element.duration.denominator && cursor.element.notes[0].tieForward) {
                objData.push({
                    elementNumerator: cursor.element.duration.numerator + getNextElementDuration(cursor),
                    elementDenominator: cursor.element.duration.denominator,
                    elementNumber: objData.length + 1,
                    pitch: cursor.element.notes[0].pitch,
                    idx: getRandNum(1000)
                });
                cursor.next();
            } else if (!cursor.element.notes[0].tieForward && cursor.element.type === 93) {
                objData.push({
                    elementNumerator: cursor.element.duration.numerator,
                    elementDenominator: cursor.element.duration.denominator,
                    elementNumber: objData.length + 1,
                    pitch: cursor.element.notes[0].pitch,
                    idx: getRandNum(1000)
                });
                cursor.next();
            }
        }

        if (cursor.element.type === 25) {
            console.log("beforeRest", cursor.tick)
            objData.push({
                elementNumerator: cursor.element.duration.numerator,
                elementDenominator: cursor.element.duration.denominator,
                elementNumber: objData.length + 1,
                pitch: "no-pitch",
                idx: getRandNum(1000)
            });     
            cursor.next();
        }
    }

    for (var i = 0; i < 30; i++) {
        console.log(objData[0].pitch)
        if (objData[i % objData.length].pitch === "no-pitch") {
            cursor.setDuration(objData[i % objData.length].elementNumerator, objData[i % objData.length].elementDenominator);
            cursor.addRest();
        } else {
            cursor.setDuration(objData[i % objData.length].elementNumerator, objData[i % objData.length].elementDenominator);
            cursor.addNote(getRandomNumber(60, 70));
        }
    }
}

}

Attachment Size
measure-ryth.png 23.25 KB

Comments

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