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.
17 use Getopt
::Euclid qw
[ :minimal_keys
];
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
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");
62 tie
( %ratings, 'DB_File', $ARGV{ratings
}, O_RDONLY
)
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");
74 debug
("adding [$random]\n");
75 eval { $mpd->playlist->add( decode
('utf-8', $random) ) };
76 debug
("error: $@\n") if $@
;
78 untie %ratings if $istied;
82 usleep
$ARGV{sleep} * 1000 * 1000; # microseconds
85 exit; # should not be there...
88 return unless $ARGV{debug
};
90 my ($s,$m,$h) = ( localtime(time) )[0,1,2,3,6];
91 my $date = sprintf "%02d:%02d:%02d", $h, $m, $s;
100 mpd-dynamic - a dynamic playlist for mpd
105 mpd-dynamic [options]
110 This is mpd-dynamic version 0.4
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! :-)
133 =head2 General behaviour
135 You can customize the usage of mpd-dynamic with the following options:
142 Number of old tracks to keep in the backlog. Defaults to 10.
145 old.type: integer >= 0
151 Number of new tracks to keep in the to-be-played playlist. Defaults to 10.
154 new.type: integer > 0
158 =item -s[leep] <sleep>
160 Time spent sleeping (in seconds) before checking if playlist should be
161 updated. Default to 5 seconds.
164 sleep.type: number > 0
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
181 encoding.type: string
182 encoding.default: 'utf-8'
193 Print the usual program information
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.
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:
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>.
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.
230 min.type: integer > 0
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.