When using the HiFiBerry Amp2 with Retropie on the Raspberry Pi, volume control is sadly lacking. So after a lot of trial and error, I’ve developed the best way to assign buttons or keys to change the volume. One issue I ran into was this volumecontrol::init() – failed to find mixer elements Volume Control error. After learning how the HifiBerry communicates with Retropie and how to setup TriggerHappy, I managed to fix this completely!
TriggerHappy is a tool included with Retropie that monitors key or button presses and then runs a command. The first thing we need to do is slightly modify it slightly. So let’s get started!
Let’s Get TriggerHappy!
Once you have booted up Retropie, you need to press F4 on the keyboard to get to the command prompt. Here, we need to open the TriggerHappy script and modfy some code.
sudo nano /etc/init.d/triggerhappy
Inside the script, we need to look for the line that starts with DAEMON_ARGS and change –user nobody to –user pi like here.
DAEMON_ARGS=”–daemon –triggers /etc/triggerhappy/triggers.d/ –socket /var/run/thd.socket –pidfile $PIDFILE –user pi /dev/input/event*”
Exit and save by pressing Control X, then Y and then press ENTER.
What Button Do I Press?
Next we need to determine what the key or button name is so we can reference it later. We do this by using the triggerhappy hotkey daemon tool or thd by executing the following command.
thd –dump /dev/input/event*
You’ll be left with nothing until you start pressing some keys or buttons. In particular, my Arcade Joystick returned the following results.
EV_KEY BTN_PINKIE 1 /dev/input/event0
EV_KEY BTN_PINKIE 0 /dev/input/event0
EV_KEY BTN_TOP2 1 /dev/input/event0
EV_KEY BTN_TOP2 0 /dev/input/event0
Broken down, BTN_PINKIE is the name of the button. The number 1 represent a press and the 0 represents a release. What we really need here is the button or key name.
Getting Amixer to Talk to HifiBerry Amp2
Next we need to determine the way to tell the HifiBerry to change the volume. Generally this is refereed to as PCM. When using add on boards or USB Audio devices, this will need to change as PCM is generally referring to the audio interface of the Raspberry Pi. So to figure out what term to use, we need to launch the Amixer command.
This will probably spit out a huge load of different information. To go up and down the list you can hold down SHIFT and press PAGE UP or PAGE DOWN. In the case of the HifiBerry Amp2, the line that stood out was as follows.
Simple mixer control ‘Digital’,0
From this we can determine that the term we need to replace PCM with is Digital. Great! Almost there now.
Setting up Retropie Volume Control
Lastly, we need to create a trigger file for registering volume up and volume down.
sudo nano /etc/triggerhappy/triggers.d/audio.conf
In this new file, we need to refer to the button, it’s state and the command to run. Before we start though we can also break this down a little further. The state of 0 refers to no button press, 1 refers to button down, 2 refers to a held button down. The command to run is amixer and we will set the Digital
BTN_TOP2 1 amixer set Digital 2%+
BTN_TOP2 2 amixer set Digital 2%+
BTN_PINKIE 1 amixer set Digital 2%-
BTN_PINKIE 2 amixer set Digital 2%-
Everytime the BTN_TOP2 is pressed it will increase the volume by 2%. And by pressing BTN_PINKIE, the volume will decrease by 2%. None of this will work though until we do a full reboot.
Exit and save by pressing Control X, then Y and then press ENTER. And now we can reboot Retropie.
Testing Raspberry Pi Volume Control
Once you rebooted, you can test it directly from the command prompt by running the command we used in the trigger file we created. Press the volume down key or button a few times and we can get the level of volume.
amixer get Digital
Press the volume down button a few more times and run that command again to make sure the levels have changed. Otherwise, you can test the new Retropie Volume Control by entering a game. Let me know if you run into any issues.