Trying to write a crescendo/diminuendo plugin

• Aug 3, 2011 - 06:13

I'm trying to write a plugin to create crescendos and diminuendos. What it's going to do is you first set the dynamic value of the first and last notes where you want the dynamic change (either using a dynamic marking such as pianissimo or forte, or just manually changing the note velocities), then you select all the notes and the plugin first counts how many notes there are, then gets the values of the first and last note velocities, it gets the difference (last - first), then divides by the number of notes, and increments the notes up by that value.

The thing that's getting me stuck, is cursor.chord().note(0).velocity only returns blank values, everything else seems to be working, but it can't get the velocity values. I'm using the latest MuseScore 1.1, which is the version which added support for reading and editing the velocity.


Comments

I'm thinking maybe this is a bug? When I change .velocity to .pitch, it works fine, and returns the integer of the pitch. So maybe there's a bug in the plugin architecture for note velocity.

In reply to by [DELETED] 5

First let me say this is my first plugin I've written, so if it's written strange or sloppy, that's why. But I was using QMessageBox to display the values of things to make sure it was working, a way of testing since we don't have a terminal output like in Java. The QMessageBox successfully returned all the values of other things, like note.pitch, so I really don't think that it is the QMessageBox. Any ways, here is what I have so far:

function setDynamics() {
messageBox = new QMessageBox();
messageBox.setWindowTitle("Message Box");

var cursor = new Cursor(curScore);
var selectionEnd = new Cursor(curScore);
var numberOfChords = 0;
var startingVelocity = 0;
var endingVelocity = 0;

cursor.goToSelectionStart();
selectionEnd.goToSelectionEnd();

//Find how many notes/chords are in selection
for(numberOfChords = 0; cursor.tick() < selectionEnd.tick(); numberOfChords++) {
cursor.next();
while(cursor.isRest()) {cursor.next();}
}

//Get starting and ending velocities
cursor.goToSelectionStart();
for(var i = 1; i <= numberOfChords; i++) {

if(i == 1) {
if(cursor.isChord()) {
startingVelocity = cursor.chord().note(0).velocity;
messageBox.text = cursor.chord().note(0).velocity;
messageBox.exec();
}
}
if(i == numberOfChords) {
var chord = cursor.chord();
endingVelocity = cursor.chord().note(0).velocity;
messageBox.text = cursor.chord().note(0).velocity;
messageBox.exec();
}
cursor.next();
while(cursor.isRest()) {cursor.next();}
}
messageBox.text = startingVelocity+" and "+endingVelocity;
messageBox.exec();

//Set velocity to notes
for(var i = 1; i <= numberOfChords; i++) {

}
}

In reply to by [DELETED] 5

Well I tried running your very own code, and it still returns undefined, and like I said before I can replace velocity with pitch and it returns a value, so it's getting the notes (not to mention the fact that cursor.isChord returns true). What system do you have? Windows? Maybe this is a glitch with the Ubuntu version of MuseScore?

OH MY GOD, I feel SOOOO stupid right now.... I just checked and I have version 1.0! I feel so stupid.... I just need to update and it should work fine...

I'm going to build MuseScore 1.1 in a little bit, right now it's downloading the dependencies, then I should be able to finish the plugin today (hopefully).

Well I compiled/installed 1.1, and finishing the plugin was a snap! I'm just going to finish a few more things and then I'll post it

I finished the plugin, it is functional and easy to use. I've already uploaded it here:
http://musescore.org/en/project/crescendoanddiminuendoplayback

It did take me a little while to get it uploaded correctly though, this is the first plugin I've created, but it was really pretty easy (other than the fact that I didn't realize I was on MuseScore 1.0). After updating MuseScore it was pretty easy to finish.

And thanks to Marc Sabatella for giving me the initial idea of how to go about making this plugin.

Correct me if I'm wrong, but this has to be a glitch with MuseScore. My plugin works fine on anything I write, but on all of the scores I import from MusicXML, they all crash when I try to use the plugin. If I rewrite the exact same thing, detail for detail, it works fine, it's ONLY when I import a song from a MusicXML format. I even tried copying and pasting the notes onto a new piece of music, and it still crashes, so it must be something that the MusicXML import does to individual notes, and copying and pasting them copies and pastes the problem along with it. I have no idea why it is doing this, but it's irritating, because I wrote this plugin specifically for these songs that I import from MusicXML. Any help would be appreciated.

I'm going to write a bug report once I understand this better, but I do think I have an idea what's going on. In my plugin, to find out how many notes/chords are in the selected area for the increment I use:
for(numberOfChords = 0; cursor.tick() < selectionEnd.tick(); numberOfChords++) {
cursor.next();
while(cursor.isRest()) {cursor.next();}
}

which works great, but on the MusicXML I imported it's finding more chords then there are, I realized random notes are being counted multiple times, I have no clue why. And it will be very random, the first few 1/4 and 1/8 notes are counted once, then some of the 1/4 and 1/8 notes suddenly start counting as 2. And what this ends up doing is giving my endingVelocity variable a value of 0 which ends up crashing the plugin.

Also, trying to get a work around I wanted to do something using cursor.time() so if it was equal to the time of the previous it would skip it, and that should work around this problem however cursor.time() is only returning 0, regardless of where in the score I use it. And this isn't just on the XML imports, this is on anything, not sure if I'm misunderstanding how cursor.time() is supposed to work, I thought it returned the time in milliseconds that the cursor is at.

In reply to by [DELETED] 5

It wasn't crashing on copy/paste, what I meant was after copying and pasting the notes into a new score, it would still crash when I ran the plugin in the NEW score, but if I just rewrote the part on a new score detail for detail and ran it, then it wouldn't crash. But I did kinda figure out what was going on and found a work around. Read my other comment below, I just uploaded my latest version of the plugin and it's ready to use.

I have to say I am proud of myself though, I've never written a plugin for ANYTHING before, just some basic javascript in web design, but the MuseScore developers did make it pretty easy to write the plugins, I like the framework they gave us :]

I figured it out! It had to do with the while(cursor.tick() < selectionEnd.tick())
On the imported ones, the selectionEnd.tick is a few beats ahead, but I was able to get a work around, because even though it's going ahead, it's not reading a chord, so when I switched the for statement to a while statement for finding the number of chords, I put the numberOfChords++ inside of an if(cursor.isChord) block, that way it wouldn't count the extra steps it was taking, and now the plugin works flawlessly!

I'm going to upload the latest one right now, and now everyone can use it if they wish, if anyone has any problems please let me know.

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