I can think of two ways.
One of them is to constantly run the algorithmic layout mechanism in the background by controlling a synthesizer system that creates music on the go, like sound effects. It can react literally to anything, and quite quickly (for example, based on a bar). You could, for example, have themes for each character in the game and enter them as characters displayed ... even change the feeling of individual character themes in accordance with the events in the game (a sad topic when your friend dies ?.).
Another is the presence of a large number of audio tracks annotated for their BPM and impact position in the sample, as well as ordered and annotated so that they have transition points. Then the audio system would listen to the next track with a rhythm aligned at the transition point, and then an intersection, just like a dance DJ. This system will be slightly less reactive, so it will be carefully aligned with the level design, so the changes in the music will be a bit overpriced before the events in the game.
I think they can be combined to some extent.
source share