Plugin: Accessing rest dots

• Apr 9, 2022 - 18:00

How do I access rests dots in a plugin to change their colour? I can change the colour of the rests themselves but, even though there are dotted rests in my score, I can't seem to get a handle on the dots.

This console.log line never triggers:

finddots.png


Comments

You can assess this by the duration:

var current = durationTo64(element.duration);
var analyze = analyzeDuration(current);
 
console.log(current + " => " + analyze.base + "/" + analyze.ratio)
 
function analyzeDuration(target) {
    var base = Math.log(target) / Math.log(2);
    var remaining = base - (base | 0); // base - trunc(base);
    base = base | 0; // = trunc
    base = Math.pow(2, base);
    var ratio = ((Math.pow(2, remaining) * 10000) | 0) / 10000;
 
    var half = null; ;
 
    // Gestion des arrondis
    // Gestion des arrondis
    if (Math.abs(ratio - 1.125) <= 0.01) {
        ratio = 1.125;
        half = 1.25;
    } else if (Math.abs(ratio - 1.25) <= 0.01) {
        ratio = 1.25;
        half = 1.5;
    } else if (Math.abs(ratio - 1.375) <= 0.01) {
        ratio = 1.375;
        half = 1.75;
    } else if (Math.abs(ratio - 1.4375) <= 0.01) {
        ratio = 1.4375;
        half = 1.875;
    } else if (Math.abs(ratio - 1.46875) <= 0.01) {
        ratio = 1.46875;
        half = 1.9375;
    } else if (Math.abs(ratio - 1.5) <= 0.01)
        ratio = 1.5;
    else if (Math.abs(ratio - 1.75) <= 0.01)
        ratio = 1.75;
    else if (Math.abs(ratio - 1.875) <= 0.01)
        ratio = 1.875;
    else if (Math.abs(ratio - 1.9375) <= 0.01)
        ratio = 1.9375;
 
    return {
        "base": base,
        "ratio": ratio,
        "half": half
    };
 
}
 
 
function durationTo64(duration) {
    return 64 * duration.numerator / duration.denominator;
    }

Remark: "dotted" elements have always a length >= 1.5* the "non-dotted" element length.
This code also deals where one have to achieve a length between in [1,1.5[ of the the "non-dotted" element.

In reply to by parkingb

If I can't get a JavaScript object to access the rest dot then I don't see any way of changing the dot's properties, e.g. colour. Am I missing something?

(I can do this easily enough via my Excel plugout framework but that means working with the MSCX file rather than the MSCZ file which, in this case, is just not an efficient way to work.)

In reply to by jeetee

Oops! It's public now.

Selection.elements sounds like it would not be useable unless I actually selected a range. This plugin processes the whole score as it has no benefit in applying only to selections, but I'll check out your suggestion as a possible workaround.

In reply to by yonah_ag

You can force a selection with the selectRange method. Sometimes with the API you don't have any other choices than making an explicit selection in order to access some elements or functions you would normally expect to be exposed by the API as object properties or object methods but that are not.

In reply to by parkingb

@parkingb
This doesn't work for whole rests that span a non 4/4 measure. What we really need is access to the nominal duration e.durationType. This returns an QVariant(Ms::TDuration) which unfortunately is completely empty and unreadable. It has 0 properties and both
console.log(toString(selection.elements[0].durationType));
console.log(JSON.stringify(selection.elements[0].durationType));
don't give meaningful results. Has anyone found a workaround for that?

In reply to by harmony8

> whole rests that span a non 4/4 measure

Can you post an example (screenshot) of this ? I'm not sure to understand...

Indeed, the durationType is always undefined. However you have a duration property at the CHORD level that returns a Duration object.

E.g. for a dotted quarter:

....[Chord].duration.denominator: 8
....[Chord].duration.numerator: 3

In reply to by parkingb

I mean that these two cases (see attached image) can not be differentiated. Both have a duration of 3/4. If you use duration related logic you will always derive this to be a dotted half rest, but there might be a whole rest displayed instead. I don't think this case can be identified without durationType. Has anyone found a solution for this?

Attachment Size
Unbenannt.png 6.75 KB

In reply to by harmony8

If your goal is to differentiate both cases (the "duration" rest and the "empty measure" rest), indeed using the duration property doesn't work.
But I wonder. In which would you to differentiate both cases ?

In MU4, I tested at the rest level durationType is defined:
restDurationMU4.png

In reply to by harmony8

Building around the MuseScore internal format (or the limitations of what is available via the plugin framework) might not be the most sustainable approach for this use case.
It might be worth looking into the MusicXML format instead (which MuseScore can export).

In reply to by jeetee

What you could do is create a plugin exporting the file to MusicXML, launch this plugin from an external app and the analyse the MusicXML file.

I've done this with success, from a java application of mine. This is a kind of score index. The application launch a MU plugin that extracts info from MU file selected by the user and save this in a json file that the application parses in order to store the file properties.

In your case, instead of exporting the file in a json format you could export it in the MusicXML format.

The limit of this approach is that it requires MU3.6-7 as the API of MU4.x does not support exporting files for the time being.

The same limitation would apply if you would proceed the other way around : write a plugin launched from MU that would export the file to MusicXML and then have the plugin launching an external application/script.

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