I can now see what needs to be done in terms of file changes, I just have to find a way to do it. (Not going to describe it here, for obvious reasons)
In order not to make too drastic a change to the file, I think I would implement a 'successive approximation' strategy, in which the midi file is processed once, applying a change of about 50% of the maximum calculated, then checked. If it still needs more work, it would be processed again, getting closer to the 'ideal'.