Getting the number of tempo changes from beginning of score until selection

• Apr 10, 2020 - 19:01

I want to add a functionality where I need to find the amount of tempo annotations, that have happened between the beginning of the score and the start of the selection.
I have this piece of code. Since this is my very first try at plug-ins I have a hard time to find the right things in the documentations and the only threads that are talking about this kind of thing are for MuseScore 1.

Here is the code:

function findRecentBPM(){
            var tempos = [];
            var cursor = curScore.newCursor();
 
            cursor.rewind(1); //sets cursor to start of selection
            var endTempoSearch = cursor.segment;
            cursor.rewind(0); //sets cursor to start of score
            var startTempoSearch = cursor.segment;
 
 
            while(startTempoSearch < endTempoSearch){
                        var an = startTempoSearch.annotations;
                        for (var i = 0; i < an.length; i++) {
                              if (an[i].type == Element.TEMPO_TEXT) {
                                    tempos.push(Math.round(an[i].tempo * 60));
                              }
                        }
                        startTempoSearch.next;
                  }
            return tempos.length
      }

For instance the attached image should return a value of 3.
Hopefully someone can guide me into the right direction. :)

Attachment Size
Capture.JPG 40.2 KB

Comments

while(startTempoSearch &lt; endTempoSearch){

Those variables are Segment object pointers; you're comparing memory addresses of objects, not whether they are the same or not.
Either change that condition to comparing the .tick property of those segments or to be a test of inequality instead.

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