Side note: This build actually happened two or three years ago but I can’t recall exactly when, so I decided to use the date of writing instead.
When I had my first “real job” (as a working student, that is) I decided to treat myself and splurged 100 Euros on a pair of old Canton passive speakers to replace the ones I found in my parent’s basement. Naturally, sound-wise they were a vast improvement so I listened to music quite often. They’re driven by an old 80’s Onkyo receiver I placed a few meters away from my desk. That means I had to get up, turn the thing on, wait for the relay to click, switch to the right input and go back to my desk to start the music on my computer. Obviously, the same routine was required whenever I wanted to go back to silence again but usually I was too lazy and just kept it on without playing anything, sitting there for hours, wasting power.
Initial ideas
For that reason — and because I felt like tinkering — I thought about how to automate that procedure. The simplest solution would have been to just get a different receiver with an in-built remote control but I wanted to keep that one and all I needed was a way to switch it on and change inputs anyway. So I opted for the second obvious choice to build something myself.
Ordinarily I would’ve used an Arduino two switch those few GPIOs but I wanted an easy way to control it via my local network and I remember the WiFi and Ethernet shields to be quite costly. Like any respectable person I always have a few unused Raspberry Pis lying around and using one of those has the additional benefit of having an on-board sound card that I can connect to the amplifier.
To play music from the Pi, I chose to use pulseaudio as it supports streaming audio from clients to a server (Pi) via the network. This solution is heavily dependent on network quality and can be quite laggy via WiFi. Since I want to listen to my own music most of the time anyway, I’m using MPD. It also features those newfangled music streaming services — like Spotify — along with various plugins for online radio and such.
Hardware
To switch the receiver on and off I connected a relay (SPST) in parallel to the on/off switch. This way, I can still turn the amplifier on with either the relay or the power switch without interference between the two. Switching relays directly from a GPIO will most likely burn out that pin as the current drawn is higher than the 16 mA limit. To avoid that I’m switching a transistor via GPIO and that transistor — supplied by the Pi’s 5 V rail — in turn switches the relay.
As mentioned above, the default input after the receiver is switched on is always “Tuner” which in the age of digital and internet radio doesn’t make a lot of sense anyway. To be able to switch between the inputs remotely, I’m using an opto-coupler that shorts the buttons to ground, just like the actual tactile switches would. Again, they can still be used as if nothing changed inside the device.
Software
With the hardware side sorted, the software side is merely a matter of switching the corresponding GPIOs for certain events. Whenever something is playing, the relay is turned on and the input is switched to “CD”. Whether the sound card is currently playing something or not can be read from the respective state file (at least for pulseaudio). This approach is nice, as it’s agnostic to the actual audio player used. If the audio is stopped, a timer runs for a set amount of time — 5 minutes in my case — until the relay is switched off. Essentially, that’s everything there is to it, but I added a few minor things.
When the receiver is switched on, it “warms up” until the speaker relay clicks which takes around three seconds. If I just press play in the client software, I’d miss those three seconds at the beginning of the first song. Hence, I also monitor the MPD daemon log for client connections and also switch the relay if a client connects. Apparently, some clients don’t actually connect to MPD which means they won’t trigger the log message. I should look into a workaround. Using this client connection feature works a treat when using the smartphone, but if the client app is opened when unlocking the phone, the amplifier will turn on for another five minutes which is unnecessary if I stopped the music a few hours ago. So I added a second, shorter timer of one minute that “catches” those accidental connections but still leaves me enough time to decide on something to play, if it wasn’t an accident.
Done
Overall, I’m rather pleased with the result. It’s working fine and didn’t take very long to build. The only thing I wanted to add for a while now is remote volume control. Of course, I can adjust most of it via software from the Pi but sometimes even that is too quiet. I think adding a servo or a stepper motor to turn the volume knob from the inside ought to work just fine.
You can find the schematic and code for this project in the corresponding git repository.