update pod to be more coherent in email addresses + see also
[audio-mpd.git] / bin / mpd-dynamic
blob9807d17d343c1784ce5f0b2858d94f8572a6a3a1
1 #!/usr/bin/perl
3 # This file is part of Audio::MPD
4 # Copyright (c) 2007-2008 Jerome Quelin, all rights reserved.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the same terms as Perl itself.
11 use strict;
12 use warnings;
14 use Audio::MPD;
15 use DB_File;
16 use Encode;
17 use Getopt::Euclid qw[ :minimal_keys ];
18 use Proc::Daemon;
19 use Time::HiRes qw[ usleep ];
23 my $song = 0; # song currently playing
24 my $playlist = 0; # playlist version
25 my $mpd = Audio::MPD->new;
27 Proc::Daemon::Init unless $ARGV{debug};
29 # fetch list of songs known by mpd.
30 my @files = $mpd->collection->all_pathes;
33 while (1) { # endless loop
34 my $status;
35 eval { $status = $mpd->status };
36 next if $@; # error while peaking status
38 # do playlist and/or current song have changed?
39 next unless $status->playlist > $playlist
40 || defined $status->song && $status->song != $song;
41 debug("checking playlist...\n");
43 # yup - update playlist & song.
44 $playlist = $status->playlist;
45 $song = $status->song;
47 # keep at most $ARGV{old} songs.
48 if ( $song > $ARGV{old} ) {
49 my $old = $song - $ARGV{old};
50 debug( "need to remove $old songs\n" );
51 eval { $mpd->playlist->delete(0) for 1..$old };
54 # add at most $ARGV{new} songs.
55 my @pl = $mpd->playlist->as_items;
56 if ( $#pl - $song < $ARGV{new} ) {
57 my $new = $ARGV{new} - ( $#pl - $song );
58 debug("need to add $new songs\n");
60 my %ratings;
61 my $istied =
62 tie( %ratings, 'DB_File', $ARGV{ratings}, O_RDONLY )
63 ? 1 : 0;
65 PICK_ONE:
66 for (1..$new) {
67 my $random = encode('utf-8', $files[ rand @files ]);
68 if ( $istied && exists $ratings{$random}
69 && $ratings{$random} != 0
70 && $ratings{$random} < $ARGV{min} ) {
71 debug("rating too low: $ratings{$random} [$random]\n");
72 redo PICK_ONE;
74 debug("adding [$random]\n");
75 eval { $mpd->playlist->add( decode('utf-8', $random) ) };
76 debug("error: $@\n") if $@;
78 untie %ratings if $istied;
81 } continue {
82 usleep $ARGV{sleep} * 1000 * 1000; # microseconds
85 exit; # should not be there...
87 sub debug {
88 return unless $ARGV{debug};
89 my ($msg) = @_;
90 my ($s,$m,$h) = ( localtime(time) )[0,1,2,3,6];
91 my $date = sprintf "%02d:%02d:%02d", $h, $m, $s;
92 warn "$date $msg";
95 __END__
98 =head1 NAME
100 mpd-dynamic - a dynamic playlist for mpd
103 =head1 USAGE
105 mpd-dynamic [options]
108 =head1 VERSION
110 This is mpd-dynamic version 0.4
113 =head1 DESCRIPTION
115 This program implements a dynamic playlist for MPD, build on top of the
116 C<Audio::MPD> perl module.
118 MPD (music player daemon) is a cool music player, but it lacks a dynamic
119 playlist. A dynamic playlist is a playlist that will change
120 automatically over time. In particular, it will remove already played
121 songs (keeping at most a given number of songs) and add new songs to the
122 playlist so it never fall short of songs.
124 Note that since mpd is a daemon needing no gui to work, C<mpd-dynamic> is
125 also a daemon. That is, it will fork and do all its work from the background.
126 This way, you can fire C<mpd> and C<mpd-dynamic> and forget completely
127 about your music (especially since C<mpd-dynamic> is a low-resource program):
128 it will just be there! :-)
131 =head1 OPTIONS
133 =head2 General behaviour
135 You can customize the usage of mpd-dynamic with the following options:
138 =over 4
140 =item -o[ld] <old>
142 Number of old tracks to keep in the backlog. Defaults to 10.
144 =for Euclid:
145 old.type: integer >= 0
146 old.default: 10
149 =item -n[ew] <new>
151 Number of new tracks to keep in the to-be-played playlist. Defaults to 10.
153 =for Euclid:
154 new.type: integer > 0
155 new.default: 10
158 =item -s[leep] <sleep>
160 Time spent sleeping (in seconds) before checking if playlist should be
161 updated. Default to 5 seconds.
163 =for Euclid:
164 sleep.type: number > 0
165 sleep.default: 5
168 =item -d[ebug]
170 Run mpd-dynamic in debug mode. In particular, the program will not daemonize
171 itself. Default to false.
174 =item -e[ncoding] <encoding>
176 Print debug messages with this encoding. Since mpd-dynamic is meant to be a
177 silent daemon, this option will not be used outside of debug mode. Default
178 to C<utf-8>.
180 =for Euclid:
181 encoding.type: string
182 encoding.default: 'utf-8'
185 =item --version
187 =item --usage
189 =item --help
191 =item --man
193 Print the usual program information
196 =back
198 Note however that those flags are optional: since C<mpd-dynamic> comes with
199 some sane defaults, you can fire C<mpd-dynamic> as is.
202 =head2 Ratings
204 You can also take advantage of ratings if you want. With those options, songs
205 need to have at least a given rating (or no rating yet) to be inserted: this
206 way, you will only listen to your favorite songs!
208 Note that if you supply a non-existant rating db-file, the rating mechanism
209 will be ignored. The following options control the rating mechanism:
212 =over 4
214 =item -r[atings] <ratings>
216 The path of a db file with the ratings per song. The keys are the song path
217 (relative to MPD root), and the value is an integer (the rating). Default to
218 C<~/.mpd/ratings.db>.
220 =for Euclid:
221 ratings.type: readable
222 ratings.default: "$ENV{HOME}/.mpd/ratings.db"
225 =item -m[in[imum]] <min>
227 The minimum rating for a song to be inserted in the playlist. Default to 4.
229 =for Euclid:
230 min.type: integer > 0
231 min.default: 4
234 =back
237 =head1 AUTHOR
239 Jerome Quelin, C<< <jquelin@cpan.org> >>
242 =head1 COPYRIGHT & LICENSE
244 Copyright (c) 2007-2008 Jerome Quelin, all rights reserved.
246 This program is free software; you can redistribute it and/or modify
247 it under the same terms as Perl itself.
249 =cut