jimallyn
Posts: 51
Joined: Wed Nov 12, 2014 9:13 am

How to stream Pi audio over the internet?

Sat Feb 07, 2015 12:08 am

I am looking for a way to stream audio played or generated on the RPi over the internet. I have found several articles on a RPi baby monitor, but that is for taking an external audio source like a microphone and streaming that. That's not what I want to do.

Some background: I have been using my RPi and the book "Hello world! : computer programming for kids and other beginners" to learn to program in Python. For several reasons, I have been doing this mostly over the internet, using TightVNC. I am to the point in the book where the programs (simple game programs) will be playing audio, mostly .wav and .mp3 files stored on the Flash drive. TightVNC apparently does not allow streaming audio over the net. I would like to be able to hear the audio being played on the RPi from Python programs remotely.

Any suggestions on how to do this?
“So you say you love the poor? Name them.” - Gustavo Gutierrez

ktb
Posts: 1380
Joined: Fri Dec 26, 2014 7:53 pm

Re: How to stream Pi audio over the internet?

Sat Feb 07, 2015 10:27 am

I don't know myself, but is xrdp and x11rdp available for the Pi? That should support audio.

jimallyn
Posts: 51
Joined: Wed Nov 12, 2014 9:13 am

Re: How to stream Pi audio over the internet?

Sun Feb 15, 2015 4:51 am

I have done a bunch of Googling, and have come up with several methods to stream audio over the network, as long as the audio is coming from an external source. Most of them use something like this:

Code: Select all

arecord -f cd -t raw | oggenc - -r | ssh <user>@<remotehost> mplayer -
I have tried variations on that theme, and I can get it to say it is streaming audio to the other computer on the network, but of course I don't hear anything on the other computer because the RPi has no audio input.

So, what I need to know is how to route audio played on the Pi to ssh? Some sort of redirection or pipeline? Anybody got a suggestion?
“So you say you love the poor? Name them.” - Gustavo Gutierrez

User avatar
ragnarjensen
Posts: 332
Joined: Wed May 15, 2013 6:13 pm
Location: Stockholm, Sweden
Contact: Website

Re: How to stream Pi audio over the internet?

Sun Feb 15, 2015 2:19 pm

ALSA's loopback module comes to mind.

Code: Select all

sudo modprobe snd-aloop
To make it survice reboots, add this line to /etc/modules

Code: Select all

snd-aloop
After loading the snd-aloop module, you should have a new, virtual sound card comprising two Loopback devices, each with eight sub-devices. The example below shows one of my Pis. It has only the Pi's built-in audio and the two loopback devices - card 1.

Code: Select all

ragnar@quattropi:~$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 1: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
Then play back to the loopback device. Here's an example using mpg123.

Code: Select all

mpg123-alsa -a hw:Loopback,0,0 mp3-files_or_playlist ...
Sound played back onto the Loopback,0,0 device comes back as a virtual input on Loopback,1,0 and can be recorded or streamed from it.

Code: Select all

arecord -D  hw:Loopback,1,0 ...
--
Ragnar

jimallyn
Posts: 51
Joined: Wed Nov 12, 2014 9:13 am

Re: How to stream Pi audio over the internet?

Sun Feb 15, 2015 10:14 pm

Thanks for the information, ragnarjensen, but that still doesn't accomplish what I want to do. I think it gives me a step in the right direction, though. I don't want to stream a playlist from one application, I want every sound from every application on the Pi to be streamed. Do you know of a way to do that?
“So you say you love the poor? Name them.” - Gustavo Gutierrez

User avatar
ragnarjensen
Posts: 332
Joined: Wed May 15, 2013 6:13 pm
Location: Stockholm, Sweden
Contact: Website

Re: How to stream Pi audio over the internet?

Sun Feb 15, 2015 10:41 pm

It was only meant to be a step in the right direction ;)

The following is mostly stolen from post #4 in this thread over at the Arch Linux forums

Create a file named .asoundrc in your home directory and paste this into it:

Code: Select all

pcm.!default {
  type asym
  playback.pcm "LoopAndReal"
  capture.pcm "looprec"
}

pcm.looprec {
    type hw
    card "Loopback"
    device 1
    subdevice 0
}

pcm.LoopAndReal {
  type plug
  slave.pcm mdev
  route_policy "duplicate"
}

pcm.mdev {
  type multi
  slaves.a.pcm pcm.MixReale
  slaves.a.channels 2
  slaves.b.pcm pcm.MixLoopback
  slaves.b.channels 2
  bindings.0.slave a
  bindings.0.channel 0
  bindings.1.slave a
  bindings.1.channel 1
  bindings.2.slave b
  bindings.2.channel 0
  bindings.3.slave b
  bindings.3.channel 1
}

pcm.MixReale {
  type dmix
  ipc_key 1024
  slave {
    pcm "hw:0,0"
    rate 48000
    #rate 44100
    periods 128
    period_time 0
    period_size 1024 # must be power of 2
    buffer_size 8192
  }
}

pcm.MixLoopback {
  type dmix
  ipc_key 1025
  slave {
    pcm "hw:Loopback,0,0"
    rate 48000
    #rate 44100
    periods 128
    period_time 0
    period_size 1024 # must be power of 2
    buffer_size 8192
  }
}
Your default playback device will now be a virtual sound card that duplicates all speaker sound to the loopback device.
The default recording device is the loopback device.
Any sounds heard in the speakers will be available for recording or streaming from the loopback device.
--
Ragnar

