An improved horizontal spacing algorithm

• Nov 30, 2021 - 11:22

Following a hint by Marc Sabatella and starting from this older discussion I have written a new spacing algorithm for Musescore which guarantees spacing consistency and almost exactly replicates the spacing rules of Gould. It also has some other interesting features, including a customizable spacing formula and a more refined behaviour under the stretching } and { commands. The modifications to the code are surgical (less than 50 lines I think), which means the new function should not interfere unexpectedly with any other behaviour of the program. Here is my working branch

I've tested it on anything I could think of and it seems to work perfectly. Please, help me break it, see if you can find limit cases or weird behaviour. I'd be very happy to receive suggestions. If all looks fine, I'll make the pull request, and I hope it will be considered for the release of 4.0 :) Attached here is a pdf with the full documentation, including some simple explanation about the the fascinating math of music spacing.

I want to thank Marc Sabatella and Barnie Snyman for that amazing discussion, which made me quickly realise the problems and a way to fix them.

Looking forward to your comments and opinions!


I read that previous spacing discussion with great interest and was thrilled to see your followup & extremely elegant solution here today. The attached PDF is a phenomenally interesting read. I hope to see your spacing formula implemented in the release of Musescore 4.0!

I leave this comment to congratulate you on your contribution and also to not miss any further commentary on the subject. :)

I've played around with the build from your PR and it's amazing. Does exactly what it needs to do, with surprisingly little actual change to the code. Fantastic job, and thanks for the writeup! I wish we had more of these code design documents.

In reply to by Marc Sabatella

Well, I have to thank you for that older discussion, cause that's what made me go like: "Wait, I know how to fix that!". Anyhow, I'm really happy that it's working well!
I've had a brief chat with Simon and it seems that the long-term plan (or wish, at least) is to rewrite the algorithm altogether and go for something much more sophisticated. But in the meanwhile, we get a solid step forward with little effort :)

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