Accessing the Currently-Selected Duration in a Plugin
Hi, I'm thinking of making a plugin, never made one before, and I've been reading through other people's code and what documentation I can find to figure out how to do it.
My question is, how do I access the currently-selected duration in my code? As in, how do I figure out which one of those little duration-buttons at the top of the score is currently selected? I've found the setDuration function in the doxygen API docs, which takes two integers to set a Cursors duration, but I'm trying to get access to the those two integers themselves.
Also, I'm curious as to how the Fraction class is used for representing durations: I assume that a half note would be numerator = 1, denominator = 2, but would numerator = 2, denominator=4 also be a half note?
If I took this half note and added the numerator of a quarter note (which I assume is 1/4) would it give me a dotted half note (3/4, presumably)? the .txt file shows what I'm trying to do (don't know if it's actually valid qml but you get the idea).
(In the doxygen doc it lists 'actualDuration' and 'globalDuration' as fractions but 'duration' as a QVariant, which is sorta like a void pointer, right? But if I recall correctly people were referencing it like a fraction.)
Attachment | Size |
---|---|
musescorequestion.txt | 375 bytes |
Comments
This is a starting point:
I took a few assumptions (for the ease of the code readability):
.duration
is the nominal element's duration. To get the effective (aka "real") duration, you must access the element's tuplet properties (its ratio).Other approaches could go via the
Cursor
object:Check which one works the best for your case in different use cases (in terms of selection), such as :
In reply to This is a starting point: … by parkingb
OK, thanks for the example code. Ultimately I'm trying to make it so I can press a specified key and it will lengthen the last-inputted note/chord by the currently-selected duration. So, enter an eighth note, press a key and it lengthens to a quarter note, press it again and it becomes a dotted quarter note, etc. I either need to know the current active duration (which would make this simple), or be able to set up an event listener, which I'm not sure if plugins can do.
In reply to OK, thanks for the example… by J_Bowden
To handle tuplets look at:
https://musescore.org/en/project/new-retrograde
// get note/rest durations first
if(e.tuplet) { // tuplets are a special case
tupdur.push(e.tuplet.globalDuration.numerator);
tupdur.push(e.tuplet.globalDuration.denominator);
ratio.push(e.tuplet.actualNotes);
ratio.push(e.tuplet.normalNotes);
}
In reply to OK, thanks for the example… by J_Bowden
Yes, THIS. I thought I invented the idea today, but happy to see someone already figured it out.
How is it coming? I am going to look into this directly. Surely you can query the state of toolbar items???
It sounds like plugins can create gui's so worst case I suppose you could create an artificial note value for the plugin to use, and ignore the setting in the toolbar.
In reply to Yes, THIS. I thought I… by woodslanding
Oh now I wouldn't be able to even start on this project until at least like halfway through this year, so if you can figure it out please do.
If you can query the state of the toolbar duration, I think should be pretty easy. Something like: 1) get current toolbar duration; 2) make a cursor set to the currently selected note; 3) get the duration of that note; 4) new duration = toolbar duration + selected duration; 5) rewrite all the notes in the chord of the currently selected note to the new duration. Then, shortcut this to one of the keys (like Enter, it ain't doing anything important) so that every-time you want to lengthen a note you just hit enter. Of course it wouldn't work in MS4 but I mean the plugin system there is still in-dev.
In reply to OK, thanks for the example… by J_Bowden
It looks from the code above like you can easily get the length of the currently selected note. You could just store that value when you enable the feature, and ignore the values in the toolbar, which will then update with changes of note length normally.
You will have to reset the length of newly created notes to the stored length every time the toolbar value changes, but once again, it doesn't require querying the toolbar, just changing the selection. But maybe that means you'd have to toggle the script on and off to change the notestretch value.
It's not clear to me that the toolbar would actually change its note length though, if already created notes are having their lengths changed via script. I guess that needs to be tested. If it doesn't, then it would in fact be very elegant to use the toolbar values to set the notestretch amount in the script. Then you wouldn't have to toggle the script on and off.
Sorry, thinking out loud here. So now I see where you might need a listener... hmmm. I'll have to read the docs some more. I guess if you run a query of the toolbar in a loop that runs periodically as long as the script is enabled, that's effectively a listener, right? But you still have to know the active duration. Doesn't look like the retrograde script ever gets that, it deals exclusively with the selection. I guess I'll grub around and see if I can find an example of something referencing toolbar values.
Which I guess is exactly where we started.
Well, I'm still going to leave this up, in case I forget, and start going through this whole reasoning process again. ;)
In reply to It looks from the code above… by woodslanding
I don't think you can run a plugin while in note input mode, but I may be wrong...
In reply to I don't think you can run a… by elsewhere
Seems to work fine for me.
In reply to OK, thanks for the example… by J_Bowden
Have you tried my Duration Editor plugin ? It offers a mini-toolbar and let's you manipulate the notes durations:
PLEASE read the plugin page, because it uses another duration manipulation paradigm than the traditional Musescore one.
In reply to Have you tried my [Duration… by parkingb
I did read about it, and since I was worried that ultimately the only way to do this might be to create your own notelength gui, I wondered about trying to add the feature to your plugin.
In reply to I did read about it, and… by woodslanding
I'm always open to add features to my plugin as long as it fits into the plugin philosophy.
Feel free to describe precisely what you are trying to achieve here. A User Story is always a good way to describe a feature request. Once you've done it, I will look at it and tell you if it is feasible or not and if I can find some spare time to work on it.
In reply to I'm always open to add… by parkingb
Done!
In reply to Have you tried my [Duration… by parkingb
Yeah, your code was actually what I was reading through to try and figure out how plugins work.
In reply to Yeah, your code was actually… by J_Bowden
This plugin is definitely not the easiest to understand. As it uses concepts most plugin don't use (such as transposing instruments or multi -voices edition) and as it tweaks heavily Musescore's note modification paradigm.
However most of the logic that could interest you is located in "NoteHelper js" library.