How to achieve variable gain using a micro-controller
What approaches is possible for a variable gain?


One approach if you are on a budget is to use high frequency PWM to toggle between the audio signal and 0 volts. The resulting waveform can be simply low pass filtered to recover the audio at a reduced volume that is proportional to PWM duty cycle: -



Picture from this Q&amp;A.

So, in the top image the input is (say) a triangle wave. The 2nd image shows the triangle wave and the PWM signal (above nyquist of course) and this samples the triangle wave at reasonable speed. After low pass filtering (can be as simple as an RC filter) the amplitude is reduced. Altering the duty cycle changes the volume level.

You can implement this with a transistor (with care) or an analogue gate.