📜 ⬆️ ⬇️

Distributed audio player on Odroid U2


Once I noticed that I spend quite a lot of time in the kitchen, where the sound from the speakers located in the room comes to bad. And then I wanted to make a good silent player that can simultaneously play music in several rooms. Of course, the problem could be solved by simply turning the volume control, but this method was discarded as inhuman to the neighbors. Another important point was that I wanted to use the same speakers to output sound from the computer. Why should I have several sets of acoustics in one room?
The post was quite long, as I tried to pay attention to the considerations for which I chose this or that solution.

Iron




Soft


Synchronized playback


Programs that can synchronously reproduce sound very little. Our ear can discern the slightest deviation and we begin to hear an echo. Pleasures as much as from announcements of arriving trains through the speakerphone at the station in the outback. Two sound systems show good results: jack and PulseAudio . They say that squeezebox still works well. I also tried mplayer and vlc, but I didn’t get acceptable synchronicity from them. Vlc copes well with the video stream, if you use RTP multicast , but the sound has a noticeable echo. And with video on demand , there is no synchronization at all, since each client receives a separate stream. Mplayer just terribly slow, if udp sync is enabled.
So, if there is no need to display video synchronously, then jack or PulseAudio are a good choice due to its flexibility. If you need a video ... let me know if there is something really working.

Jack vs PulseAudio


After a long search, it turned out that there was no struggle between them: they were written for very different purposes. I do not need very short delays and tremendous flexibility in switching threads while working in order to listen to music. But I want to load less CPU and not to drive packets over the network, if nothing is played. All I need is to turn on and off the pre-defined outputs. Therefore, in my case, PulseAudio is the preferred choice.
')

PulseAudio


Hint: First you need to set the clock correctly. If they are badly synchronized between devices, strange bugs can occur. Therefore, ntpd should be installed everywhere.
You can set up synchronous playback in several ways. You can use module-combine-sink with module-tunnel-sink, or you can use broadcast rtp streams. I chose module-combine-sink, as it gives a much smaller cut. But it does not work via WiFi, and rtp can be. But I do not like WiFi, I love the wires.
Another subtlety is that PulseAudio can work either as a system-wide daemon, or for each user separately. The latter is the preferred solution , mainly due to security concerns, for example, so that users cannot disconnect or redirect other people's flows. But if we are talking about a client without X11, then only system mode will remain. And security problems ... so this is exactly what I need: a common sound server for several trusted users.
If you selected the system mode, then do not forget to add yourself to the pulse-access group. Configuration files are located in the / etc / pulse / directory. For system mode, this is system.pa , and for user default.pa .

Output devices


These are devices to which amplifiers are connected. They need to allow remote access, so add the module-native-protocol-tcp to the config:

load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;192.168.0.0/16 


Input devices


On the device from which the audio stream will go, you need to configure the remote outputs. For each output device (except for local outputs) we add the following lines:

 load-module module-tunnel-sink server=<output device address> 


Now you need to learn how to turn them on and off dynamically. PulseAudio sends data even to muted channels, but does not send to the outputs that are disabled. In order not to turn off the audio output for all applications, we will create intermediate outputs for each real output:

 load-module module-combine-sink sink_name=kitchen_mpd slaves=tunnel-sink.u2k.home load-module module-combine-sink sink_name=hall_mpd slaves=alsa_output.usb-ESI_Audiotechnik_GmbH_Dr._DAC_nano-01-nano.analog-stereo 


And, finally, combine these outputs into one:

 load-module module-combine-sink sink_name=mpd_sink slaves=kitchen_mpd,hall_mpd 


Now, after restarting PulseAudio, you can turn on and off individual outputs with commands like this:

 #  pactl suspend-sink hall_mpd 1 #  pactl suspend-sink hall_mpd 0 


Hooray! A tunable output that synchronously reproduces sound through several output devices connected via a network is ready.

Music reproduction


Now you need to connect any player to this output. I chose MPD, since it can be remotely controlled from almost any device. This is convenient when listening to music in the kitchen, and the computer is in the room. The setup is very simple:

 audio_output { type "pulse" name "Pulse" server "u2.home" # optional sink "mpd_sink" } 


But if there are several outputs, I still want to control them via the MPD interface. I wrote a small hack for MPD that allows you to do this. This is a modified NullOutput that executes arbitrary commands when turned on and off. It remains to configure these outputs for the corresponding pactl calls and hide the main (first) output with another small hack.

 audio_output { type "exec" name "" enable "pactl suspend-sink hall_mpd 0" disable "pactl suspend-sink hall_mpd 1" } audio_output { type "exec" name "" enable "/usr/bin/pactl suspend-sink kitchen_mpd 0" disable "/usr/bin/pactl suspend-sink kitchen_mpd 1" } 


Sharing


On a stationary computer, I registered in /etc/pulse/client.conf
 default-server = u2.home 

As a result, all applications are connected to a remote sound server directly. It works flawlessly.

Problems


The main problem faced was unreliability. If the network connection is lost, PulseAudio deletes the corresponding outputs and does not re-create them. It is necessary to restart manually.
Another problem is that the status of the outputs in the MPD and PulseAudio may not coincide. You have to make a button once more to turn the sound on or off.

Virtues


I can listen to music at breakfast and at dinner. Moreover, the music is well audible in most parts of the apartment and does not play softly or loudly. Optionally, individual zones can be turned off. To manage it is easy and simple. In general, the cat approves:



PS The specific combination of dr. DAC nano and odroid U2 is not very successful. With a high probability, this sound card will work only through a USB hub due to insufficient capacitor capacity on the VBUS at U2.

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


All Articles