The DIY Music Streamer Part 6.



How do we best address the issue with several volume controls? It depends a bit on what we want to achieve. One solution could be to use the ALSA mixer to set the hardware volume control and let Mopidy and shairport-sync use software volume control. Hardware volume control could be set to 50% and 100% software volume control in the applications would then represent 50% of maximum output from the hardware. Another option is to let the applications use the hardware volume control trough the ALSA API.

Lets take a closer look at the settings available in Mopidy and shairport-sync. The settings of interest in the Mopidy [audio] section are:
  • output
  • mixer
  • mixer_volume
Mopidy also got an extension named Mopidy-ALSAMixer to let Mopidy control the mixer trough ALSA. Lets install this extension with the command:

sudo apt-get install mopidy-alsamixer

This extension gives us some new settings in the [alsamixer] section:
  • card 
  • control 
  • min_volume 
  • max_volume 
  • volume_scale
 Now run the command:

amixer scontrols

It will  output:

Simple mixer control 'DSP Program',0
Simple mixer control 'Analogue',0
Simple mixer control 'Analogue Playback Boost',0
Simple mixer control 'Auto Mute',0
Simple mixer control 'Auto Mute Mono',0
Simple mixer control 'Auto Mute Time Left',0
Simple mixer control 'Auto Mute Time Right',0
Simple mixer control 'Clock Missing Period',0
Simple mixer control 'Deemphasis',0
Simple mixer control 'Digital',0
Simple mixer control 'Max Overclock DAC',0
Simple mixer control 'Max Overclock DSP',0
Simple mixer control 'Max Overclock PLL',0
Simple mixer control 'Volume Ramp Down Emergency Rate',0
Simple mixer control 'Volume Ramp Down Emergency Step',0
Simple mixer control 'Volume Ramp Down Rate',0
Simple mixer control 'Volume Ramp Down Step',0
Simple mixer control 'Volume Ramp Up Rate',0
Simple mixer control 'Volume Ramp Up Step',0


Card is 0 by default. The mixer we want to control is named "Digital" (the truncated version of Digital Playback Volume). The volume_scale is cubic, even better than log but what about min and max volume? Back to our previous calculations, what do we want it to be at full volume? It might depend on some additional settings. I don't want the 0.8 dB Analogue Playback Boost enabled and some say the HiFiBerry DAC+ sounds better with the -6 dB Analogue Playback Volume enabled.

My preamp got a gain of 6.92 dB. Add the -6 dB from Analogue Playback Volume and we are left with 0.92 dB. 2.1 V amplified 0.92 dB = 2.33 V. -3.82 dB is needed to bring it down to 1.5 V (the input sensitivity of my power amp). -4 dB will add some headroom. Open a terminal and logon to your Raspberry Pi.

ssh pi@192.168.1.10

Delete any any persistent settings for the ALSA mixer with the following command:

sudo rm /var/lib/alsa/asound.state

Start the ALSA mixer with the command:

sudo alsamixer

Set the Analogue Playback to -6 dB.


 Set the Analogue Playback Boost to 0 dB.


 Set the Digital Playback to -4 dB and note that -4dB equals 86 % in ALSA mixer.


Exit ALSA mixer with ESC and save these settings globally with the command:

sudo alsactl store

We now know that 86% on the ALSA mixer represents -4 dB. And that the Analogue Volume Control will add another -6 dB. 2.1 V output attenuated 10 dB equals 664 mV. My preamp will amplify it 6.92 dB bringing it up to 1.47 V. We can also use the ALSA mixer to figure out that 9% represents -61.5 dB and 8 % represents -60.5 dB. So max volume should be 86 % and min volume 8 % to get a 60 dB volume range.

Back to the [alsamixer] section of the Mopidy configuration file. It should then read something like:

[alsamixer] 
enabled = true
control = Digital
min_volume = 8
max_volume = 86


We should also add alsasink as output and alsamixer as the mixer in the [sound] section. Mopidy will startup with the volume set to 100 % by default. This value can be set with the mixer_volume. I usually set it 20 % to be on the safe side if the attenuator on my preamp happens to be set to 0 dB.

[audio]
output = alsasink
mixer = alsamixer
mixer_volume = 20

Open your Mopidy configuration file for editing with the command:

sudo nano /etc/mopidy/mopidy.conf

Update the configuration with:
  
[audio]
output = alsasink
mixer = alsamixer
mixer_volume = 20

[alsamixer] 
enabled = true
control = Digital
min_volume = 8
max_volume = 86


Save and exit with Ctrl-X followed by Y. Restart Mopidy with:

sudo systemctl restart mopidy

You can now start the ALSA mixer again and see how the volume changes in real time as you change volume in Mopidy. I was a bit surprised when full volume in Mopidy only registered as 83 (-5dB) and not 86. The same for no volume in Mopidy registered as 10 (-59 dB) in ALSA mixer. So I tried a couple of different values. The values representing max -4 dB and min -63 dB turned out to be:

min_volume = 6
max_volume = 90


Next up is shairport-sync. The settings of interest in the configuration file are:

alsa =
{
     mixer_control_name


general =
{
   
volume_max_db
    volume_range_db
}


The mixer_control_name will tell shairport-sync what mixer to use. The volume_max_db the max volume and the volume_range_db the range of the volume control from max and down. The volume_range_db value is 60 by default so it can be omitted.

Open your shairport-sync configuration file for editing with the command:

sudo nano /etc/shairport-sync.conf

Update the configuration with:

alsa =
{
     mixer_control_name = "Digital";


general =
{
   
volume_max_db = -4.0;



Save and exit with Ctrl-X followed by Y. Restart shairport-sync with:

sudo systemctl restart shairport-sync

You can now start the ALSA mixer again and see how the volume changes in real time as you change volume from your AirPlay Transmitter. Max volume seems to work but I could not get  the volume range to work. The min volume always stayed = 2 no matter what value I used for volume_range_db.

You can also try to switch the other two mixing interfaces; Analogue Playback Volume and Analogue Playback Boost Volume, on and off while in the ALSA mixer to see what sounds best with your gear.

You should now be all set but as you have seen, there are loads of settings to be used and tweaked. I hope this series of posts can inspire you to build and optimize a music streamer to your liking.