You are not logged in.

#1 2009-07-07 03:31:23

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

How to check when MPD changes track?

I want to log every time when MPD changes song, but I can't find out how. Anyone got any ideas?

Offline

#2 2009-07-07 04:45:10

BurntSushi
Member
From: Massachusetts
Registered: 2009-06-28
Posts: 362
Website

Re: How to check when MPD changes track?

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

#3 2009-07-07 05:03:46

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: How to check when MPD changes track?

Yeah I thought in the same way too... That's a damn ugly hack though. wink Maybe there's some other way...

Offline

#4 2009-07-07 13:40:51

koper
Member
Registered: 2006-12-17
Posts: 2

Re: How to check when MPD changes track?

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

#5 2009-07-07 15:55:12

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: How to check when MPD changes track?

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

#6 2009-07-07 17:57:20

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: How to check when MPD changes track?

Thanks koper!

Offline

#7 2009-07-07 19:22:38

GogglesGuy
Member
From: Rocket City
Registered: 2005-03-29
Posts: 610
Website

Re: How to check when MPD changes track?

koper wrote:

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

#8 2009-07-08 11:03:35

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: How to check when MPD changes track?

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

#9 2009-07-08 13:29:14

msdark
Member
From: Chile
Registered: 2008-08-17
Posts: 49
Website

Re: How to check when MPD changes track?

(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

#10 2009-07-08 14:58:33

fflarex
Member
Registered: 2007-09-15
Posts: 466

Re: How to check when MPD changes track?

@Rasi: While you're using the updated mpc, you should probably use 'mpc current' instead of piping it to head.

Offline

#11 2009-07-08 21:18:12

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: How to check when MPD changes track?

fflarex wrote:

@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

Board footer

Powered by FluxBB