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
24 use Getopt
::Euclid qw
[ :minimal_keys
];
26 use Time
::HiRes qw
[ usleep
];
28 Proc
::Daemon
::Init
unless $ARGV{debug
};
31 my $song = 0; # song currently playing
32 my $playlist = 0; # playlist version
33 my $mpd = Audio
::MPD
->new;
35 # fetch list of songs known by mpd.
36 my @files = $mpd->collection->all_pathes;
39 while (1) { # endless loop
41 eval { $status = $mpd->status };
42 next if $@
; # error while peaking status
44 # do playlist and/or current song have changed?
45 next unless $status->playlist > $playlist
46 || defined $status->song && $status->song != $song;
47 debug
("checking playlist...\n");
49 # yup - update playlist & song.
50 $playlist = $status->playlist;
51 $song = $status->song;
53 # keep at most $ARGV{old} songs.
54 if ( $song > $ARGV{old
} ) {
55 my $old = $song - $ARGV{old
};
56 eval { $mpd->delete(0) for 1..$old };
59 # add at most $ARGV{new} songs.
60 my $pl = $mpd->playlist;
61 if ( $#$pl - $song < $ARGV{new} ) {
62 my $new = $ARGV{new
} - ( $#$pl - $song );
63 debug
("need to add $new songs\n");
67 tie
( %ratings, 'DB_File', $ARGV{ratings
}, O_RDONLY
)
72 my $random = $files[ rand @files ];
73 if ( $istied && exists $ratings{$random}
74 && $ratings{$random} != 0
75 && $ratings{$random} < $ARGV{min
} ) {
76 debug
("rating too low: $ratings{$random} [$random]\n");
79 debug
("will add [$random]\n");
80 eval { $mpd->add( $random ) };
81 debug
("error: $@\n") if $@
;
83 untie %ratings if $istied;
87 usleep
$ARGV{sleep} * 1000 * 1000; # microseconds
90 exit; # should not be there...
93 return unless $ARGV{debug
};
95 my ($s,$m,$h) = ( localtime(time) )[0,1,2,3,6];
96 my $date = sprintf "%02d:%02d:%02d", $h, $m, $s;
105 mpd-dynamic - a dynamic playlist for mpd
110 mpd-dynamic [options]
115 This is mpd-dynamic version 0.2
120 This program implements a dynamic playlist for MPD, build on top of the
121 C<Audio::MPD> perl module.
123 MPD (music player daemon) is a cool music player, but it lacks a dynamic
124 playlist. A dynamic playlist is a playlist that will change automatically
127 In particular, it will remove already played songs (keeping at most a given
128 number) and add new songs to the playlist so it never fall short of songs.
130 Note that since mpd is a daemon needing no gui to work, C<mpd-dynamic> is
131 also a daemon. That is, it will fork and do all its work from the background.
132 This way, you can fire C<mpd> and C<mpd-dynamic> and forget completely
133 about your music (especially since C<mpd-dynamic> is a low-resource program):
134 it will just be there! :-)
139 You can customize the usage of mpd-dynamic with the following options:
145 Number of old tracks to keep in the backlog. Defaults to 10.
148 old.type: integer >= 0
154 Number of new tracks to keep in the to-be-played playlist. Defaults to 10.
157 new.type: integer > 0
161 =item -s[leep] <sleep>
163 Time spent sleeping (in seconds) before checking if playlist should be
164 updated. Default to 5 seconds.
167 sleep.type: number > 0
171 =item -r[atings] <ratings>
173 The path of a db file with the ratings per song. The keys are the song path,
174 and the value is an integer (the rating). Default to C<~/.mpd/ratings.db>.
177 ratings.type: readable
178 ratings.default: "$ENV{HOME}/.mpd/ratings.db"
181 =item -m[in[imum]] <min>
183 The minimum rating for a song to be inserted in the playlist. Default to 4.
186 min.type: integer > 0
192 Run mpd-dynamic in debug mode. In particular, the program will not daemonize
193 itself. Default to false.
204 Print the usual program information
209 Note however that those flags are optional: since C<mpd-dynamic> comes with
210 some sane defaults, you can fire C<mpd-dynamic> as is.
215 Jerome Quelin <jquelin@cpan.org>
220 Copyright (c) 2007 Jerome Quelin <jquelin@cpan.org>
222 This program is free software; you can redistribute it and/or modify it under
223 the terms of the GNU General Public License as published by the Free Software
224 Foundation; either version 2 of the License, or (at your option) any later
227 This program is distributed in the hope that it will be useful, but WITHOUT
228 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
229 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.