[project @ 5762]
[audio-mpd.git] / bin / mpd-dynamic
blob52b1990ddf4b073e8a54b48c0b651e2f248af58a
1 #!/usr/bin/perl
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 use strict;
20 use warnings;
22 use Audio::MPD;
23 use Getopt::Euclid qw[ :minimal_keys ];
24 use Proc::Daemon;
25 use Time::HiRes qw[ usleep ];
27 Proc::Daemon::Init unless $ARGV{debug};
30 my $song = 0; # song currently playing
31 my $playlist = 0; # playlist version
32 my $mpd = Audio::MPD->new;
34 # fetch list of songs known by mpd.
35 my @files = $mpd->collection->all_pathes;
38 while (1) { # endless loop
39 my $status;
40 eval { $status = $mpd->status };
41 next if $@; # error while peaking status
43 # do playlist and/or current song have changed?
44 next unless $status->playlist > $playlist
45 || defined $status->song && $status->song != $song;
48 # yup - update playlist & song.
49 $playlist = $status->playlist;
50 $song = $status->song;
52 # keep at most $ARGV{old} songs.
53 if ( $song > $ARGV{old} ) {
54 my $old = $song - $ARGV{old};
55 eval { $mpd->delete(0) for 1..$old };
58 # add at most $ARGV{new} songs.
59 my $pl = $mpd->playlist;
60 if ( $#$pl - $song < $ARGV{new} ) {
61 my $new = $ARGV{new} - ( $#$pl - $song );
62 eval { $mpd->add( $files[ rand @files ] ) for 1..$new };
65 } continue {
66 usleep $ARGV{sleep} * 1000 * 1000;
69 exit; # should not be there...
72 __END__
75 =head1 NAME
77 mpd-dynamic - a dynamic playlist for mpd
80 =head1 USAGE
82 mpd-dynamic
85 =head1 VERSION
87 This is mpd-dynamic version 0.2
90 =head1 DESCRIPTION
92 This program implements a dynamic playlist for mpd.
94 MPD (music player daemon) is a cool music player, but it lacks a dynamic
95 playlist. A dynamic playlist is a playlist that will change automatically
96 over time.
98 In particular, it will remove already played songs (keeping at most a given
99 number) and add new songs to the playlist so it never fall short of songs.
101 Note that since mpd is a daemon needing no gui to work, C<mpd-dynamic> is
102 also a daemon. That is, it will fork and do all its work from the background.
103 This way, you can fire C<mpd> and C<mpd-dynamic> and forget completely
104 about your music (especially since C<mpd-dynamic> is a low-resource program):
105 it will just be there! :-)
108 =head1 OPTIONS
110 =item -c[onf] <file>
112 Path of configuration file. Defaults to C<~/.mpd/mpd-dynamic.conf>.
114 =for Euclid:
115 file.type: readable
116 file.default: "$ENV{HOME}/.mpd/mpd-dynamic.conf"
119 =item -o[ld] <old>
121 Number of old tracks to keep in the backlog. Defaults to 10.
123 =for Euclid:
124 old.type: integer >= 0
125 old.default: 10
128 =item -n[ew] <new>
130 Number of new tracks to keep in the to-be-played playlist. Defaults to 10.
132 =for Euclid:
133 new.type: integer > 0
134 new.default: 10
137 =item -s[leep] <sleep>
139 Time spent sleeping (in seconds) before checking if playlist should be updated.
141 =for Euclid:
142 sleep.type: number > 0
143 sleep.default: 5
146 =item -d[ebug]
148 Run mpd-dynamic in debug mode. In particular, the program will not daemonize
149 itself.
152 =head1 AUTHOR
154 Jerome Quelin <jquelin@cpan.org>
157 =head1 COPYRIGHT
159 Copyright (c) 2007 Jerome Quelin <jquelin@cpan.org>
161 This program is free software; you can redistribute it and/or
162 modify it under the same terms as Perl itself.