Increasing pitch

• Mar 6, 2024 - 15:14

Hey guys,

I'm trying to increase the pitch of a note by 10 in my code. However, I'm encountering an issue with the following line:

cursor.element.notes[0].pitch = cursor.element.notes[0].pitch + 10;
For instance, if all original notes have a pitch of 65 (which corresponds to F4), adding 10 should result in D#5s. However, instead of getting D#5s, I'm getting F5s.

How can I fix this?
import QtQuick 2.0
import MuseScore 3.0

MuseScore {
menuPath: "Plugins.pluginName"
description: "Description goes here"
version: "1.0"
onRun: {
var els = curScore.selection.elements;
var tracks = [];

    for (var i in els) {             
        if (!tracks.some(function(x) { return x == els[i].track; })) {
            tracks.push(els[i].track); 
        }
    }

    var cursor = curScore.newCursor(); 

    cursor.rewind(2); // go to the end of the selection
    var endTick = cursor.tick;

    if (endTick == 0) { // dealing with some bug when selecting to end.
        endTick = curScore.lastSegment.tick + 1;
    }

    var endStaff = cursor.staffIdx + 1;
    var endTrack = endStaff * 4;

    console.log("endTrack", endTrack)

    // Start        
    cursor.rewind(1); // go to the beginning of the selection
    var startSegTick = curScore.selection.startSegment.tick;
    var startTick = cursor.tick;
    var startStaff = cursor.staffIdx;
    var startTrack = startStaff * 4;
    cursor.rewind(1); // beginning of selection

    console.log("tracks", tracks)

    for (var j = 0; j < tracks.length; j++) {
        cursor.rewind(1);
        cursor.track = tracks[j];

        while (cursor.segment != null && cursor.tick < endTick) {      
            cursor.element.notes[0].pitch = cursor.element.notes[0].pitch + 10;
            cursor.next();
        }     
    }
}

}


Comments

In reply to by kamilio141414

So how would I input the correct note correctly based on previous notes.

Do I use some kind of formula like I used ? This code doesn't work properly

import QtQuick 2.0
import MuseScore 3.0

MuseScore {
menuPath: "Plugins.pluginName"
description: "Description goes here"
version: "1.0"
onRun: {
var els = curScore.selection.elements;
var tracks = [];

    for (var i in els) {             
        if (!tracks.some(function(x) { return x == els[i].track; })) {
            tracks.push(els[i].track); 
        }
    }

    var cursor = curScore.newCursor(); 

    cursor.rewind(2); // go to the end of the selection
    var endTick = cursor.tick;

    if (endTick == 0) { // dealing with some bug when selecting to end.
        endTick = curScore.lastSegment.tick + 1;
    }

    var endStaff = cursor.staffIdx + 1;
    var endTrack = endStaff * 4;

    console.log("endTrack", endTrack)

    // Start        
    cursor.rewind(1); // go to the beginning of the selection
    var startSegTick = curScore.selection.startSegment.tick;
    var startTick = cursor.tick;
    var startStaff = cursor.staffIdx;
    var startTrack = startStaff * 4;
    cursor.rewind(1); // beginning of selection

    console.log("tracks", tracks)

    for (var j = 0; j < tracks.length; j++) {
        cursor.rewind(1);
        cursor.track = tracks[j];

        while (cursor.segment != null && cursor.tick < endTick) {      
       var note = cursor.element.notes[0];
       note.pitch = note.pitch + 10
       note.tpc  = note.pitch - 46
       cursor.next()
        }     
    }
}

}

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