[project @ 5837]
[audio-mpd.git] / bin / mpd-dynamic
blob2712021965b92af1461f48ec1f615f724d83909d
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->playlist->delete(0) for 1..$old };
60 # add at most $ARGV{new} songs.
61 my @pl = $mpd->playlist->as_items;
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->playlist->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
126 automatically over time. In particular, it will remove already played
127 songs (keeping at most a given number of songs) and add new songs to the
128 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! :-)
137 =head1 OPTIONS
139 =head2 General behaviour
141 You can customize the usage of mpd-dynamic with the following options:
144 =over 4
146 =item -o[ld] <old>
148 Number of old tracks to keep in the backlog. Defaults to 10.
150 =for Euclid:
151 old.type: integer >= 0
152 old.default: 10
155 =item -n[ew] <new>
157 Number of new tracks to keep in the to-be-played playlist. Defaults to 10.
159 =for Euclid:
160 new.type: integer > 0
161 new.default: 10
164 =item -s[leep] <sleep>
166 Time spent sleeping (in seconds) before checking if playlist should be
167 updated. Default to 5 seconds.
169 =for Euclid:
170 sleep.type: number > 0
171 sleep.default: 5
174 =item -d[ebug]
176 Run mpd-dynamic in debug mode. In particular, the program will not daemonize
177 itself. Default to false.
180 =item -e[ncoding] <encoding>
182 Print debug messages with this encoding. Since mpd-dynamic is meant to be a
183 silent daemon, this option will not be used outside of debug mode. Default
184 to C<utf-8>.
186 =for Euclid:
187 encoding.type: string
188 encoding.default: 'utf-8'
191 =item --version
193 =item --usage
195 =item --help
197 =item --man
199 Print the usual program information
202 =back
204 Note however that those flags are optional: since C<mpd-dynamic> comes with
205 some sane defaults, you can fire C<mpd-dynamic> as is.
208 =head2 Ratings
210 You can also take advantage of ratings if you want. With those options, songs
211 need to have at least a given rating (or no rating yet) to be inserted: this
212 way, you will only listen to your favorite songs!
214 Note that if you supply a non-existant rating db-file, the rating mechanism
215 will be ignored. The following options control the rating mechanism:
218 =over 4
220 =item -r[atings] <ratings>
222 The path of a db file with the ratings per song. The keys are the song path
223 (relative to MPD root), and the value is an integer (the rating). Default to
224 C<~/.mpd/ratings.db>.
226 =for Euclid:
227 ratings.type: readable
228 ratings.default: "$ENV{HOME}/.mpd/ratings.db"
231 =item -m[in[imum]] <min>
233 The minimum rating for a song to be inserted in the playlist. Default to 4.
235 =for Euclid:
236 min.type: integer > 0
237 min.default: 4
240 =back
243 =head1 AUTHOR
245 Jerome Quelin <jquelin@cpan.org>
248 =head1 COPYRIGHT
250 Copyright (c) 2007 Jerome Quelin <jquelin@cpan.org>
252 This program is free software; you can redistribute it and/or modify it under
253 the terms of the GNU General Public License as published by the Free Software
254 Foundation; either version 2 of the License, or (at your option) any later
255 version.
257 This program is distributed in the hope that it will be useful, but WITHOUT
258 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
259 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.