| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Audio::MPD q{0.19.0}; |
| 5 |
use Time::HiRes qw(usleep); |
| 6 |
|
| 7 |
=head1 NAME |
| 8 |
|
| 9 |
mpskip - emulate a skipping record |
| 10 |
|
| 11 |
=head1 SYNOPSIS |
| 12 |
|
| 13 |
mpskip [num_skips] [duration] [skippos] [host] |
| 14 |
|
| 15 |
=head1 DESCRIPTION |
| 16 |
|
| 17 |
B<mpskip> makes mpd emulate a skipping record. |
| 18 |
|
| 19 |
If B<num_skips> is not specified (or is 0), it will skip forever (or until |
| 20 |
someone manually seeks past the "bad" part of the song, or changes songs). |
| 21 |
|
| 22 |
The B<duration> is how long the skip should be, in seconds. Floating point |
| 23 |
values can be used. |
| 24 |
|
| 25 |
The B<skippos> is the number of seconds into the song to place the skip point. |
| 26 |
Default is the current play position. |
| 27 |
|
| 28 |
If the hostname is omitted, the MPD_HOST environment variable will be used. |
| 29 |
|
| 30 |
=head1 LIMITATIONS |
| 31 |
|
| 32 |
Doesn't insert the pop you hear on a real record player when the needle |
| 33 |
skips. |
| 34 |
|
| 35 |
If run against a remote host, it may not skip at exactly the same place |
| 36 |
each time due to network issues. |
| 37 |
|
| 38 |
=head1 AUTHOR |
| 39 |
|
| 40 |
Copyright 2007 Joey Hess <[email protected]> |
| 41 |
|
| 42 |
Licensed under the GNU GPL version 2 or higher. |
| 43 |
|
| 44 |
http://kitenet.net/~joey/code/mpdtoys |
| 45 |
|
| 46 |
=cut |
| 47 |
|
| 48 |
my $num_skips=0; |
| 49 |
if (@ARGV && $ARGV[0] =~ /^[0-9]+$/) { |
| 50 |
$num_skips=shift; |
| 51 |
} |
| 52 |
|
| 53 |
my $duration=1; |
| 54 |
if (@ARGV && $ARGV[0] =~ /^[0-9.]+$/) { |
| 55 |
$duration=shift; |
| 56 |
} |
| 57 |
|
| 58 |
my $skippos; |
| 59 |
if (@ARGV && $ARGV[0] =~ /^[0-9]+$/) { |
| 60 |
$skippos=shift; |
| 61 |
} |
| 62 |
|
| 63 |
if (@ARGV) { |
| 64 |
$ENV{MPD_HOST}=shift; |
| 65 |
} |
| 66 |
|
| 67 |
my $mpd=Audio::MPD->new(conntype => "reuse"); |
| 68 |
if ($mpd->status->state ne 'play') { |
| 69 |
die "error: mpd has to be playing first\n"; |
| 70 |
} |
| 71 |
|
| 72 |
my $songid=$mpd->current->id; |
| 73 |
$skippos=$mpd->status->time->seconds_sofar unless defined $skippos; |
| 74 |
|
| 75 |
print "skipping".($num_skips ? " $num_skips times" : ""). |
| 76 |
" at position $skippos for $duration seconds\n"; |
| 77 |
|
| 78 |
while (usleep $duration * 1000000 / 2 ) { |
| 79 |
my $status=$mpd->status; |
| 80 |
my $pos=$status->time->seconds_sofar; |
| 81 |
exit if $status->state ne 'play'; |
| 82 |
exit if $mpd->current->id ne $songid; |
| 83 |
exit if $pos > $skippos + $duration + 1; |
| 84 |
next if $pos <= $skippos; |
| 85 |
$mpd->seek($skippos); |
| 86 |
if ($num_skips) { |
| 87 |
$num_skips--; |
| 88 |
exit unless $num_skips; |
| 89 |
} |
| 90 |
} |