Adding notes/rest and increase the measure length

• Sep 22, 2021 - 20:16

Hi,
Is there a (simple) way to have the cursor.addNote adding a note and increasing the measure length ?
Like with the "Insert" mode:
Insérer.png

Similary, is there a way to remove an element, even if it leads to shortened measure ?
ms1.png

Thanks,


Comments

I haven't tried it, but my understanding is you can executable any named command from a plugin, so running the "insert-a" command should work, and conversely "time-delete" (internal name for "remove selected range").

In reply to by Marc Sabatella

Thanks. The only thing I'm not able to manage is the duration of the inserted notes.
As these commands are not working at the cursor level (curScore.newCursor), setting cursor.setDuration(1,8) does not affect the duration of the inserted notes.

Other observation, using a "insert-a" command let the UI in that insert mode. How can I programmatically leave that insert mode (i.e. simulate the user pressing the "Escape" key) ?

In my attempt to add a rest at the end of the score, I've tried this:

function appendRest2(cursor) {
    var measure = cursor.measure;
    var sig=measure.timesigActual;
    measure.timesigActual=fraction(sig.numerator+1,sig.denominator);
}

I've got a strange result:
The 5th beat is empty : no note, no rest. And I can find why to get there in order to add a note or a rest. Neither with the mouse nor by code:
empty.png

While when doing the same action from UI the 5th beat is filled with a rest:
correct.png

Any idea on how to adapt the code for 1) either having the rest adapted automatically, 2) or being able to select that "empty space" and a rest or a note ?

In reply to by parkingb

I believe I encountered the same issue when attempting to enforce time signatures. The "fill" command that is used when someone changes the actual duration of a measure with the UI was just not available to Plugins if I recall correctly.

https://musescore.org/en/project/time-signature-enforcer uses a different technique (adding note, deleting it, then swap) but I think that is likely to not work in many situations either (the plugin was intended for empty scores only).

In reply to by Marc Sabatella

These are working. Both. However, switching the voices, doesn't give a nice result, because all sudden you get a rest on voice 2 while at first you hadn't any..
Furthermore, if your score as 5 staves, you must do this for all the staves.
Other limitation, every call to cmd(xyz) leads to 1 Undo step. So undoing this single action (increasing the measure length by API) will lead to 1+N undo, N being the number of staves.
But it works.
Knowing that that a reload of the scores fills all the holes automatically is a good news, and reduced the needs to tackle that within the plugin itself.

In reply to by parkingb

Right, just as when doing this "for real", I'd expect you'd want to delete that voice 2 after the exchange. And yes, you need to do it for each staff - but, that's like two more lines of code to put it in a loop, right? For that matter, can't you just do all staves at once - and that pretty much addresses the undo issue also.

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