PAB: a Personal Audio Box

4.8K347

Intro: PAB: a Personal Audio Box

The idea for this project was born from the need to scrape the three large components of the HiFi system, which had now reached the end of their life. In addition, I needed more space in the shelf for other objects, so I took the opportunity to start studying at a Personal Audio Box to replace all functions of the three vintage "giants".

A Raspberry Pi3B+ seemed to be the best choice for these reasons:

  • Small form factor and low power consumption;
  • An audio PCM output with acceptable quality;
  • Availability of mopidy, an extensible music server implementing mpd protocol;
  • High integration of sources: local music, CDROM, radio streams, Spotify, Tunein, etc.

Integrating it with few other components, I was able to create a complete and headless system, capable of playing music from CDs, local files, online radio, Spotify playlists, podcasts. And through the use of a frontend, I can now manage all its operation from any device connected to the LAN (smartphone, computer, tablet).

STEP 1: Case and Layout of Components

The first problem I faced was selecting and finding a suitable case. Finding nothing at home, I found this cheap DVD player on Amazon for a few dollars, but anything similar will be good enough. The case has these dimensions: 27cm x 20cm x 3.5cm.

I completely removed all the content, keeping only the small board to manage the front LED, the power button and the USB input. Then I planned the internal layout for the new components (see picture).

STEP 2: The Audio Stereo Sensing Switch

Why an automatic audio switch? The need arises from the fact that I often listen to TV through the HiFi amplifier, but I didn't want to select the source switch on the amplifier every time. With this circuit, the amplifier input is always the same, and the source is automatically selected by the Audio Stereo Sensing Switch.

The schematic is straight-forward. When the PAB is not playing, the audio source to the HiFi is coming from the TV. If the PAB plays, the relay selects audio from Raspberry.

STEP 3: The Super-Capacitors Box

As known, a sudden interruption of the power supply to the Raspberry causes the immediate power off without the execution of the shutdown procedure, risking to compromise the operating system and therefore its total functionality. A supercapacitor differs from a traditional capacitor in two essential characteristics: its plates actually have a larger area and the distance between them is much smaller, as the interposed insulator works differently than a conventional dielectric. With these techniques, very high capacity (in the order of several tens of Farads) capacitors can be made while maintaining small dimensions. The idea is therefore to create a 5v "buffer" via supercapacitors and to activate shutdown when the absence of the supply voltage is detected. In this way, it will no longer be necessary to manually intervene to launch the shutdown, but simply remove the plug (or activate a switch) to ensure a safe shutdown.

Referring to the schematic, the power supply is applied to the left terminal and the Schottky diode prevents any return of current to the power supply. The two 1.2Ω 5W power resistors in parallel limit the charge current of the supercapacitors, to protect the power supply. Without these resistors, the peak current required by the two discharged supercapacitors would almost certainly be able to damage the power supply. The power diode must necessarily be of the Schottky type in order to insert a minimum voltage drop in series with the 5V bar.

The two supercapacitors are connected in series to ensure a maximum voltage of 5.4 volts at their ends (each supercapacitor is 10F, 2.7V) and the two resistors in parallel to the capacitances balance the charging currents and guarantee a slow discharge when the Raspberry is turned off. The two 1KΩ resistors parallel to the input divide the 5V of the power supply in half to take the necessary signal to detect power failure (connected to Raspberry GPIO 7). Unlike modern lithium cells, supercapacitors guarantee an almost infinite number of charge and discharge cycles, without losing any characteristics.

The circuit will therefore be able to keep the Raspberry powered and functioning for the time needed to perform a regular shutdown. The start of the shutdown process will be detected by a program running on the Raspberry which will monitor the status of the GPIO 7, to which the power level is connected. When the power is disconnected, the GPIO pin 7 passes at a low level and triggers the shutdown. This is the code:

#!/usr/bin/env python
import RPi.GPIO as GPIO
import subprocess
GPIO.setmode(GPIO.BCM) # use GPIO numbering
GPIO.setwarnings(False)
INT = 7    # pin 26 monitors Power Supply
        # use a weak pull_up to create a high
