📜 ⬆️ ⬇️

Distributed Music Player on Raspberry Pi


I have long planned to write this post, but progress was slow. To accelerate the pace I pushed this question , where I had the imprudence to say that I was writing such a post, as a result of which he aroused a keen interest, so I had no choice but to fulfill this promise, for which I express special thanks to the author of the question M03G , with whose filing and I received this accelerating Pendel. And since the post seems to be released on Friday, I allowed myself a rather loose (sorry, involuntary pun came out) writing style. I hope everyone will be satisfied.

(Yes, the post largely overlaps with the post Distributed audio player on the Odroid U2 , but a little easier to set up)


Prologue


I began to dream of a similar system in 2003, as soon as I started living in an apartment with a number of rooms> 1, and the question of music in the kitchen (and indeed in the whole apartment) rose to its full height. The situation was aggravated by the fact that the apartment was built according to the architectural genius of Nikita Sergeevich Khrushchev, and the computer was placed in the “far” room, so that the kitchen could be heard a little more than nothing at all.
')


As time went on, the apartments changed, pondered and discarded various options, the dream remained, now going to the fullest background, now emerging from the depths of the subconscious, but did not move.

image


Prehistory


By the time the wonderful Raspberry Pi single-board computers turned out to be in wide access, I changed 3 places of residence, the same amount of work, as well as the country of residence, therefore, because of the abundance of what is happening, the dream did not give voice to even a weak voice, having fallen asleep to dusty closet in the back of the memory. Malinku I, however, still ordered. I didn’t understand myself why, but I couldn’t resist: I decided everything for the price / potential ratio. “This is in the perspective of both NAS, and a web server for experiments, and a lot of everything else,” I thought (well, on the NAS, as it turns out , it doesn’t pull, but even if I knew it then, I wouldn’t care it did not stop).



However, after the “Malinka” did get into my hands, I just had a blockage both at work and with personal projects, so the “berry” lay on the shelf and lay for several months without taste of electricity.

image


But one day everything changed.

Outset


There was nothing to do on a warm summer evening, something that had not happened to me for several years, and I decided not to miss this opportunity and to sort the trash in the cupboard a little. There I came across a black box, sadly looking at me with slots of empty slots.
“Both on, yes I have something here!”
- I thought and immediately got into the Internet for the image of the system for it. It so happened that she lay down on the table in such a way that the audio output stared straight at my left eye, asking a dumb question
“Well, shall we do something or what?”
The plan matured quickly: we plug an external screw with music into the usb port, connect speakers to the audio output, put MPD on the raspberries and get an excellent music player that can be controlled from any computer in the house (there are plenty of clients for any OS). So, Malinka finally found a use, it blinked merrily with multi-colored LEDs, and it was good.

Good but not enough.
“Heh, there is such a wonderful thing like Icecast2 that gets along excellently with MPD and stream music to the net!”
- flashed through my head. And then it will be possible to listen on any device with any player who can grab an audio stream from the network. These thoughts burst out so loud (I would even say deafeningly) that the menacing rumble reached the forgotten dusty closet and woke up a hopelessly asleep dream, which immediately began to hollow into the wall with a tin mug:
“And who bothers you to take another couple of raspberries, write the simplest software that, on command from the network, turns on / off the same mplayer to play the specified URL (the same one to which Icecast2 broadcasts from the first raspberry), and release me from here at the end ends ?? ”
But here I (and her) waited for the first bummer (as it turned out later - for the better): the power of the raspberry was not enough to pinch the stream on the fly and stream it - the sound stuttered. I climbed to look for whether it is possible to “persuade” Icecast2 not to pinch, but directly stream what is being fed to it. And while smoking the MPD config, I came across the familiar word “pulseaudio”.
“So, stop, yes, this is the kind of crap that can potentially play on the network? Or maybe I don’t need to make a fuss with Icecast2 and a samopisnyi on / off switch? ”
And so it happened ... And the cabinet remained unassembled.

Go!



image


So let's go. Source material: two or more Raspberry Pi (one will be the server on which MPD is spinning, the rest are clients), the same number of speakers for playing sound, a bit of Linux knowledge, and an evening of free time. Well, of course, a music collection with correctly spelled tags, since MPD operates with them. I had all this available, so I informed my wife beforehand that I was not there the next evening, I fell ill, disappeared from home, went to Vancouver, and emigrated nafig in general from this planet, and that I would come back tomorrow for dinner, and head plunged into Google. Google did not disappoint.

Preparatory stage (same for server and clients)


I will not describe in detail the process of installing the system on the Raspberry Pi, the instructions on the Internet are complete. I will only make a reservation that I, as a Debian lover with 8 years of experience, used Raspbian , so all instructions will be given for him. In general, download the image, flash, stick the card, load. The initial setting is also standard, I will list what I did:


If you are not very confident in the console, then you can skip the last two points, and configure it directly from the “crimson” gui, but I did everything by logging in via ssh.

