[plugins noob] automating syncopation

• Jul 6, 2020 - 03:48

Hello. I am using MuseScore to convert running eightnotes into eightnotes on the beat and syncopated eightnotes. I have zombied together some code from various sources to try to make a plugin that accomplishes this end.

I'm stuck on how to actually remove the eight notes. I'm trying ``cursor.element.notes.remove()'' but it is throwing an error. Should I be replacing the notes with eight rests instead?

Code included below. Thank you for your time!

 
import QtQuick 2.0
import MuseScore 3.0
 
MuseScore {
      menuPath: "Plugins.pluginName"
      description: "Description goes here"
      version: "1.0"
      onRun: {
            console.log("hello world")
            var cursor = curScore.newCursor()
            cursor.rewind(1);
            var startStaff;
            var endStaff;
            var endTick;
            var fullScore = false;
            if (!cursor.segment) { // no selection
                  fullScore = true;
                  startStaff = 0; // start with 1st staff
                  endStaff = curScore.nstaves - 1; // and end with last
            } else {
                  startStaff = cursor.staffIdx;
                  cursor.rewind(2);
                  if (cursor.tick == 0) {
                        endTick = curScore.lastSegment.tick + 1;
                  } else {
                        endTick = cursor.tick;
                  }
                  endStaff = 4;
            }
 
          cursor.rewind(1);
          cursor.voice = 0;
          cursor.staffIdx = startStaff;
 
          var syncopate = true
          while (cursor.segment && (fullScore || cursor.tick < endTick)) { 
            syncopate = !syncopate  
             if(syncopate){break; }
             else{
                  cursor.element.notes.remove() // error messages :(
             }
          cursor.next()
          }
          Qt.quit()
          }
      }

Comments

Ooh boy. This was a rough one, but here is some code that kinda does what I want. I ended up using explode to get rid of any chords then can run this code. Requires having extra blank staffs. Assumes input is running eightnotes. Just have to play around with the instaff, outstaff, and syncopated variables and the product works!

I was never able to figure out how to get rid of extra notes and worked around it. For science, I would be interested if it is possible to resolve my original problem in a cleaner way, but I will be using this messy, messy code in its stead.

Thank you again for your consideration.

import QtQuick 2.0
import MuseScore 3.0
 
MuseScore {
      menuPath: "Plugins.pluginName"
      description: "Description goes here"
      version: "1.0"
      onRun: {
            var instaff = 1
            var outstaff = 4
            var syncopated = false;
 
            var cursor = curScore.newCursor();
            cursor.rewind(1);
            var sh = curScore.newCursor();
            sh.rewind(1);
            var startStaff;
            var endTick;
            var fullScore = false;
            if (!cursor.segment) { // no selection
                  fullScore = true;
                  startStaff = 0; // start with 1st staff
                  endStaff = curScore.nstaves - 1; // and end with last
            } else {
                  startStaff = cursor.staffIdx;
                  cursor.rewind(2);
                  if (cursor.tick == 0) {
                        endTick = curScore.lastSegment.tick + 1;
                  } else {
                        endTick = cursor.tick;
                  }
            }
          cursor.rewind(1); 
          cursor.voice = 0; 
          cursor.staffIdx = instaff;
          sh.rewind(1);
          sh.voice = 0; 
          sh.staffIdx = outstaff;
 
          while (cursor.segment && (fullScore || cursor.tick < endTick)) { 
            if (cursor.element && cursor.element.type == Element.CHORD) {
                  syncopated = !syncopated
                  var notes = cursor.element.notes;
 
                   if(syncopated){
                   console.log(sh.staffIdx)
                   sh.addNote(notes[0].pitch)
                   sh.next();
                   } else {}
 
              }
              cursor.next();
               sh.staffIdx = outstaff;
          }
 
          Qt.quit()
         }
      }

Can you explain the actual goal here? I mean, I know what syncopation is, but I don't see how it applies here. Can you show a before/after of what you are trying to achieve?

In reply to by Marc Sabatella

Before running the code, I added two blank staffs and converted the whole rest into 8 eight rests.

Then selected the whole top measure (running eight notes for an octave).

First time I ran the plugin, I used these settings:
var instaff = 0
var outstaff = 1
var syncopated = false

second run:
var instaff = 0
var outstaff = 2
var syncopated = true;

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