Detect when something is streaming ?

Get help for Volumio here!

Ads helps Volumio remain Free and Open Source. Please consider donating to help us continue to serve you.

Detect when something is streaming ?

Postby ccomino » Thu Nov 13 2014 17:56

Hi everyone,
I'm not really sure that this is the correct section, but I didn't find a suitable one...

I use a raspberry with volumio installed to drive two speakers via Airplay.
Because the speakers (and also the raspberry) are located in a not easily reachable position, I need to leave the speakers always on.

My idea is to use a usb relay to turn on and off the speakers.

Now I made a very simple php page and whenever i want to listen to the music, I turn on the relay using that page.

BUT:

Is it possible to do this automatically ? I mean, is it possible to setup a script that detects when anything is sent to the speakers (HiFiBerry DAC) and turn on and off the relays ?

I hope that I have been clear enough...

Thank you for any help, unfortunately it is a rather urgent matter, because I need to move all this device to my holydays house by the end of november.

Carlo.
Random avatar
ccomino
Fresh off the boat
Fresh off the boat
 
Posts: 6
Joined: Wed Nov 12 2014 20:08

Ads helps Volumio remain Free and Open Source. Please consider donating to help us continue to serve you.


Detect when something is streaming ?

Postby MobeyDuck » Thu Nov 13 2014 18:44

Yes, this should be possible.
I think that the easiest way to do this would be to trigger with playback. So when volumio plays a song the speakers are turned on and when it stops it turns of. This all would be on the software / script side of things. Basically when you've installed the stereo and raspberry pi you only need an ssh connection to set it up.

Note that i currently dont know how you need to do it on the software side of things, but you should be able to read out the state of MPD to see whether or not it is playing a song. But if you would use spotify theres a chance that the speakers are turned of since spotify uses its own player.

But you are using a USB Relay, while the GPIO's are perfectly fit to control a relay this would save the USB port for other uses.
User avatar
MobeyDuck
Moderator
 
Posts: 482
Joined: Sun Jan 05 2014 20:28
Location: The Netherlands


Detect when something is streaming ?

Postby ccomino » Thu Nov 13 2014 19:04

Thank you MobeyDuck for your prompt answer!
But... ehm...
I'm a very newbye about linux matters...
I only want to use the Airplay features of Volumio for now. Surely not want to use spotify.

Could you please send me a sample script from which i can start ?
This is the relay i use:
http://www.ebay.it/itm/271361447857

My idea is to setup a script that "hears" when the stream begins and closes the relay, and another script scheduled in the crontab that every about 10 minutes checks if there is anything playing and if nothing is playing opens the relay.
I think that is better to divide the two scripts because if i need to reboot the Raspberry when something is playing the relay remains closed.
If i divide the two scripts when the raspberry starts up again the script opens the relay because nothing is playing.
Thank you!
Carlo.
Random avatar
ccomino
Fresh off the boat
Fresh off the boat
 
Posts: 6
Joined: Wed Nov 12 2014 20:08


Detect when something is streaming ?

Postby MobeyDuck » Thu Nov 13 2014 22:56

the script is rather easy to make, what you need to do is:

While true
- Check if there is a song playing
- If true (the song is playing) turn speakers on
- If False wait seconds (so that there is time for an other song to start)
- - check again
- - IF False turn speakers of
- wait seconds

(note the interval should be changed according to your needs)

This would get you a little something like the next code
Code: Select all
X=0
while true
   If song plays, speaker should be on, X=0
   If song not playing,
      do X = X+1
   If X=3
      turn speakers of
   wait 238 seconds


If you run this from boot X wil be 0 at startup so the speakers wil turn on.
While true makes a loop.
X is used to make sure the speakers stay turned on when a song stopped playing at the moment of the check and gives you some time to start an other song.

238 seconds is the average play time - according to http://www.statcrunch.com/5.0/viewrepor ... roupid=948

X = 3 equals 3 checks of 238 seconds --> 714 seconds or about 11 minutes total. This should be changed according to your needs.

If you start this script at boot the speakers are turned on with your R-Pi so you don't need to re-enable cron (the first volumio versions didnt had cron enabled) and it all works from one script.


So theres you pseudo code, you need to alter it for the language you'll be using.

Now you only need to find out what to check to see if theres a song playing. AKA. The hard part. and then how to trigger the relay from a script.
This could be done "the ugly way" by simply executing command-line actions via a script.

As i said, for now i don't know what to check to see if there is a song playing. You should look for documentation about this, MPD is the actual player so the information youll need is in there.
User avatar
MobeyDuck
Moderator
 
Posts: 482
Joined: Sun Jan 05 2014 20:28
Location: The Netherlands


Detect when something is streaming ?

Postby ccomino » Fri Nov 14 2014 05:38

Thank you very much again!
Next week-end i will try and i will be back to you.
Thanks a lot!
Carlo.
Random avatar
ccomino
Fresh off the boat
Fresh off the boat
 
Posts: 6
Joined: Wed Nov 12 2014 20:08


Detect when something is streaming ?

Postby ccomino » Fri Nov 14 2014 09:00

Hi,
I found this:

http://stackoverflow.com/questions/1128 ... or-stopped

Could you please help me once again to setup a script ? I'm not expert in phyton neither...

Thank you again!
Carlo.
Random avatar
ccomino
Fresh off the boat
Fresh off the boat
 
Posts: 6
Joined: Wed Nov 12 2014 20:08


Detect when something is streaming ?

Postby ccomino » Fri Nov 14 2014 09:30

perhaps this is what i need:

http://www.musicpd.org/clients/mpc/

No need for python :)
Random avatar
ccomino
Fresh off the boat
Fresh off the boat
 
Posts: 6
Joined: Wed Nov 12 2014 20:08


Detect when something is streaming ?

Postby MobeyDuck » Fri Nov 14 2014 12:08

It turns out that mpc is installed and ready to use.

First i had to find a mpc command that shows either the playlist or the current song, and only that.
I took the command mpc playlist at first, but this only works when trash mode is on so that the playlist wil be empty. Looking further on http://linux.die.net/man/1/mpc

I found the current command
Code: Select all
mpc current
this only shows the current playing song and only if it is playing.

Using that and google-ing a bit more to find how to incorporate it with a python script i came up with the next code.

Code: Select all
import subprocess
x=0

while true :
   process = subprocess.Popen("mpc current".split(), stdout=subprocess.PIPE)
   output = process.communicate()[0]
   
   if output != "" : #if output is empty
      x=x+1
   
   else : #output is not empty
      x=0

   if x>= 3 : #after 3 checks with no song playing turn off relay
      # trigger relay off
   
   else :
      # keep speaker on / trigger relay on
   
   wait 238


Now you only need to find out how to trigger the relay and incorporate it in the script and of course test it. (run as a python script)
User avatar
MobeyDuck
Moderator
 
Posts: 482
Joined: Sun Jan 05 2014 20:28
Location: The Netherlands


Return to Help

Who is online

Users browsing this forum: No registered users and 4 guests