[project @ 5831]
[audio-mpd.git] / bin / mpd-dynamic
blob7bd830b2f65401a1f2f99c61be029f661dc1aa68
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 DB_File;
24 use Encode;
25 use Getopt::Euclid qw[ :minimal_keys ];
26 use Proc::Daemon;
27 use Time::HiRes qw[ usleep ];
29 Proc::Daemon::Init unless $ARGV{debug};
32 my $song = 0; # song currently playing
33 my $playlist = 0; # playlist version
34 my $mpd = Audio::MPD->new;
36 # fetch list of songs known by mpd.
37 my @files = $mpd->collection->all_pathes;
40 while (1) { # endless loop
41 my $status;
42 eval { $status = $mpd->status };
43 next if $@; # error while peaking status
45 # do playlist and/or current song have changed?
46 next unless $status->playlist > $playlist
47 || defined $status->song && $status->song != $song;
48 debug("checking playlist...\n");
50 # yup - update playlist & song.
51 $playlist = $status->playlist;
52 $song = $status->song;
54 # keep at most $ARGV{old} songs.
55 if ( $song > $ARGV{old} ) {
56 my $old = $song - $ARGV{old};
57 eval { $mpd->delete(0) for 1..$old };
60 # add at most $ARGV{new} songs.
61 my $pl = $mpd->playlist;
62 if ( $#$pl - $song < $ARGV{new} ) {
63 my $new = $ARGV{new} - ( $#$pl - $song );
64 debug("need to add $new songs\n");
66 my %ratings;
67 my $istied =
68 tie( %ratings, 'DB_File', $ARGV{ratings}, O_RDONLY )
69 ? 1 : 0;
71 PICK_ONE:
72 for (1..$new) {
73 my $random = $files[ rand @files ];
74 if ( $istied && exists $ratings{$random}
75 && $ratings{$random} != 0
76 && $ratings{$random} < $ARGV{min} ) {
77 debug("rating too low: $ratings{$random} [$random]\n");
78 redo PICK_ONE;
80 debug("will add [$random]\n");
81 eval { $mpd->add( $random ) };
82 debug("error: $@\n") if $@;
84 untie %ratings if $istied;
87 } continue {
88 usleep $ARGV{sleep} * 1000 * 1000; # microseconds
91 exit; # should not be there...
93 sub debug {
94 return unless $ARGV{debug};
95 my ($msg) = @_;
96 my ($s,$m,$h) = ( localtime(time) )[0,1,2,3,6];
97 my $date = sprintf "%02d:%02d:%02d", $h, $m, $s;
98 warn "$date " . encode($ARGV{encoding}, $msg);
101 __END__
104 =head1 NAME
106 mpd-dynamic - a dynamic playlist for mpd
109 =head1 USAGE
111 mpd-dynamic [options]
114 =head1 VERSION
116 This is mpd-dynamic version 0.4
119 =head1 DESCRIPTION
121 This program implements a dynamic playlist for MPD, build on top of the
122 C<Audio::MPD> perl module.
124 MPD (music player daemon) is a cool music player, but it lacks a dynamic
125 playlist. A dynamic playlist is a playlist that will change automatically
126 over time.
128 In particular, it will remove already played songs (keeping at most a given
129 number) and add new songs to the playlist so it never fall short of songs.
131 Note that since mpd is a daemon needing no gui to work, C<mpd-dynamic> is
132 also a daemon. That is, it will fork and do all its work from the background.
133 This way, you can fire C<mpd> and C<mpd-dynamic> and forget completely
134 about your music (especially since C<mpd-dynamic> is a low-resource program):
135 it will just be there! :-)
138 =head1 OPTIONS
140 =head2 General behaviour
142 You can customize the usage of mpd-dynamic with the following options:
145 =over 4
147 =item -o[ld] <old>
149 Number of old tracks to keep in the backlog. Defaults to 10.
151 =for Euclid:
152 old.type: integer >= 0
153 old.default: 10
156 =item -n[ew] <new>
158 Number of new tracks to keep in the to-be-played playlist. Defaults to 10.
160 =for Euclid:
161 new.type: integer > 0
162 new.default: 10
165 =item -s[leep] <sleep>
167 Time spent sleeping (in seconds) before checking if playlist should be
168 updated. Default to 5 seconds.
170 =for Euclid:
171 sleep.type: number > 0
172 sleep.default: 5
175 =item -d[ebug]
177 Run mpd-dynamic in debug mode. In particular, the program will not daemonize
178 itself. Default to false.
181 =item -e[ncoding] <encoding>
183 Print debug messages with this encoding. Since mpd-dynamic is meant to be a
184 silent daemon, this option will not be used outside of debug mode. Default
185 to C<utf-8>.
187 =for Euclid:
188 encoding.type: string
189 encoding.default: 'utf-8'
192 =item --version
194 =item --usage
196 =item --help
198 =item --man
200 Print the usual program information
203 =back
205 Note however that those flags are optional: since C<mpd-dynamic> comes with
206 some sane defaults, you can fire C<mpd-dynamic> as is.
209 =head2 Ratings
211 You can also take advantage of ratings if you want. With those options, songs
212 need to have at least a given rating (or no rating yet) to be inserted: this
213 way, you will only listen to your favorite songs!
215 Note that if you supply a non-existant rating db-file, the rating mechanism
216 will be ignored. The following options control the rating mechanism:
219 =over 4
221 =item -r[atings] <ratings>
223 The path of a db file with the ratings per song. The keys are the song path
224 (relative to MPD root), and the value is an integer (the rating). Default to
225 C<~/.mpd/ratings.db>.
227 =for Euclid:
228 ratings.type: readable
229 ratings.default: "$ENV{HOME}/.mpd/ratings.db"
232 =item -m[in[imum]] <min>
234 The minimum rating for a song to be inserted in the playlist. Default to 4.
236 =for Euclid:
237 min.type: integer > 0
238 min.default: 4
241 =back
244 =head1 AUTHOR
246 Jerome Quelin <jquelin@cpan.org>
249 =head1 COPYRIGHT
251 Copyright (c) 2007 Jerome Quelin <jquelin@cpan.org>
253 This program is free software; you can redistribute it and/or modify it under
254 the terms of the GNU General Public License as published by the Free Software
255 Foundation; either version 2 of the License, or (at your option) any later
256 version.
258 This program is distributed in the hope that it will be useful, but WITHOUT
259 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
260 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.