It’s been a while since I made a post, but here I am. Back again. What’s new? Not a lot. Finished my A level exams (just got STEP to go), had prom the other day which was fun. But now, down to business.
I wanted to write about an awesome little script setup I’ve been working on over the last few days. Intended solely to make my life easier, I am very happy with how they turned out!
This system uses mpd, dmenu, xmonad.hs and a handy perl script. I have linked them all together in what I feel is an epic music setup.
I started by writing a script called plmpc. It’s written in Perl.
#!/usr/bin/perl
use strict;
use warnings;
use Switch;
## CONFIG
my $fadetime = 1;
## END CONFIG
my $dmp = `mpc`;
$dmp =~ /volume:([0-9]*)%/;
my $vol = $1;
my $playing;
if ($dmp =~ /playing/) {
$playing = 1;
} else {
$playing = 0;
}
if ($#ARGV+1 > 0) {
my $arg = $ARGV[0];
switch($arg) {
case 'stop' { stop(); }
case 'pause' { pause(); }
case 'play' { play(); }
case 'splay' {
my $term = '';
foreach my $argnum (1 .. $#ARGV) {
$term = $term." $ARGV[$argnum]";
}
$term =~ s/^ //;
print "Searching: $term\n";
my $tmp = `mpc playlist | wc -l`;
$tmp =~ /([0-9]*)/;
my $toplay = $1;
$toplay++;
print `mpc search any "$term" | mpc add`;
`mpc play $toplay`;
}
case 'toggle' { ($playing eq 1) ? pause() : play(); }
else {
my $cmds = '';
my $c = 0;
while($c < $#ARGV+1) {
$cmds = $cmds.' '.$ARGV[$c];
$c += 1;
}
system("mpc$cmds");
}
}
} else {
system("mpc");
}
#####################
sub play {
my $tmp = 0;
`mpc volume 0`;
system('mpc play');
while($tmp < $vol) {
$tmp += 2;
`mpc volume $tmp`;
sleep($fadetime/50);
}
}
sub stop {
my $tmp = $vol;
while ($tmp > 0) {
$tmp -= 2;
`mpc volume $tmp`;
sleep($fadetime/50);
}
system('mpc stop');
`mpc volume $vol`;
}
sub pause {
my $tmp = $vol;
while ($tmp > 0) {
$tmp -= 2;
`mpc volume $tmp`;
sleep($fadetime/50);
}
system('mpc pause');
`mpc volume $vol`;
} |
This script does exactly the same as mpc, but if you issue a stop, pause, play or toggle command it will fade the sound. Handy. It also has another trick up it’s sleeve. “splay” will search for items, add them to the playlist and start playing them.
Next I built some commands into xmonad:
, ((modMask .|. controlMask, xK_period), spawn "mpc next")
, ((modMask .|. controlMask, xK_comma ), spawn "mpc prev")
, ((modMask .|. controlMask, xK_m ), spawn "mpc toggle")
, ((modMask .|. controlMask, xK_n ), spawn "mpc stop")
, ((modMask .|. controlMask, xK_z ), spawn "mpc volume -2")
, ((modMask .|. controlMask, xK_x ), spawn "mpc volume +2")
, ((modMask .|. controlMask, xK_c ), spawn "mpd; mpc clear; mpc ls | mpc add; mpc random on; mpc play")
, ((modMask .|. controlMask, xK_v ), spawn "mpc random")
, ((modMask .|. controlMask, xK_b ), spawn "plmpc splay `echo | dmenu -nb '#222222' -nf '#999999' -sb '#5B7496' -fn '-*-montecarlo-medium-*-*-*-*-*-*-*-*-*-*-*'`") |
I should note that the plmpc script should be in ~/bin/ and ~/bin/ should be in $PATH.
All of these (I should hope) are self explanatory – but the last line is the coolest of them all. I used dmenu as a simple input device for my “plmpc splay” script.
Sorry for the terrible formatting; WordPress’ fault not mine.