Debian Codegraph @

bin/mpd-dynamic

raw
1 #!/usr/bin/perl
2 #
3 # This file is part of Audio-MPD
4 #
5 # This software is copyright (c) 2007 by Jerome Quelin.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10
11 use strict;
12 use warnings;
13
14 # PODNAME: mpd-dynamic
15 # ABSTRACT: a dynamic playlist for mpd
16
17 use Audio::MPD;
18 use DB_File;
19 use Encode;
20 use Getopt::Euclid qw[ :minimal_keys ];
21 use List::AllUtils qw{ shuffle };
22 use Proc::Daemon;
23 use Time::HiRes qw[ usleep ];
24
25
26 #
27 my $song = 0; # song currently playing
28 my $playlist = 0; # playlist version
29 my $mpd = Audio::MPD->new;
30
31 Proc::Daemon::Init unless $ARGV{debug};
32
33 # fetch list of songs known by mpd.
34
35 while (1) { # endless loop
36 my $status;
37 eval { $status = $mpd->status };
38 next if $@; # error while peaking status
39
40 # do playlist and/or current song have changed?
41 next unless $status->playlist > $playlist
42 || defined $status->song && $status->song != $song;
43 debug("checking playlist...\n");
44
45 # yup - update playlist & song.
46 $playlist = $status->playlist;
47 $song = $status->song // 0;
48
49 # keep at most $ARGV{old} songs.
50 if ( $song > $ARGV{old} ) {
51 my $old = $song - $ARGV{old};
52 debug( "need to remove $old songs\n" );
53 eval { $mpd->playlist->delete(0) for 1..$old };
54 }
55
56 # add at most $ARGV{new} songs.
57 my @pl = $mpd->playlist->as_items;
58 if ( $#pl - $song < $ARGV{new} ) {
59 my $new = $ARGV{new} - ( $#pl - $song );
60 debug("need to add $new songs\n");
61
62 my %ratings;
63 my $istied =
64 tie( %ratings, 'DB_File', $ARGV{ratings}, O_RDONLY )
65 ? 1 : 0;
66
67 my @files = $mpd->collection->all_pathes;
68 die "Please set up mpd's audio collection before running mpd-dynamic"
69 unless @files;
70
71 my @oksongs;
72 if ( $istied ) {
73 @oksongs =
74 shuffle
75 grep { $ratings{$_} >= $ARGV{min} }
76 grep { $ratings{$_} != 0 }
77 grep { exists $ratings{$_} }
78 @files;
79 } else {
80 @oksongs = shuffle @files;
81 }
82 my @newones = splice @oksongs, 0, $new;
83 foreach my $path (@newones) {
84 my $song = encode('utf-8', $path);
85 debug("adding [$song]\n");
86 eval { $mpd->playlist->add($path) };
87 debug("error: $@\n") if $@;
88 }
89 untie %ratings if $istied;
90 }
91
92 } continue {
93 usleep $ARGV{sleep} * 1000 * 1000; # microseconds
94 }
95
96 exit; # should not be there...
97
98 sub debug {
99 return unless $ARGV{debug};
100 my ($msg) = @_;
101 my ($s,$m,$h) = ( localtime(time) )[0,1,2,3,6];
102 my $date = sprintf "%02d:%02d:%02d", $h, $m, $s;
103 warn "$date $msg";
104 }
105
106 __END__
107
108 =pod
109
110 =head1 NAME
111
112 mpd-dynamic - a dynamic playlist for mpd
113
114 =head1 VERSION
115
116 version 2.004
117
118 =head1 DESCRIPTION
119
120 This program implements a dynamic playlist for MPD, build on top of the
121 L<Audio::MPD> perl module.
122
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
125 automatically over time. In particular, it will remove already played
126 songs (keeping at most a given number of songs) and add new songs to the
127 playlist so it never fall short of songs.
128
129 Note that since mpd is a daemon needing no gui to work, C<mpd-dynamic> is
130 also a daemon. That is, it will fork and do all its work from the background.
131 This way, you can fire C<mpd> and C<mpd-dynamic> and forget completely
132 about your music (especially since C<mpd-dynamic> is a low-resource program):
133 it will just be there! :-)
134
135 =head1 USAGE
136
137 mpd-dynamic [options]
138
139 =head1 OPTIONS
140
141 =head2 General behaviour
142
143 You can customize the usage of mpd-dynamic with the following options:
144
145 =over 4
146
147 =item -o[ld] <old>
148
149 Number of old tracks to keep in the backlog. Defaults to 10.
150
151 =for Euclid: old.type: integer >= 0
152 old.default: 10
153
154 =item -n[ew] <new>
155
156 Number of new tracks to keep in the to-be-played playlist. Defaults to 10.
157
158 =for Euclid: new.type: integer > 0
159 new.default: 10
160
161 =item -s[leep] <sleep>
162
163 Time spent sleeping (in seconds) before checking if playlist should be
164 updated. Default to 5 seconds.
165
166 =for Euclid: sleep.type: number > 0
167 sleep.default: 5
168
169 =item -d[ebug]
170
171 Run mpd-dynamic in debug mode. In particular, the program will not daemonize
172 itself. Default to false.
173
174 =item -e[ncoding] <encoding>
175
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>.
179
180 =for Euclid: encoding.type: string
181 encoding.default: 'utf-8'
182
183 =item --version
184
185 =item --usage
186
187 =item --help
188
189 =item --man
190
191 Print the usual program information
192
193 =back
194
195 Note however that those flags are optional: since C<mpd-dynamic> comes with
196 some sane defaults, you can fire C<mpd-dynamic> as is.
197
198 =head2 Ratings
199
200 You can also take advantage of ratings if you want. With those options, songs
201 need to have at least a given rating (or no rating yet) to be inserted: this
202 way, you will only listen to your favorite songs!
203
204 Ratings can be created / updated with C<mpd-rate>.
205
206 Note that if you supply a non-existant rating db-file, the rating mechanism
207 will be ignored. The following options control the rating mechanism:
208
209 =over 4
210
211 =item -r[atings] <ratings>
212
213 The path of a db file with the ratings per song. The keys are the song path
214 (relative to MPD root), and the value is an integer (the rating). Default to
215 C<~/.mpd/ratings.db>.
216
217 =for Euclid: ratings.type: readable
218 ratings.default: "$ENV{HOME}/.mpd/ratings.db"
219
220 =item -m[in[imum]] <min>
221
222 The minimum rating for a song to be inserted in the playlist. Default to 4.
223
224 =for Euclid: min.type: integer > 0
225 min.default: 4
226
227 =back
228
229 =head1 AUTHOR
230
231 Jerome Quelin
232
233 =head1 COPYRIGHT AND LICENSE
234
235 This software is copyright (c) 2007 by Jerome Quelin.
236
237 This is free software; you can redistribute it and/or modify it under
238 the same terms as the Perl 5 programming language system itself.
239
240 =cut