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
25 use Getopt
::Euclid qw
[ :minimal_keys
];
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
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");
68 tie
( %ratings, 'DB_File', $ARGV{ratings
}, O_RDONLY
)
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");
80 debug
("will add [$random]\n");
81 eval { $mpd->add( $random ) };
82 debug
("error: $@\n") if $@
;
84 untie %ratings if $istied;
88 usleep
$ARGV{sleep} * 1000 * 1000; # microseconds
91 exit; # should not be there...
94 return unless $ARGV{debug
};
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);
106 mpd-dynamic - a dynamic playlist for mpd
111 mpd-dynamic [options]
116 This is mpd-dynamic version 0.4
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
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! :-)
140 =head2 General behaviour
142 You can customize the usage of mpd-dynamic with the following options:
149 Number of old tracks to keep in the backlog. Defaults to 10.
152 old.type: integer >= 0
158 Number of new tracks to keep in the to-be-played playlist. Defaults to 10.
161 new.type: integer > 0
165 =item -s[leep] <sleep>
167 Time spent sleeping (in seconds) before checking if playlist should be
168 updated. Default to 5 seconds.
171 sleep.type: number > 0
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
188 encoding.type: string
189 encoding.default: 'utf-8'
200 Print the usual program information
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.
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:
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>.
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.
237 min.type: integer > 0
246 Jerome Quelin <jquelin@cpan.org>
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
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.