PAB: a Personal Audio Box

by fmarzocca in Circuits > Raspberry Pi

4996 Views, 34 Favorites, 0 Comments

PAB: a Personal Audio Box

Main.jpg

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).

Supplies

  • Raspberry PI3B+
  • An old DVD case
  • CDROM reader
  • 5v-5A power supply
  • Supercapacitors
  • Various components (transistors, LED, relay, Op-Amp): see project details

Case and Layout of Components

Schermata 2020-01-11 alle 11.53.21.png
layout.jpg
20190810_173408.jpg
hifi.jpg

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).

The Audio Stereo Sensing Switch

ASSS.jpg
20190216_174636.jpg
Schematic_Audio-Stereo-Sense-Switch_A-Auto-input-Switch_20190214160744.png

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.

The Super-Capacitors Box

SC1.jpg
schema.jpg

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.

Assembling and Use of USB Ports

block_schem.jpg

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...

Software and Operating System

20190810_173331.jpg
Schermata 2020-01-11 alle 14.53.07.png
Screenshot_20190319-171052_Chrome.jpg
Screenshot_20190319-171153_Chrome.jpg
20190810_173408.jpg
20190321_135047.jpg
20190321_130831.jpg
20190321_104812.jpg
Schermata 2020-01-11 alle 15.05.31.png

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).