A few years ago I wrote a Haskell program for translating MusicXML scores (coming from music typesetting software like Sibelius, Dorico, or MuseScore) into MIDI events, in a way that provided custom interpretation of the scores and score objects. I did this to experiment with ways of adding flexible expression to a score that would otherwise sound stiff if played by the typesetting software itself or a DAW.
There are some existing Haskell music packages but none of them allowed me to do enough customization to explore the specific ideas I had in mind, so I wrote from scratch.
I just came back to my program after several years of using it but not looking at the source. It’s pretty dreadful. I need to make some changes and it’s hard to understand how to make them. I think it might be worth rewriting.
Note that one of the simplest ways to express how a score gets translated into MIDI (which is necessarily a pretty complex task) is to define several stages of the translation. In the first stage, we represent a score by a one-to-one representation of objects in the original score. In the second stage, we translate the notes on the score to a kind of pseudonote, which is like a note but has a lot of context added to it and some alterations, perhaps altered in time or pitch. It is also possible that a score note gets translated into 0, 1, or many pseudonotes. In the third stage the pseuonotes are translated into MIDI events, again with a possible many-to-one relationship.
What I’m thinking of doing is using a state monad that stores the entire context and history of the translation. What I’m doing in the existing program is pass around just the needed context. But when I make significant changes to the “model” of what the program is doing (like say, I introduce the idea that notes can be shifted in time by absolute seconds rather than the previous model which was shifting by beats), I find myself frustrated by the need to change what is being passed around.
The algorithms are necessarily complex. What I want to try to do is write easily understood functions. Each function will do some transformations or generations of data following the necessary steps, which will be named as clearly as possible and listed sequentially. All context will be available to read at every stage. As far as generating new data, there will be some Maybe fields which are initialized to Nothing and gradually change to things like Just Stage2 as the sequence of steps is followed.
Another complexity is that I want good error messages. I could perhaps chain an Error monad with my State monad. Also I could put a random generator in the state for those parts of the translation that use randomness.