2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 2 of the License, or
5 # (at your option) any later version.
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 package Audio
::MPD
::Playlist
;
22 use Scalar
::Util qw
[ weaken
];
24 use base qw
[ Class
::Accessor
::Fast
];
25 __PACKAGE__
->mk_accessors( qw
[ _mpd
] );
28 #our ($VERSION) = '$Rev$' =~ /(\d+)/;
35 # my $collection = Audio::MPD::Playlist->new( $mpd );
37 # This will create the object, holding a back-reference to the Audio::MPD
38 # object itself (for communication purposes). But in order to play safe and
39 # to free the memory in time, this reference is weakened.
41 # Note that you're not supposed to call this constructor yourself, an
42 # Audio::MPD::Playlist is automatically created for you during the creation
43 # of an Audio::MPD object.
48 my $self = { _mpd
=> $mpd };
49 weaken
( $self->{_mpd
} );
58 # -- Playlist: retrieving information
61 # my @items = $pl->as_items;
63 # Return an array of C<Audio::MPD::Item::Song>s, one for each of the
64 # songs in the current playlist.
69 my @list = $self->_mpd->_cooked_command_as_items("playlistinfo\n");
75 # my @items = $pl->items_changed_since( $plversion );
77 # Return a list with all the songs (as API::Song objects) added to
78 # the playlist since playlist $plversion.
80 sub items_changed_since
{
81 my ($self, $plid) = @_;
82 return $self->_mpd->_cooked_command_as_items("plchanges $plid\n");
87 # -- Playlist: adding / removing songs
92 # Add the song identified by $path (relative to MPD's music directory) to
93 # the current playlist. No return value.
96 my ($self, $path) = @_;
97 $self->_mpd->_send_command( qq[add
"$path"\n] );
102 # $pl->delete( $song [, $song [...] ] );
104 # Remove song number $song (starting from 0) from the current playlist. No
108 my ($self, @songs) = @_;
110 "command_list_begin\n"
111 . join( '', map { "delete $_\n" } @songs )
112 . "command_list_end\n";
113 $self->_mpd->_send_command( $command );
118 # $pl->deleteid( $songid [, $songid [...] ]);
120 # Remove the specified $songid (as assigned by mpd when inserted in playlist)
121 # from the current playlist. No return value.
124 my ($self, @songs) = @_;
126 "command_list_begin\n"
127 . join( '', map { "deleteid $_\n" } @songs )
128 . "command_list_end\n";
129 $self->_mpd->_send_command( $command );
136 # Remove all the songs from the current playlist. No return value.
140 $self->_mpd->_send_command("clear\n");
147 # Remove all of the songs from the current playlist *except* the current one.
152 my $status = $self->_mpd->status;
153 my $cur = $status->song;
154 my $len = $status->playlistlength - 1;
157 "command_list_begin\n"
158 . join( '', map { $_ != $cur ?
"delete $_\n" : '' } reverse 0..$len )
159 . "command_list_end\n";
160 $self->_mpd->_send_command( $command );
164 # -- Playlist: changing playlist order
169 # Shuffle the current playlist. No return value.
173 $self->_mpd->_send_command("shuffle\n");
178 # $pl->swap( $song1, $song2 );
180 # Swap positions of song number $song1 and $song2 in the current playlist.
184 my ($self, $from, $to) = @_;
185 $self->_mpd->_send_command("swap $from $to\n");
190 # $pl->swapid( $songid1, $songid2 );
192 # Swap the postions of song ID $songid1 with song ID $songid2 in the
193 # current playlist. No return value.
196 my ($self, $from, $to) = @_;
197 $self->_mpd->_send_command("swapid $from $to\n");
202 # $pl->move( $song, $newpos );
204 # Move song number $song to the position $newpos. No return value.
207 my ($self, $song, $pos) = @_;
208 $self->_mpd->_send_command("move $song $pos\n");
213 # $pl->moveid( $songid, $newpos );
215 # Move song ID $songid to the position $newpos. No return value.
218 my ($self, $song, $pos) = @_;
219 $self->_mpd->_send_command("moveid $song $pos\n");
223 # -- Playlist: managing playlists
226 # $pl->load( $playlist );
228 # Load list of songs from specified $playlist file. No return value.
231 my ($self, $playlist) = @_;
232 $self->_mpd->_send_command( qq[load
"$playlist"\n] );
237 # $pl->save( $playlist );
239 # Save the current playlist to a file called $playlist in MPD's playlist
240 # directory. No return value.
243 my ($self, $playlist) = @_;
244 $self->_mpd->_send_command( qq[save
"$playlist"\n] );
249 # $pl->rm( $playlist )
251 # Delete playlist named $playlist from MPD's playlist directory. No
255 my ($self, $playlist) = @_;
256 $self->_mpd->_send_command( qq[rm
"$playlist"\n] );
268 Audio::MPD::Playlist - an object to mess MPD's playlist
273 my $song = $mpd->playlist->randomize;
278 C<Audio::MPD::Playlist> is a class meant to access & update MPD's
282 =head1 PUBLIC METHODS
290 This will create the object, holding a back-reference to the C<Audio::MPD>
291 object itself (for communication purposes). But in order to play safe and
292 to free the memory in time, this reference is weakened.
294 Note that you're not supposed to call this constructor yourself, an
295 C<Audio::MPD::Playlist> is automatically created for you during the creation
296 of an C<Audio::MPD> object.
301 =head2 Retrieving information
305 =item $pl->as_items( )
307 Return an array of C<Audio::MPD::Item::Song>s, one for each of the
308 songs in the current playlist.
311 =item $pl->items_changed_since( $plversion )
313 Return a list with all the songs (as API::Song objects) added to
314 the playlist since playlist $plversion.
320 =head2 Adding / removing songs
324 =item $pl->add( $path )
326 Add the song identified by C<$path> (relative to MPD's music directory) to the
327 current playlist. No return value.
330 =item $pl->delete( $song [, $song [...] ] )
332 Remove song number C<$song>s (starting from 0) from the current playlist. No
336 =item $pl->deleteid( $songid [, $songid [...] ] )
338 Remove the specified C<$songid>s (as assigned by mpd when inserted in playlist)
339 from the current playlist. No return value.
344 Remove all the songs from the current playlist. No return value.
349 Remove all of the songs from the current playlist *except* the
350 song currently playing.
356 =head2 Changing playlist order
360 =item $pl->shuffle( )
362 Shuffle the current playlist. No return value.
365 =item $pl->swap( $song1, $song2 )
367 Swap positions of song number C<$song1> and C<$song2> in the current
368 playlist. No return value.
371 =item $pl->swapid( $songid1, $songid2 )
373 Swap the postions of song ID C<$songid1> with song ID C<$songid2> in the
374 current playlist. No return value.
377 =item $pl->move( $song, $newpos )
379 Move song number C<$song> to the position C<$newpos>. No return value.
382 =item $pl->moveid( $songid, $newpos )
384 Move song ID C<$songid> to the position C<$newpos>. No return value.
390 =head2 Managing playlists
394 =item $pl->load( $playlist )
396 Load list of songs from specified C<$playlist> file. No return value.
399 =item $pl->save( $playlist )
401 Save the current playlist to a file called C<$playlist> in MPD's playlist
402 directory. No return value.
405 =item $pl->rm( $playlist )
407 Delete playlist named C<$playlist> from MPD's playlist directory. No
416 You can find more information on the mpd project on its homepage at
417 L<http://www.musicpd.org>, or its wiki L<http://mpd.wikia.com>.
419 Regarding this Perl module, you can report bugs on CPAN via
420 L<http://rt.cpan.org/Public/Bug/Report.html?Queue=Audio-MPD>.
422 Audio::MPD development takes place on <audio-mpd@googlegroups.com>: feel free
423 to join us. (use L<http://groups.google.com/group/audio-mpd> to sign in). Our
424 subversion repository is located at L<https://svn.musicpd.org>.
429 Jerome Quelin <jquelin@cpan.org>
432 =head1 COPYRIGHT AND LICENSE
434 Copyright (c) 2007 Jerome Quelin <jquelin@cpan.org>
437 This program is free software; you can redistribute it and/or modify
438 it under the terms of the GNU General Public License as published by
439 the Free Software Foundation; either version 2 of the License, or
440 (at your option) any later version.
442 This program is distributed in the hope that it will be useful,
443 but WITHOUT ANY WARRANTY; without even the implied warranty of
444 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
445 GNU General Public License for more details.