GPIO.setup(INT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def main():
    while True:
        # set an interrupt on a falling edge and wait for it to happen
       GPIO.wait_for_edge(INT, GPIO.FALLING)
        # check the pin level again
        if GPIO.input(INT) == 0:
            # still low, shutdown Pi
           subprocess.call(['poweroff'], shell=True, \
               stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if __name__ == '__main__':
    main()

The program must be saved in /usr/local/bin/ .py and configured to run when the Raspberry starts. From tests carried out, the capacities of the two supercapacitors have proven to be sufficient to ensure the shutdown time for the Raspberry. If more time is needed, it will be sufficient to introduce two other supercapacitors in parallel to the existing ones, or replace them with two of greater capacity.

STEP 4: Assembling and Use of USB Ports

The Block Schematic shows how to connect the several devices for PAB on the main 3 bus (+5v, USB and audio stereo).

Note that the CD reader power supply has been connected directly to the main Power Supply through a "Y" cable, while the audio input goes to the Raspberry. The four USB Raspberry ports have been used for:

  • CD reader;
  • a 250GB pendrive to store local music files (mp3, m4a, wma, flac, etc.);
  • a 16GB micro SD-card (with USB adapter) to store the full backup of the main Raspi SD (see below);
  • a connection to the external USB port on the case.

The external USB port can be used to play external music or to power external devices. In my case, I am powering an external Bluetooth transmitter as I have discarded the Raspi's internal one due to low range and instability. With the external bluetooth I am driving 2 different stereo speakers at home.

The 16GB micro SD card (with USB adapter) is holding a full Raspberry backup. I am using rpi-clone, which has revealed to be a very good project that allows to have a full working backup of the Raspberry without the need to remove the internal SD. I have swapped many times this SD with the internal one, without any problem. So I have setup a cronjob for root user:

#Backup on sda - each Wednesday night

15 2 * * 3 /usr/sbin/rpi-clone sda -u | mail -s "PAB backup on SD - done" <myemail@mydomain.com>

I have then re-used the original power button on the case to shutdown and restart the Raspberry, following this guide: https://howchoo.com/g/mwnlytk3zmm/how-to-add-a-pow...

STEP 5: Software and Operating System

The main operating system of PAB is a plain Raspbian minimal (Debian Buster) with several specific additions:

  • rpi-clone for main backup;
  • ssmtp, a simple MTA to get mail off the system;
  • udevil, to allow automount of USB drives;
  • abcde, to grab my CD collection and compress it to any audio format;
  • mopidy, a full Music Player Daemon with a bunch of plugins.

I have then written a full PAB Scheduler server application using python3 and tornado, whose code is out of the scope of this article, but I can provide instructions on request. With the Scheduler you can setup playlists for any time of your day, differentiating weekdays from weekends.

The main software running PAB is mopidy. For installation and configuration of mopidy (quite extensive) please refer to its documentation here: https://docs.mopidy.com/en/latest/

These are the installed plugins:

  • Mopidy-Alsamixer
  • Mopidy-Internetarchive
  • Mopidy-Local-Sqlite
  • Mopidy-Podcast
  • Mopidy-Scrobbler
  • Mopidy-Soundcloud
  • Mopidy-Spotify
  • Mopidy-Spotify-Tunigo
  • Mopidy-Cd
  • Mopidy-Iris
  • Mopidy-Local-Images
  • Mopidy-TuneIn

In order to get full control of PAB, I have choosen the Iris frontend extension (see pictures). This is a very powerful web application with following features:

  • Full web-based interface controls for Mopidy
  • Improved support for local libraries (powered by Mopidy-Local-Sqlite)
  • Browse and manage playlists and tracks
  • Discover new, popular and related music (powered by Spotify)
  • Freely hosted
  • Integration with:
    • Spotify
    • LastFM
    • Genius
    • Snapcast
    • Icecast

In this way, I am free to control my music from almost anywhere (computer, tablet, smartphone).

7 Comments

AWESOME!! For the build itself, but especially for giving me a solution to what's been bugging me for a while, now: how to safely power down RPi. I need a bit of help, though, to make sure i understand correctly:
1. The RPi is now powered through the super capacitor setup, via P1-02 and P1-25, right? Or does it still require the microUSB power supply? (i just googled a bit, and learned you can actually power it directly)
2. The case power button is not really necessary for the shutdown, right? basically, when you unplug the main power supply, the RPi shuts down due to the code running in the background, correct?

I'm building an arcade cabinet for my daughter, and i've been searching for an elegant solution for shutting it ALL down (RPi, monitor, speakers etc) from a single push of a button. Cause she's 6, and couldn't be bothered to go into the menu and properly shut down emulationStation and RPi, wait 30-60 seconds, and only THEN turning the switch off. :)

Awesome design, nicely explained, diagrams and all.. I'm impressed, I've favorited this and will download the pdf, just to make sure i don't lose the information within.
Hi John, thank you for your kind words!
Yes, you are right on both points: RPi is powered via P1-02 and P1-25 and you don't need the micro USB power. The case power button was there before I had the idea of the supercapacitors, and it was used to shutdown and wakeup the RPi. You don't need it for your project. I will be glad to help you more if you need.
Thanks,
Fabio.
+1 for recycle DVD casing (instead of making/buying new)
You replaced your dual cassette deck with this? I'm calling BS on that picture. You added that component to the scene to be dramatic! :-)

Great project! This gives me a lot of good information for something I'm planning to do. I particularly like that you wired the button on the case to shut down the pi. Reasonable power control is something that's missing from most headless Raspberry pi DIY projects.

If I were to make one suggestion, I would highly recommend spending the extra money and using a Raspberry pi 4 (actually, the 2GB pi 4 is the same price as a pi 3B+ on Amazon right now, so there's no compelling reason to buy a new pi 3). You are likely to run into show-stopping I/O latency on a pi 3 (or earlier) when heavily using two or more USB ports simultaneously. The problem will be made worse if you are also using wired ethernet, which utilizes the USB system bus. I've had that experience multiple times with different applications - "running a media server" being one of those applications, and "backing up a Raspberry pi" being another.

Thanks for sharing!
Thank you for your comment.
Regarding the cassette deck, there were no reason to keep them. I have "recorded" (stripped) most of my CDs into .flac files and if I need music recording, I can always do it with PAB with the proper configuration.

At the moment I am not running into any latency issue, but I will consider your suggestion for Pi4. Thanks
Dang! That's a ton of space saved :)