Duration Calculation Algorithm

• Mar 23, 2020 - 21:48

I've written a program to simulate cancer growth in musicxml songs. I'm using musescore to convert the output (after validation) to mp3, midi, etc., but it runs into errors quite a bit, particularly with the calculated duration not matching the expected one. I've managed to get my program pretty close, so it works with most pieces, but there are still some more complicated ones that are a little off.

Does anyone happen to know musescore's algorithm for calculating durations, or at least be able to point me in the right direction? I've combed the the forums pretty thoroughly and have only found a few hints as to how it works.

Any assistance will be greatly appreciated!


Comments

There's not that much "calculation" involved.
There is an actual measure duration for each measure, by default matching the time signature.
If voice 1 notes don't fill this duration, either to long/short, then it is considered a corruption.

In reply to by jeetee

I probably should have been more clear. I'm not concerned about the note durations so much as the whole measure duration. If the calculated measure duration deviates from the specified time signature (in the attributes block of the first measure of that part), I get an error when I load the file into MuseScore. So what I want to know is how does MuseScore determine if a measure is consistent with the time signature?

In reply to by icwells

I got that question the first time around; seems you didn't get my answer though.

You get that warning if the combined duration of notes (and rests) in voice 1 of a measure are not equal to the actual measure duration.
Actual measure duration is a property that can be set on each measure (but defaults to time signature).

Voice 1 must be complete according to the actual duration of the measure

In reply to by jeetee

What I'm asking for is the code (or pseudo-code) that MuseScore uses to do that. Specifically, how does it determine the specified duration from the time signature and divisions in the attributes block in order to compare it to the actual measure duration?

Our program is independent so I need to know how MuseScore determines this so I can correct any errors in the xml.

In reply to by icwells

So as you can see from Jojo's link to code, it really is as simple as summing up the contents of a voice within a measure.

If the duration of my measure is 7/8 and its contents is: 1/4 + 3/8 + 1/2 then the sum of that content (9/8) is larger, thus the measure is considered corrupt.

In reply to by icwells

And really, it shouldn't matter what MuseScore's code looks like exactly. Ideally, you would just make sure to generate valid MusicXML, and if it's valid, MuseScore will accept it. Make sure there are the proper number of beats in the measure according to the standard rules of how music works - notes are read left to right within a voice, each assumed to start after the previous note ends, so as mentioned, the durations of the notes need to add up to that of the measure, pretty much as simple as that.

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