[project @ 5746]
[audio-mpd.git] / bin / mpd-dynamic
blob5bb70c9df173e9dad06fa6c49fb6e5e6d81bfc32
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 Proc::Daemon;
24 use Readonly;
27 # program constants.
28 Readonly my $OLD => 10; # number of old songs to keep
29 Readonly my $NEW => 10; # number of new songs to have at every moment
30 Readonly my $SLEEP => 5; # time to sleep before checking for changes
33 Proc::Daemon::Init;
36 my $song = 0;
37 my $playlist = 0;
38 my $mpd = Audio::MPD->new;
40 # fetch list of songs known by mpd.
41 my @files = $mpd->collection->all_pathes;
44 while (1) { # endless loop
45 my $status;
46 eval { $status = $mpd->status };
47 next if $@;
49 if ( $status->playlist > $playlist
50 || defined $status->song && $status->song != $song ) {
51 # playlist and/or current song has changed
52 $playlist = $status->playlist;
53 $song = $status->song;
54 update_playlist();
57 } continue {
58 sleep $SLEEP;
61 exit; # should not be there...
65 sub update_playlist {
66 # keep at most $OLD songs.
67 if ( $song > $OLD ) {
68 my $old = $song - $OLD;
69 $mpd->delete(0) for 1..$old;
72 # add at most $NEW songs.
73 my $pl = $mpd->playlist;
74 if ( $#$pl - $song < $NEW ) {
75 my $new = $NEW - ( $#$pl - $song );
76 $mpd->add( $files[ rand @files ] ) for 1..$new;
81 __END__
84 =head1 NAME
86 mpd-dynamic - a dynamic playlist for mpd
89 =head1 USAGE
91 mpd-dynamic
94 =head1 DESCRIPTION
96 this program implements a dynamic playlist for mpd.
98 MPD (music player daemon) is a cool music player, but it lacks a dynamic
99 playlist. A dynamic playlist is a playlist that will change automatically
100 over time.
102 In particular, it will remove already played songs (keeping at most a given
103 number) and add new songs to the playlist so it never fall short of songs.
105 C<mpd-dynamic> is a low-resource program.
107 Note that since mpd is a daemon needing no gui to work, C<mpd-dynamic> is
108 also a daemon. That is, it will fork and do all its work from the background.
109 This way, you can fire C<mpd> and C<mpd-dynamic> and forget completely
110 about your music: it will just be there! :-)
113 =head1 AUTHOR
115 Jerome Quelin <jquelin@cpan.org>
118 =head1 COPYRIGHT
120 Copyright (c) 2007 Jerome Quelin <jquelin@cpan.org>
122 This program is free software; you can redistribute it and/or
123 modify it under the same terms as Perl itself.
126 =cut