Mandatory item: assign a static address to each “raspberry” in the settings of your router (or in its own network settings, no difference) - later it will be very useful.

Well, let's get started:

1. First of all, we will update the system to a fresh state and install PulseAudio. In general, we will now do a lot of things from the root, so go under your account in your favorite way ( choose your own taste ):

  $ sudo -i
 # aptitude update && aptitude upgrade && aptitude install pulseaudio 


2. Since it is assumed that everything will work immediately after the download, ideally - without X and the user's login, we need PulseAudio to work in system-wide mode. To do this, edit the file / etc / default / pulseaudio as follows:

  ...
 PULSEAUDIO_SYSTEM_START = 1
 ... 


3. Next, in order for everything to work, you need to add user pi to the pulse-access group:

  # usermod -a -G pulse-access pi 


4. Now enable the reception of sound over the network. To do this, add the line to the /etc/pulse/system.pa file

  ...
 load-module module-native-protocol-tcp auth-ip-acl = 127.0.0.1; 192.168.1.0/24
 ... 

Here we authorize both “himself” and all devices from the local network with addresses 192.168.1. * (If you have another address range, correct it accordingly). In principle, the first part is needed only for the server (to play on itself), and the second - for clients (to receive from the network), but we will do both, so that if something happens, the raspberries are interchangeable with minimal gestures.

4. Actually, we start the pulseaudio service

  # service pulseaudio start 


This is where the common part ends. Let's go to the settings of clients and server.

Customer


Perhaps, it will seem strange to someone, but “the client is already ready”, and no additional configuration is required, so we will continue to move strictly to a server of the order of 50 meters and work on the server.

Server


First of all, you need to install MPD on the server, and at the same time a console client (for initial setup):

  # aptitude install mpd mpc 


Now edit the config /etc/mpd.conf . He, like many other configs in Linux, is well commented, so I’ll mark only the main necessary edits:

Specify the path to the collection
  music_directory "/ mnt / disk1 / music / mp3 / sorted" 


Bind on any network interface:
  bind_to_address "any" 


To standard port:
  port "6600" 


Auto-update the collection when files change:
  auto_update "yes" 


And most importantly, we configure audio outputs. Here we create one output for each of the client “Malines”, plus one for the server itself - it will also act as an equal client. This is where the static IP addresses that we assigned to each of the raspberries will come in handy.

  audio_output {
         type "pulse"
         name "Office"
         server "localhost" # server
 }

 audio_output {
         type "pulse"
         name "Living Room"
         server "192.168.1.4" # client # 1
 } 

(and so on, by the number of existing customers)

Well, restart MPD. You can end the root session at this point.

  # service mpd restart
 # exit 


Scan the collection to create a database (it will take some time, depending on the size of the collection):
  $ mpc -w update 


Actually, this is all.

Rulim!


It remains nothing: how to manage this whole business. There is nothing complicated here, at our disposal a wagon and a small cart of customers for various operating systems. When choosing, be sure to pay attention to whether the client is able to switch output channels (or, as they are also called, Output Devices) - in fact, this is exactly the feature that allows you to turn on / off a particular client. Under Mac I stopped at Theremin , under Linux - at Gmpc , I didn’t have to use Windows clients, so I can’t advise anything.

image


Climax


With light and smooth finger movements, gently touching the touchpad, I started Theremin, added the first track I got to the playlist and pressed Play. A second from the speakers in the hall came the gentle play of the good old composition “Master of Puppets” of the vocal-instrumental ensemble “Metallica”. The child sitting inside of me exulted -
"Hooray!!! This is what I dreamed of for 10 years !!! ”
Additional degrees of spiritual warmth were generated from the awareness of the fact that no “rocket science” was involved, and everything was assembled literally in the evening from “improvised materials”, and even without a single piece of blue electrical tape. I listened - from the office upstairs also came the sound! I climbed into the menu and took a daw off with the “Living Room” - the sound from the speakers in the hall was gone, and remained only in the office.



Victory!

image


Epilogue


After a few minutes, the internal perfectionist demanded unification of the interface between the devices and the AXES, and so the crazy thought immediately flashed through my head “But I want a web interface, so that it has all the necessary functions!” but short. This kind of fichasty, but put through the opu. This one is generally abandoned. ”And so on. Not to mention the fact that they all had one fatal flaw . In general, the most intelligent readers have already understood everything: I decided to write my own, “with musical scores and pianists”. Especially since I never really worked on web development, but I always wanted to try the jQuery scary beast, and generally appreciate how far web technologies have gone since I last wrote and threw my hamster on narod. ru, with javascripts and rotating menus. Therefore, for three week evenings and one day off, this miracle was born:

image

(screenshot taken from the screen of the BlackBerry PlayBook)

But about it - next time.

UPD: web interface link: github.com/Mezomish/WebMPC

Source: https://habr.com/ru/post/195458/


All Articles