You are not logged in.
I want to log every time when MPD changes song, but I can't find out how. Anyone got any ideas?
Offline
A hackish way might be to run a simple shell script to check the output of an mpd client (like mpc). If you're fine with assuming every song will be at least x seconds long, you could setup a simple while loop that sleeps x seconds, and checks if the output of mpc (or some other client) has changed since the last check (and make sure that it is playing a song). You could even change the sleep time dynamically based on how much time the song has left to play, if you're really concerned about CPU usage.
I'm not sure if there's anyway you could setup an event to fire within mpd, though.
Last edited by BurntSushi (2009-07-07 04:45:56)
Education is favorable to liberty. Freedom can exist only in a society of knowledge. Without learning, men are incapable of knowing their rights, and where learning is confined to a few people, liberty can be neither equal nor universal.
Tu ne cede malis sed contra audentior ito
Offline
Yeah I thought in the same way too... That's a damn ugly hack though. Maybe there's some other way...
Offline
here is the code in C that prints out name of the song when mpd changes it.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libmpd.h>
#include <debug_printf.h>
#define RED "\x1b[31;01m"
#define DARKRED "\x1b[31;06m"
#define RESET "\x1b[0m"
#define GREEN "\x1b[32;06m"
#define YELLOW "\x1b[33;06m"
void status_changed(MpdObj *mi, ChangedStatusType what)
{
if(what&MPD_CST_SONGID)
{
mpd_Song *song = mpd_playlist_get_current_song(mi);
if(song)
{
printf(GREEN"Song:"RESET" %s - %s\n", song->artist, song->title);
}
}
}
int main()
{
int run = 1, iport = 6600;
char *hostname = getenv("MPD_HOST");
char *port = getenv("MPD_PORT");
char *password = getenv("MPD_PASSWORD");
MpdObj *obj = NULL;
if(!hostname) {
hostname = "localhost";
}
if(port){
iport = atoi(port);
}
obj = mpd_new(hostname, iport,password);
mpd_signal_connect_status_changed(obj,(StatusChangedCallback)status_changed, NULL);
mpd_set_connection_timeout(obj, 10);
if(!mpd_connect(obj))
{
mpd_send_password(obj);
do{
mpd_status_update(obj);
}while(!usleep(100000) && run);
}
return 1;
}
to compile it:
gcc source_code -o output_file -lmpd -I/usr/include/libmpd-1.0/libmpd/
the code it's actually a modified example from libmpd site:
https://svn.musicpd.org/MOVED/libmpd/tr … testcase.c
Last edited by koper (2009-07-07 13:45:55)
Offline
does this use mpds idle protocol? because it should. Its the only way of getting information without polling.
He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.
Douglas Adams
Offline
Thanks koper!
Offline
here is the code in C that prints out name of the song when mpd changes it.
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <libmpd.h> #include <debug_printf.h> #define RED "\x1b[31;01m" #define DARKRED "\x1b[31;06m" #define RESET "\x1b[0m" #define GREEN "\x1b[32;06m" #define YELLOW "\x1b[33;06m" void status_changed(MpdObj *mi, ChangedStatusType what) { if(what&MPD_CST_SONGID) { mpd_Song *song = mpd_playlist_get_current_song(mi); if(song) { printf(GREEN"Song:"RESET" %s - %s\n", song->artist, song->title); } } } int main() { int run = 1, iport = 6600; char *hostname = getenv("MPD_HOST"); char *port = getenv("MPD_PORT"); char *password = getenv("MPD_PASSWORD"); MpdObj *obj = NULL; if(!hostname) { hostname = "localhost"; } if(port){ iport = atoi(port); } obj = mpd_new(hostname, iport,password); mpd_signal_connect_status_changed(obj,(StatusChangedCallback)status_changed, NULL); mpd_set_connection_timeout(obj, 10); if(!mpd_connect(obj)) { mpd_send_password(obj); do{ mpd_status_update(obj); }while(!usleep(100000) && run); } return 1; }
to compile it:
gcc source_code -o output_file -lmpd -I/usr/include/libmpd-1.0/libmpd/
the code it's actually a modified example from libmpd site:
https://svn.musicpd.org/MOVED/libmpd/tr … testcase.c
So this wakes up every 0.1 seconds....? there goes your battery life...
Offline
mpc 0.16 supports the idle event.
#!/bin/bash
while : ; do
mpc idle
mpc current
done
This will show the word "player" on each time play/pause/stop/next/prev have succeeded.
Other printouts are "mixer (volume change) "output" (output device enabled/disabled) "options" (e.g. playback mode changed). "update" (database updating)
The above script in action:
carnager@freebox ~/mpc % ./mpc.sh
player
Pearl Jam - Gone
player ---> PAUSE EVENT
Pearl Jam - Gone
player ---> NEXT EVENT
Pearl Jam - Better Man
Last edited by Rasi (2009-07-08 21:18:26)
He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.
Douglas Adams
Offline
(Sorry for my english)
i was wrote a C program to show notifications when mpd change the playin song .. now the code is out of date, but maybe the code will help you
http://aur.archlinux.org/packages.php?ID=19986
The idea was very simple... the binary check for the state of mpd using the same algorithm of lastfmsubmitd
I hope this can help you
www.msdarkici.wordpress.com (Spanish) All what i Do in the linux world!!!
Arch User... Feel Free... Feel Speed....
Offline
@Rasi: While you're using the updated mpc, you should probably use 'mpc current' instead of piping it to head.
Offline
@Rasi: While you're using the updated mpc, you should probably use 'mpc current' instead of piping it to head.
ah cool.. didnt know that
He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.
Douglas Adams
Offline