jimallyn
Posts: 51
Joined: Wed Nov 12, 2014 9:13 am

Re: How to stream Pi audio over the internet?

Mon Feb 16, 2015 12:00 am

Cool! Thanks very much. I'll give that a try. I bought this RPi to learn on, and I am definitely learning a few things!
“So you say you love the poor? Name them.” - Gustavo Gutierrez

injurer
Posts: 4
Joined: Fri Mar 25, 2016 8:53 pm

Re: How to stream Pi audio over the internet?

Mon Mar 28, 2016 6:13 pm

It's an old post but I can't get it to work. When trying to play something through the default device it gets stuck. Any ideas what could be wrong?

fransko
Posts: 19
Joined: Sat Jan 05, 2013 11:23 pm

Re: How to stream Pi audio over the internet?

Tue Jan 03, 2017 10:33 pm

Create a file named .asoundrc in your home directory and paste this into it:

What do you mean by home directory???

can you tell my that please

JFamily
Posts: 27
Joined: Tue Aug 11, 2015 1:40 am

Re: How to stream Pi audio over the internet?

Wed Jan 04, 2017 9:26 pm

fransko wrote:Create a file named .asoundrc in your home directory and paste this into it:

What do you mean by home directory???

can you tell my that please
If you open the terminal or freshly log in to the device via PuTTy, you are in the home directory. If you ever need to get back to the home directory, you can simply enter:

Code: Select all

cd ~
If you type

Code: Select all

pwd
It will show you the directory you are currently in (most likely /home/pi (home directory))
I'm notoriously terrible at being concise.
RaspberryPi 3 / SanDisk Ultra 8GB Class 10 / Raspbian Jessie / Kodi v15.2
RaspberryPi 2 / SanDisk Ultra 8GB Class 10 / Raspbian Jessie / PleXMBC (wip)

lah
Posts: 3
Joined: Fri Sep 04, 2015 2:02 pm

Re: How to stream Pi audio over the internet?

Tue Feb 14, 2017 2:17 pm

Here is a post about it :
http://stackoverflow.com/questions/4220 ... -with-dmix
So far there is a limitation : you cannot play several sounds at same time. I cannot get dmix plugin working on raspberry. When I add dmix in working configuration sound playing stays stuck.
I'm trying to make it work, but after many attempts, I think deeper investigations must be done instrumenting alsa code.

RpiName
Posts: 712
Joined: Sat Jul 06, 2013 3:14 am

Re: How to stream Pi audio over the internet?

Tue Feb 14, 2017 3:11 pm

jimallyn wrote:I am looking for a way to stream audio played or generated on the RPi over the internet. I have found several articles on a RPi baby monitor, but that is for taking an external audio source like a microphone and streaming that. That's not what I want to do.

Some background: I have been using my RPi and the book "Hello world! : computer programming for kids and other beginners" to learn to program in Python. For several reasons, I have been doing this mostly over the internet, using TightVNC. I am to the point in the book where the programs (simple game programs) will be playing audio, mostly .wav and .mp3 files stored on the Flash drive. TightVNC apparently does not allow streaming audio over the net. I would like to be able to hear the audio being played on the RPi from Python programs remotely.

Any suggestions on how to do this?
This has already been done quite easily before by somebody. It's possible to stream both live video and/or audio streams generated by the Raspberry Pi to any browser (no plugin required) on a PC or smartphone with UV4L. Follow this link carefully:

viewtopic.php?f=43&t=163738&p=1058123&h ... p#p1058123

MORINGA
Posts: 1
Joined: Sat Nov 25, 2017 12:36 pm

Re: How to stream Pi audio over the internet?

Sat Nov 25, 2017 4:42 pm

Hi!
24h straight, I can't seem to make it work at all.

First, I need some kind of functional tightvnc viewer (connect). I have server working on both sides (my windows laptop and my pi - which is 3, model B). I've once managed to make it work with that very limited RealVNC + mouse and keyboard on Pi, which is absolutely terrible!
Tightvncconnect is not exactly friendly, and I didn't find much of a documentation about it. Xtightvncconnect is even worse, so I'm running blind on this one. (Yeah, it's like "good luck learning on your own about available displays")

Then, I need to stream a video file from Pi, with some quality audio. I tried UV4L, configured loopback (it shows on PulseAudio, but it never sent any audio at all, though it plays locally - via omxplayer - both on HDMI and audio jack (all I found through my searching was "camera", "webcam" and "microphone", which is obviously not the case). Tried FFMPEG also, no success (and I don't have time to do a 6-month Harvard course to just learn how to use the damn thing!!).
I remember using NSV for a college work, but I'm not sure if it's audio only (at that time, it was all that mattered, b/c QoS and stuff). I also found some things IceCast-related, but it's the same.

Here's some draft...and both notebooks may act as a backup of each other, too. Also, both notebooks and the Pi are connected via Wi-Fi (the purpose is to get rid of as many cables as possible; later on, I'll probably make it work the other way around: the Pi acting as a multi-receiver for audio streams, and sending them independently through, say, audio jack and HDMI, or audio jack and USB sound card, w/e)
schematics.png
schematics.png (18.93 KiB) Viewed 4907 times

Return to “Graphics, sound and multimedia”