You need some kind of compressor…
Apple provides some by default as Audio Unit plugins - but you then need something to run them in. The simplest I know of is Rogue Amoeba's SoundSource [Rogue Amoeba really do make some very solid sound manipulation tools, this is about their cheapest/simplest.]
You can then 'intercept' the output at either System or application level & insert the compressor to control the output.
A multiband compressor is not the easiest thing to juggle the settings of, if you're not used to audio engineering [& the Apple one doesn't have the best graphical representation to see what you're doing]. If you struggle, there's a simpler single-band comp called AUDynamicsProcessor.
SoundSource can run any Audio Unit plugin, so you might be able to find another compressor more suited to your specific needs. There are a lot available, free & paid. An alternative type of compressor is known as a Limiter [sometimes called a brick-wall limiter], which in effect holds an absolute maximum output & won't let anything be louder. They can be very effective, but also very aggressive in how they affect the sound.
The SoundSource app sits in the menu bar, so you can toggle it with a couple of clicks.
Personally, my multiband of choice is the Waves LinMB [Linear Phase Multiband Compressor] which lets you see as well as hear what you're doing- but that's another $30 on top of SoundSource.