Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Run Script with play button (yet another...)
#1
Question 
Hello everybody,

my name is Matthias and I've switched from volumio to moode because I like the look and the ability to have a usable os in the background.

I've modifed my old mini-HiFi System with a RPi to play internet radio streams via the tape input  Cool
Because my mini-HiFi System draws a lot of power in standby mode, I want to use the "Eco-Power" feature. Therefore I soldered some wires to the pcb where the button is located and connected it to an optocoupler and the other side to a Pi GPIO. It works really well in volumio.

That's what I wanted to have in moode, too. I know that I'm not the only one as I saw a lot of threads on this topic.
But: Most of them use a frequent polling of the audio output. This works of course, but uses a lot of CPU power. I want to save the energy as the system will be not outputting audio most of the day.

My question is if there is a callback function when the play/pause button is pressed or if there is an easy way to insert a script when the button is pressed. This would lead to a much lower CPU load and to a lower power consumption.
I don't net to be able to configure it via the web ui. It'd be perfectly fine if I have to copy and activate the script via ssh.

Thanks in advance for your input! Smile

Regards to everyone!
Matthias
Reply
#2
There is no built in callback but it would be pretty easy to add one.

Look at the file /var/www/command/index.php in the code block titled // MPD commands. You would simply add some code to check for $_GET['cmd'] equal to one of 'pause', 'stop', or 'play' and then execute your script using the sysCmd() function.

-Tim
Enjoy the Music!
moodeaudio.org | Mastodon Feed | GitHub
Reply
#3
Hi Tim,

thank you very very much for your fast and perfect answer! I've got my scripts running and my amp is switching like want it to, when I hit the button.

I know that this is what I've asked for - but:
Is there a way to run scripts when choosing a radio station? It starts playing automatically so I thought that this would also trigger the command you were talking about in your post. But it seems like I'm wrong here...

Thanks again!

Regards,
Matthias
Reply
#4
Hi Matthias,

Look in file /var/www/command/moode.php. Scroll down to the section titled // Commands sent to MPD. You can call your script before the break; in the case blocks.
Enjoy the Music!
moodeaudio.org | Mastodon Feed | GitHub
Reply
#5
Hello again,

the good thing first: The script is now getting started, when I choose a radio station, great!
So thanks again for your patience and help.


But I haven't thought about a few things:
- When I have chosen a radio station and change it to another, the script of course gets started again because of the "clearplay" command.

That's why I created my own php script and included it in the index.php as well as in the moode.php.
The problem is that I don't have a variable, where I can store the "playing" state so that changing the radio station doesn't trigger the startup script of the amplifier again.

I've tried it with the global super variable in php as well as with a static local variable inside my php switch function (which calls the actual python script).
None of my attempts worked :-(. I've to also admit that I'm in no way experienced in php. Confused

My ultimate goal is to achieve the following:
When choosing a radio station or hitting the play button, start up the amplifier via script. Do not run the script again, when a radio station is already playing.
When hitting the pause/stop button, start a timer (for example 20 seconds). If the 20 seconds are over, call a shutdown script for the amplifier.
If another play request within the 20 seconds occurs, do not shut down the amplifier.


I was thinking about a thread in php for the timer part but I'm worried that it might end in the same way as it does for the "playing" state - that this state/variable is not visible or readable when another event (play/stop) occurs. Huh

I hope someone can understand what I'm trying to do/build. Also I'd be very very happy, if someone could help me partly out!

Thanks everyone - especially Tim! :-)

Regards
Matthias
Reply
#6
Ok, based on your reqts I think a simple Python script that implements a polling loop to check whether audio is playing and runs in the background would do the trick. It could also handle the 20 sec timer. You would launch it from /etc/rc.local

How long after play/pause/stop do you need to Amp to be turned on/off? This determined the polling interval.

To check whether audio is playing run the cmd below. It's checking ALSA output and is independent of which application is playing.

This example assumes card0 (I2S or On-board audio device). Use card1 for USB audio device.
Code:
# Audio playing
pi@rp3:~ $ cat /proc/asound/card0/pcm0p/sub0/hw_params
access: RW_INTERLEAVED
format: S16_LE
subformat: STD
channels: 2
rate: 44100 (44100/1)
period_size: 4410
buffer_size: 22050

# Audio not playing
pi@rp3:~ $ cat /proc/asound/card0/pcm0p/sub0/hw_params
closed

-Tim
Enjoy the Music!
moodeaudio.org | Mastodon Feed | GitHub
Reply
#7
Hi Tim,

thank you very much. I'm going to try it as soon as I'm well again.
Reply
#8
Hi Matthias,

if you stick to mpd listening (no bluetooth, airplay which directly access alsa) and you are familiar with C coding, there is a callback function. You can write a sort of custom client to mpd with libmpdclient. After establishing a connection to the running mpd session you can exec "mpd_run_idle()" which set your program to sleep state until mpd change the current state. After wake up you can read the new mpd state with "mpd_run_status" and decide what to do. With signal usage you should also to be able to handle the power off delay. But this might lead to some debug sessions...

Regards
Markus
Reply


Forum Jump: