Use the software PWM for fan control to not generate annoying sounds. Allow increasing software PWM frequency.
Control the fan when it's supposed to be, not when the g-codes are received.