There's an integrated circuit, LM13700, that has this basic function.
See figure 17 here
<
https://www.ti.com/lit/ds/symlink/lm13700.pdf>
That example uses one half the chip, and responds to pulling the 'gain control' pin low
when the sound is too loud. The tricky part is, you use the other half of the chip
to amplify, rectify the sound from AC to negative DC, smooth the DC with a capacitor,
and steal some current from the "Iabc" circuit node while that capacitor
stays negative. It's just a couple of capacitors, diode, and a lot of fiddling with
resistor values.
Alas, it's an oddball chip, and an oddball kind of design.