How to add a new note to every chord of a score
I am trying to write a plugin for MuseScore 3.x that harmonizes a melody; it should read a score with a single-voice melody and then add 2 more notes to each note (thus creating a chord), based on some rules. I can not find a way to iterate over the score and add a new note along an existing one. The cursor
method addNote
seems to overwrite the element and I can not find any API functionality to add a note to an existing element, in order to create a chord (not a chord
element - every note seems to be a chord element -, just to stack three notes to create a chord). Furthermore, overwriting an element with addNote
seems to break MuseScore. Sample code:
var cursor = curScore.newCursor();
cursor.track = 0;
cursor.voice = 0;
cursor.staffIdx = 0;
cursor.rewind(0);
while (cursor.segment) {
if (cursor.element && cursor.element.type === Element.CHORD) {
cursor.setDuration(cursor.element.numerator, cursor.element.denominator);
cursor.addNote(60);
}
cursor.next();
}
The above code breaks MuseScore. What am I missing here? Can anyone post a code snippet where they add one more note whenever they find a CHORD
element?
Comments
An ability to add a note to existing chords from a plugin was indeed missing in MuseScore 3, and it was implemented again recently, see #291790: Port the add/remove functions that exist on the Chord cpp end into the 3.x Plugin API wrapper.
This feature will be available in MuseScore 3.3 (beta version is available), and with it notes can be added to chords like shown below:
Hope this helps!
In reply to An ability to add a note to… by dmitrio95
Thanks for your answer. Unfortunately, it does not seem to work as I expected... It does add C to every existing
CHORD
element, but the notes added are not visible. They only become visible after I randomly change the current voice from the GUI. What's more, you can not undo it after the plugin runs. Any idea why this happens? My main problem is the visibility that needs a random voice toggling to happen (notes are not in a different voice, they appear in all voices after the random toggling).In reply to Thanks for your answer… by sotniarchos1
Is this a dock or dialog type plugin, or "plain old just-do-it"? The undo strategies differ between the first two and the last.
In reply to Is this a dock or dialog… by [DELETED] 1831606
dialog, because I need some input from the user
In reply to dialog, because I need some… by sotniarchos1
Then you have to call curScore.startCmd() and curScore.endCmd() around the code that changes stuff, framing the stuff that can be undone.
In reply to Then you have to call… by [DELETED] 1831606
Everything worked perfectly, thank you so much!
In reply to Everything worked perfectly,… by sotniarchos1
Black art, plugin writing, black art.