[project @ 5774]
[audio-mpd.git] / bin / mpd-dynamic
blobb3ff90c9cec68bf48fbb1b7b5ca8e601c700703d
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 Getopt::Euclid qw[ :minimal_keys ];
25 use Proc::Daemon;
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
40 my $status;
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");
65 my %ratings;
66 my $istied =
67 tie( %ratings, 'DB_File', $ARGV{ratings}, O_RDONLY )
68 ? 1 : 0;
70 PICK_ONE:
71 for (1..$new) {
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");
77 redo PICK_ONE;
79 debug("will add [$random]\n");
80 eval { $mpd->add( $random ) };
81 debug("error: $@\n") if $@;
83 untie %ratings if $istied;
86 } continue {
87 usleep $ARGV{sleep} * 1000 * 1000; # microseconds
90 exit; # should not be there...
92 sub debug {
93 return unless $ARGV{debug};
94 my ($msg) = @_;
95 my ($s,$m,$h) = ( localtime(time) )[0,1,2,3,6];
96 my $date = sprintf "%02d:%02d:%02d", $h, $m, $s;
97 warn "$date $msg";
100 __END__
103 =head1 NAME
105 mpd-dynamic - a dynamic playlist for mpd
108 =head1 USAGE
110 mpd-dynamic [options]
113 =head1 VERSION
115 This is mpd-dynamic version 0.2
118 =head1 DESCRIPTION
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
125 over time.
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! :-)
137 =head1 OPTIONS
139 You can customize the usage of mpd-dynamic with the following options:
141 =over 4
143 =item -o[ld] <old>
145 Number of old tracks to keep in the backlog. Defaults to 10.
147 =for Euclid:
148 old.type: integer >= 0
149 old.default: 10
152 =item -n[ew] <new>
154 Number of new tracks to keep in the to-be-played playlist. Defaults to 10.
156 =for Euclid:
157 new.type: integer > 0
158 new.default: 10
161 =item -s[leep] <sleep>
163 Time spent sleeping (in seconds) before checking if playlist should be
164 updated. Default to 5 seconds.
166 =for Euclid:
167 sleep.type: number > 0
168 sleep.default: 5
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>.
176 =for Euclid:
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.
185 =for Euclid:
186 min.type: integer > 0
187 min.default: 4
190 =item -d[ebug]
192 Run mpd-dynamic in debug mode. In particular, the program will not daemonize
193 itself. Default to false.
196 =item --version
198 =item --usage
200 =item --help
202 =item --man
204 Print the usual program information
207 =back
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.
213 =head1 AUTHOR
215 Jerome Quelin <jquelin@cpan.org>
218 =head1 COPYRIGHT
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
225 version.
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.