summaryrefslogtreecommitdiff
path: root/mpskip
diff options
context:
space:
mode:
Diffstat (limited to 'mpskip')
-rwxr-xr-xmpskip90
1 files changed, 90 insertions, 0 deletions
diff --git a/mpskip b/mpskip
new file mode 100755
index 0000000..5caa8d8
--- /dev/null
+++ b/mpskip
@@ -0,0 +1,90 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Audio::MPD q{0.19.0};
+use Time::HiRes qw(usleep);
+
+=head1 NAME
+
+mpskip - emulate a skipping record
+
+=head1 SYNOPSIS
+
+mpskip [num_skips] [duration] [skippos] [host]
+
+=head1 DESCRIPTION
+
+B<mpskip> makes mpd emulate a skipping record.
+
+If B<num_skips> is not specified (or is 0), it will skip forever (or until
+someone manually seeks past the "bad" part of the song, or changes songs).
+
+The B<duration> is how long the skip should be, in seconds. Floating point
+values can be used.
+
+The B<skippos> is the number of seconds into the song to place the skip point.
+Default is the current play position.
+
+If the hostname is omitted, the MPD_HOST environment variable will be used.
+
+=head1 LIMITATIONS
+
+Doesn't insert the pop you hear on a real record player when the needle
+skips.
+
+If run against a remote host, it may not skip at exactly the same place
+each time due to network issues.
+
+=head1 AUTHOR
+
+Copyright 2007 Joey Hess <joey@kitenet.net>
+
+Licensed under the GNU GPL version 2 or higher.
+
+http://kitenet.net/~joey/code/mpdtoys
+
+=cut
+
+my $num_skips=0;
+if (@ARGV && $ARGV[0] =~ /^[0-9]+$/) {
+ $num_skips=shift;
+}
+
+my $duration=1;
+if (@ARGV && $ARGV[0] =~ /^[0-9.]+$/) {
+ $duration=shift;
+}
+
+my $skippos;
+if (@ARGV && $ARGV[0] =~ /^[0-9]+$/) {
+ $skippos=shift;
+}
+
+if (@ARGV) {
+ $ENV{MPD_HOST}=shift;
+}
+
+my $mpd=Audio::MPD->new(conntype => "reuse");
+if ($mpd->status->state ne 'play') {
+ die "error: mpd has to be playing first\n";
+}
+
+my $songid=$mpd->current->id;
+$skippos=$mpd->status->time->seconds_sofar unless defined $skippos;
+
+print "skipping".($num_skips ? " $num_skips times" : "").
+ " at position $skippos for $duration seconds\n";
+
+while (usleep $duration * 1000000 / 2 ) {
+ my $status=$mpd->status;
+ my $pos=$status->time->seconds_sofar;
+ exit if $status->state ne 'play';
+ exit if $mpd->current->id ne $songid;
+ exit if $pos > $skippos + $duration + 1;
+ next if $pos <= $skippos;
+ $mpd->seek($skippos);
+ if ($num_skips) {
+ $num_skips--;
+ exit unless $num_skips;
+ }
+}