Thursday, December 22, 2016

Ubuntu Streaming your PC Laptop audio stream alsa to another LAN connected device

Ubuntu Streaming your PC Laptop audio stream alsa to another LAN connected device


I constantly like to change where in the room I sit for some computing. I get bored too easily in one place so I keep switching places. But, sadly, my sound system isnt wireless. So, how to fix this issue? I wanna move around but still want all my songs keep sounding through my central surround sound? WiFi. Yep.
With the help of the amazing power of Linux, its too much cute sound architecture ALSA and a little SSH, I stream my whole soundcard feed to another "Old, and slow" laptop that stays with my sound system. I tried Pulseaudio, but it didnt quite get me where I wanted to be. I just use it for selected which feed goes to the other laptop and which stays native.
Stictly, Ubuntu instructions
Requirements
For this work, you need
1. An extra Linux machine (Raspberry Pi/another Laptop/Netbook anything that can run ssh-servr)
2. WiFi network setup (Router, tethered from Laptop etc) 3. A little bug fixing here and there if this tutorial becomes too old

First of all, lets install the following packages
pulseaudio paprefs pavucontrol sox [on the machine that sends the sound data]
openssh-server [on the machine that receives sound data]
Im assuming both laptops have working sound system setup.

Then, on the streamer (the machine from which well send the sound) run this command

modprobe snd-aloop index=0 pcm_substreams=1
This is a module you must load in order to get a loop back device in your soundcards list. What this device does is put all your audio data so that any device can pick that from there. In this case my Laptop connected to the speakers.
If you happen to fail to load this module, check for some instruction on the internet. I found some tutorials that may help you to resolve issues. These are two good tutorials

http://confoundedtech.blogspot.com/2012/08/building-installing-alsa-loopback.html
http://www.sm5bsz.com/linuxdsp/install/snd-aloop.htm
To be able to load this automatically on each boot just add this to

/etc/modules

Now, when your modules is up, you should be able to see a loop back device in your "Sound Settings" that is called "Analog Output, Loopback device"
Now open up ~/.asoundrc and add this to that, if the file doesnt exist create it.

pcm.!default {
type dmix
slave.pcm "hw:Loopback,0,0"
}
pcm.loop {
type plug
slave.pcm "hw:Loopback,1,0"
}
This basically puts all your sound data in the loopback device. If you want to select manually instead use this.

pcm.loop {
type plug
slave.pcm "hw:Loopback,1,0"
}
Save the file. Now, the configuration part is over. You just need to reboot your system. If after rebooting your system does not come up with the loopback device, just load the module again. Now to access the sound on the other device, log into it (or ssh into it from the source machine) and run this command.

ssh -C streamer_username@lan.ip.of.streamer sox -q -t alsa loop -t wav -b 24 -r 48k - | play -q -
This basically will, log in from the laptop that is connected to the sound system to my workspace machine and create a stream file that will be fed back to it via ssh. Then that stream file will be played out on that machine. Neat right?
Afterwards when you are playing something, just run the command

pavucontrol
And from the list of running sound feeds, redirect anyone (or all, your wish) to the "Loopback Audio" device via the menu on the right.

I researched a lot for this setup. Initially my searches kept telling me that I should use "pulseaudio" to expose sound cards to the network, from which the player machine can pick up its feed and play it back through the system. That setup worked but had a lot of interceptions in the output. The problem might have been of pulseaudio alone. I had almost lost hope but I accidentally stumbled upon these two tutorials that saved me. Thanks a lot to them

http://ywwg.com/wordpress/?cat=13
http://plasmasturm.org/log/soundserverhack/
Both of them are amazing write ups.
Gotchas
Even though this system works flawlessly in quality of output, but it has a huge disadvantage of using up many resources and having a huge sync flaw with videos. But I resolved it by using VLC to adjust it, via the J and K keys.


Go to link download