Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Control MPD with xhkeys and a php script.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Vagabond
Apprentice
Apprentice


Joined: 19 Jan 2003
Posts: 192

PostPosted: Tue Jan 11, 2005 9:16 pm    Post subject: Control MPD with xhkeys and a php script. Reply with quote

I've been toying with mpd as my music player for a couple weeks now, and I quickly got xhkeys set up to issue commands to mpc ( a CLI client for mpd), but I wasn't entirely satisfied with how it worked, it didn't print terribly useful output using OSD, and i had to bind seperate keys for play and pause. So last night I hacked together a php script that is more flexible then direct commands to mpc, and prints more useful data to X using xosd. The php script is only a few minutes work ( regexp took the longest because I suck at them).

Requirements

  1. Mpd configured and running.
  2. PHP shell interpreter installed.
  3. Xhkeys installed.
  4. xosd installed.


So here's my little php script, I have it as /usr/bin/pmpc.

Code:

#!/usr/bin/php
<?php
//pmpc - php script to interface with mpc and print results using xosd
//2005 vagabond@cataclysm-software.net

for($i=1; $i<count($argv); $i++)
{
   $args[] = strtolower($argv[$i]);
}

$artist = '';
$song = '';
$status = '';
$playlistseq = '';
$playtime = '';
$playpercent = '';
$volume = '';
$repeat = '';
$random = '';
$crossfade;

function GetInfo($mpdinfo)
{
   global $artist, $song, $status, $playlistseq, $playtime, $playpercent, $volume, $repeat, $random;
   
   $mpdinfo = str_replace(array("\n", "\r"), " ", $mpdinfo);
   
   preg_match('/([\\w\\s\'\(\)\-]*) - ([\\w\\s\'\(\)\-]*) \[([\\w]*)\]\s*#(\d*\/\d*)\s*(\d*:\d*)\s*\((\d*)%\) volume: (\d*)%\s*repeat: (\w*)\s*random: (\w*)/m', $mpdinfo, $match);
   
   $artist = $match[1];
   $song = $match[2];
   $status = $match[3];
   $playlistseq = $match[4];
   $playtime = $match[5];
   $playpercent = $match[6];
   $volume = $match[7];
   $repeat = $match[8];
   $random = $match[9];
   
   GetCrossfade();
}

function GetCrossfade()
{
   global $crossfade;
   
   $crossfade = `mpc crossfade`;
   
   list($bleh, $crossfade) = split(':', $crossfade);
   $crossfade = trim($crossfade);
}

function NextSong()
{
   global $artist, $song, $status, $playlistseq, $playtime, $playpercent, $volume, $repeat, $random;
   $mpdinfo = `mpc next`;
   GetInfo($mpdinfo);
   
   PrintMsg("Skipping to next song $artist - $song");
}

function PrevSong()
{
   global $artist, $song, $status, $playlistseq, $playtime, $playpercent, $volume, $repeat, $random;
   $mpdinfo = `mpc prev`;
   GetInfo($mpdinfo);
   
   PrintMsg("Skipping to previous song song $artist - $song");
}

function PlayPause()
{
   global $status, $artist, $song, $playtime;
   if($status == 'playing')
      $mpdinfo = `mpc pause`;
   elseif($status == 'paused')
      $mpdinfo = `mpc play`;
      
   GetInfo($mpdinfo);
   
   $status = ucwords($status);
   PrintMsg("$status $song - $artist at $playtime");
}

function Volume($increase)
{
   global $artist, $song, $status, $playlistseq, $playtime, $playpercent, $volume, $repeat, $random;

   $mpdinfo = `mpc volume $increase`;
   GetInfo($mpdinfo);

   if(ctype_digit($increase))
      PrintMsg("Volume set to $volume");
   elseif($increase > 0)
      PrintMsg("Volume increased to $volume");
   elseif($increase < 0)
      PrintMsg("Volume decreased to $volume");
}

function CrossFade($increase)
{
   global $crossfade;
   
   if($increase[0] == '-')
   {
      $increase = ltrim($increase, '-');
      $crossfade -= $increase;
      if($crossfade < 0)
         $crossfade = 0;
   }      
   elseif($increase[0] == '+')
   {
      $increase = ltrim($increase, '+');
      $crossfade += $increase;
      if($crossfade > 30)
         $crossfade = 30;
   }
   else
      $crossfade = $increase;
   
   $mpdinfo = `mpc crossfade $crossfade`;
   GetCrossfade($mpdinfo);
   
   PrintMsg("Crossfade set to $crossfade");
   
}

function PrintMsg($message)
{
   $message = escapeshellcmd($message);
   $text = `echo $message | osd_cat -d 2 -s 2 -A center  -c white -f -adobe-helvetica-bold-r-normal-*-*-140-*-*-p-*-iso8859-1 $add`;
}

function PrintInfo()
{
   global $artist, $song, $playtime, $playpercent;
   PrintMsg("$artist - $song ($playtime)");
}


$mpdinfo = `mpc`;

GetInfo($mpdinfo);


switch($args[0])
{
   case 'next':
   {
      NextSong();
      break;
   }
   
   case 'prev':
   {
      PrevSong();
      break;
   }
   
   case 'pause':
   {
      PlayPause();
   }
   
   case 'volume':
   {
      Volume($args[1]);   
      break;
   }
   
   case 'crossfade':
   {
      CrossFade($args[1]);
      break;
   }
   
   default:
   {
      PrintInfo();
      break;
   }
}


?>


It doesn't handle all the mpc options yet, maybe I'll add more later, but it does the main stuff.

You'll need to configure xhkeys using xhkconf or editing ~/.XHkeys. Here's my .XHkeys file:

Code:

xhkeys.codeline2:   98;32;A#;pmpc volume +10
xhkeys.codeline4:   102;32;A#;pmpc next
xhkeys.codeline6:   104;128;A#;pmpc crossfade -5
xhkeys.codeline7:   98;128;A#;pmpc crossfade +5
xhkeys.codeline5:   100;32;A#;pmpc prev
xhkeys.codeline1:   65;32;A#;pmpc pause
xhkeys.codeline3:   104;32;A#;pmpc volume -10


Bindings are as follows:

LWin + up = volume +10
LWin + down = volume -10
LWin + right = next song
LWin + left = prev song
LWin + space = toggle play/pause
Windows menu key + up = crossfade +5
Windows menu Key + down = crossfade -5

You can also pass absolute volume and crossfade values if you want.

The script is not happy when mpd is not playing or paused, I need to work on that. I'm not aware of any other issues, though the regexp will probably fail to match songs with weird characters in the title/artist name. Also if do commands too quickly xosd will overlap the previous message.

If anyone has any comments/questions/requests I'll be keeping an eye on this thread.

Vag
Back to top
View user's profile Send private message
Shan
Guru
Guru


Joined: 04 Nov 2003
Posts: 558
Location: /dev/null

PostPosted: Sun Feb 06, 2005 2:44 am    Post subject: Reply with quote

Excellent script, I've been meaning to do something similar but I'm glad to see somebody has already done the work for me XD
_________________
{ NO -U } { STRIP }
{ TINY }
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum