[project @ 5631]
[audio-mpd.git] / lib / Audio / MPD.pm
blob1b601138bc487fe4e71d22ab5d8d88908701b71e
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;
20 use warnings;
21 use strict;
23 use Audio::MPD::Collection;
24 use Audio::MPD::Item;
25 use Audio::MPD::Status;
26 use IO::Socket;
29 use base qw[ Class::Accessor::Fast ];
30 __PACKAGE__->mk_accessors( qw[ _host _password _port collection version ] );
33 our $VERSION = '0.14.0';
36 #--
37 # Constructor
40 # my $mpd = Audio::MPD->new( [$hostname], [$port], [$password] )
42 # This is the constructor for Audio::MPD. One can specify a $hostname, a
43 # $port, and a $password.
44 # If none is specified then defaults to environment vars MPD_HOST, MPD_PORT
45 # and MPD_PASSWORD. If those aren't set, defaults to 'localhost', 6600 and ''.
47 sub new {
48 my $class = shift;
49 my ($host, $port, $password) = @_;
51 # use mpd defaults.
52 $host = $ENV{MPD_HOST} || 'localhost' unless defined $host;
53 $port = $ENV{MPD_PORT} || '6600' unless defined $port;
54 $password = $ENV{MPD_PASSWORD} || '' unless defined $password;
56 # create & bless the object.
57 my $self = {
58 _host => $host,
59 _port => $port,
60 _password => $password,
62 bless $self, $class;
64 # create the collection object and store it.
65 $self->collection( Audio::MPD::Collection->new($self) );
67 # try to issue a ping to test connection - this can die.
68 $self->ping;
70 return $self;
74 #--
75 # Private methods
79 # my @result = $mpd->_send_command( $command );
81 # This method is central to the module. It is responsible for interacting with
82 # mpd by sending the $command and reading output - that will be returned as an
83 # array of chomped lines (status line will not be returned).
85 # Note that currently, this method will connect to mpd before sending any
86 # command, and will disconnect after the command has been issued. This scheme
87 # is far from optimal, but allows us not to care about timeout disconnections.
89 # /!\ Note that we're using high-level, blocking sockets. This means that if
90 # the mpd server is slow, or hangs for whatever reason, or even crash abruptly,
91 # the program will be hung forever in this sub. The POE::Component::Client::MPD
92 # module is way safer - you're advised to use it instead of Audio::MPD.
94 # This method can die on several conditions:
95 # - if the server cannot be reached,
96 # - if it's not an mpd server,
97 # - if the password is incorrect,
98 # - or if the command is an invalid mpd command.
99 # In the latter case, the mpd error message will be returned.
101 sub _send_command {
102 my ($self, $command) = @_;
104 # try to connect to mpd.
105 my $socket = IO::Socket::INET->new(
106 PeerAddr => $self->_host,
107 PeerPort => $self->_port
109 or die "Could not create socket: $!\n";
110 my $line;
112 # parse version information.
113 $line = $socket->getline;
114 chomp $line;
115 die "Not a mpd server - welcome string was: [$line]\n"
116 if $line !~ /^OK MPD (.+)$/;
117 $self->version($1);
119 # send password.
120 if ( $self->_password ) {
121 $socket->print( 'password ' . $self->_password . "\n" );
122 $line = $socket->getline;
123 die $line if $line =~ s/^ACK //;
126 # ok, now we're connected - let's issue the command.
127 $socket->print( $command );
128 my @output;
129 while (defined ( $line = $socket->getline ) ) {
130 chomp $line;
131 die $line if $line =~ s/^ACK //; # oops - error.
132 last if $line =~ /^OK/; # end of output.
133 push @output, $line;
136 # close the socket.
137 $socket->close;
139 return @output;
145 # Public methods
147 # -- MPD interaction: general commands
150 # $mpd->ping;
152 # Sends a ping command to the mpd server.
154 sub ping {
155 my ($self) = @_;
156 $self->_send_command( "ping\n" );
161 # my $version = $mpd->version;
163 # Return version of MPD server's connected.
165 # sub version {} # implemented as an accessor.
170 # $mpd->kill;
172 # Send a message to the MPD server telling it to shut down.
174 sub kill {
175 my ($self) = @_;
176 $self->_send_command("kill\n");
181 # $mpd->password( [$password] )
183 # Change password used to communicate with MPD server to $password.
184 # Empty string is assumed if $password is not supplied.
186 sub password {
187 my ($self, $passwd) = @_;
188 $passwd ||= '';
189 $self->_password($passwd);
190 $self->ping; # ping sends a command, and thus the password is sent
195 # $mpd->updatedb( [$path] );
197 # Force mpd to recan its collection. If $path (relative to MPD's music
198 # directory) is supplied, MPD will only scan it - otherwise, MPD will rescan
199 # its whole collection.
201 sub updatedb {
202 my ($self, $path) = @_;
203 $path ||= '';
204 $self->_send_command("update $path\n");
209 # my @handlers = $mpd->urlhandlers;
211 # Return an array of supported URL schemes.
213 sub urlhandlers {
214 my ($self) = @_;
215 my @handlers =
216 map { /^handler: (.+)$/ ? $1 : () }
217 $self->_send_command("urlhandlers\n");
218 return @handlers;
222 # -- MPD interaction: handling volume & output
225 # $mpd->volume( [+][-]$volume );
227 # Sets the audio output volume percentage to absolute $volume.
228 # If $volume is prefixed by '+' or '-' then the volume is changed relatively
229 # by that value.
231 sub volume {
232 my ($self, $volume) = @_;
234 if ($volume =~ /^(-|\+)(\d+)/ ) {
235 my $current = $self->status->volume;
236 $volume = $1 eq '+' ? $current + $2 : $current - $2;
238 $self->_send_command("setvol $volume\n");
243 # $mpd->output_enable( $output );
245 # Enable the specified audio output. $output is the ID of the audio output.
247 sub output_enable {
248 my ($self, $output) = @_;
249 $self->_send_command("enableoutput $output\n");
254 # $mpd->output_disable( $output );
256 # Disable the specified audio output. $output is the ID of the audio output.
258 sub output_disable {
259 my ($self, $output) = @_;
260 $self->_send_command("disableoutput $output\n");
265 # -- MPD interaction: retrieving info from current state
268 # $mpd->stats;
270 # Return a hashref with the number of artists, albums, songs in the database,
271 # as well as mpd uptime, the playtime of the playlist / the database and the
272 # last update of the database.
274 sub stats {
275 my ($self) = @_;
276 my %kv =
277 map { /^([^:]+):\s+(\S+)$/ ? ($1 => $2) : () }
278 $self->_send_command( "stats\n" );
279 return \%kv;
284 # my $status = $mpd->status;
286 # Return an Audio::MPD::Status object with various information on current
287 # MPD server settings. Check the embedded pod for more information on the
288 # available accessors.
290 sub status {
291 my ($self) = @_;
292 my @output = $self->_send_command( "status\n" );
293 my $status = Audio::MPD::Status->new( @output );
294 return $status;
299 # my $list = $mpd->playlist;
301 # Return an arrayref of C<Audio::MPD::Item::Song>s, one for each of the
302 # songs in the current playlist.
304 sub playlist {
305 my ($self) = @_;
307 my @lines = $self->_send_command("playlistinfo\n");
308 my (@list, %param);
310 # parse lines in reverse order since "file:" comes first.
311 # therefore, let's first store every other parameter, and
312 # the "file:" line will trigger the object creation.
313 # of course, since we want to preserve the playlist order,
314 # this means that we're going to unshift the objects.
315 foreach my $line (reverse @lines) {
316 next unless $line =~ /^([^:]+):\s+(.+)$/;
317 $param{$1} = $2;
318 next unless $1 eq 'file'; # last param of this item
319 unshift @list, Audio::MPD::Item->new(%param);
320 %param = ();
322 return \@list;
327 # my $list = $mpd->pl_changes( $plversion );
329 # Return a list with all the songs (as API::Song objects) added to
330 # the playlist since playlist $plversion.
332 sub pl_changes {
333 my ($self, $plid) = @_;
335 my @lines = $self->_send_command("plchanges $plid\n");
336 my (%param, @list);
338 # parse lines in reverse order since "file:" comes first.
339 # therefore, let's first store every other parameter, and
340 # the "file:" line will trigger the object creation.
341 # of course, since we want to preserve the playlist order,
342 # this means that we're going to unshift the objects.
343 foreach my $line (reverse @lines) {
344 next unless $line =~ /^([^:]+):\s+(.+)$/;
345 $param{$1} = $2;
346 next unless $1 eq 'file'; # last param of this item
347 unshift @list, Audio::MPD::Item->new(%param);
348 %param = ();
350 return @list;
355 # my $song = $mpd->current;
357 # Return an C<Audio::MPD::Item::Song> representing the song currently playing.
359 sub current {
360 my ($self) = @_;
361 my @output = $self->_send_command("currentsong\n");
362 my %params = map { /^([^:]+):\s+(.+)$/ ? ($1=>$2) : () } @output;
363 return Audio::MPD::Item->new( %params );
368 # my $song = $mpd->song( [$song] )
370 # Return an C<Audio::MPD::Item::Song> representing the song number C<$song>.
371 # If C<$song> is not supplied, returns the current song.
373 sub song {
374 my ($self, $song) = @_;
375 return $self->current unless defined $song;
376 my @output = $self->_send_command("playlistinfo $song\n");
377 my %params = map { /^([^:]+):\s+(.+)$/ ? ($1=>$2) : () } @output;
378 return Audio::MPD::Item->new( %params );
383 # my $song = $mpd->songid( [$songid] )
385 # Return an C<Audio::MPD::Item::Song> representing the song with id C<$songid>.
386 # If C<$songid> is not supplied, returns the current song.
388 sub songid {
389 my ($self, $songid) = @_;
390 return $self->current unless defined $songid;
391 my @output = $self->_send_command("playlistid $songid\n");
392 my %params = map { /^([^:]+):\s+(.+)$/ ? ($1=>$2) : () } @output;
393 return Audio::MPD::Item->new( %params );
397 # -- MPD interaction: altering settings
400 # $mpd->repeat( [$repeat] );
402 # Set the repeat mode to $repeat (1 or 0). If $repeat is not specified then
403 # the repeat mode is toggled.
405 sub repeat {
406 my ($self, $mode) = @_;
408 $mode = not $self->status->repeat
409 unless defined $mode; # toggle if no param
410 $mode = $mode ? 1 : 0; # force integer
411 $self->_send_command("repeat $mode\n");
416 # $mpd->random( [$random] );
418 # Set the random mode to $random (1 or 0). If $random is not specified then
419 # the random mode is toggled.
421 sub random {
422 my ($self, $mode) = @_;
424 $mode = not $self->status->random
425 unless defined $mode; # toggle if no param
426 $mode = $mode ? 1 : 0; # force integer
427 $self->_send_command("random $mode\n");
432 # $mpd->fade( [$seconds] );
434 # Enable crossfading and set the duration of crossfade between songs. If
435 # $seconds is not specified or $seconds is 0, then crossfading is disabled.
437 sub fade {
438 my ($self, $value) = @_;
439 $value ||= 0;
440 $self->_send_command("crossfade $value\n");
444 # -- MPD interaction: controlling playback
447 # $mpd->play( [$song] );
449 # Begin playing playlist at song number $song. If no argument supplied,
450 # resume playing.
452 sub play {
453 my ($self, $number) = @_;
454 $number ||= '';
455 $self->_send_command("play $number\n");
459 # $mpd->playid( [$songid] );
461 # Begin playing playlist at song ID $songid. If no argument supplied,
462 # resume playing.
464 sub playid {
465 my ($self, $number) = @_;
466 $number ||= '';
467 $self->_send_command("playid $number\n");
472 # $mpd->pause( [$sate] );
474 # Pause playback. If $state is 0 then the current track is unpaused, if
475 # $state is 1 then the current track is paused.
477 # Note that if $state is not given, pause state will be toggled.
479 sub pause {
480 my ($self, $state) = @_;
481 $state ||= ''; # default is to toggle
482 $self->_send_command("pause $state\n");
487 # $mpd->stop;
489 # Stop playback.
491 sub stop {
492 my ($self) = @_;
493 $self->_send_command("stop\n");
498 # $mpd->next;
500 # Play next song in playlist.
502 sub next {
503 my ($self) = @_;
504 $self->_send_command("next\n");
508 # $mpd->prev;
510 # Play previous song in playlist.
512 sub prev {
513 my($self) = shift;
514 $self->_send_command("previous\n");
519 # $mpd->seek( $time, [$song]);
521 # Seek to $time seconds in song number $song. If $song number is not specified
522 # then the perl module will try and seek to $time in the current song.
524 sub seek {
525 my ($self, $time, $song) = @_;
526 $time ||= 0; $time = int $time;
527 $song = $self->status->song if not defined $song; # seek in current song
528 $self->_send_command( "seek $song $time\n" );
533 # $mpd->seekid( $time, $songid );
535 # Seek to $time seconds in song ID $songid. If $song number is not specified
536 # then the perl module will try and seek to $time in the current song.
538 sub seekid {
539 my ($self, $time, $song) = @_;
540 $time ||= 0; $time = int $time;
541 $song = $self->status->songid if not defined $song; # seek in current song
542 $self->_send_command( "seekid $song $time\n" );
546 # -- MPD interaction: handling playlist
549 # $mpd->add( $path );
551 # Add the song identified by $path (relative to MPD's music directory) to
552 # the current playlist. No return value.
554 sub add {
555 my ($self, $path) = @_;
556 $self->_send_command( qq[add "$path"\n] );
561 # $mpd->delete( $song [, $song [...] ] );
563 # Remove song number $song from the current playlist. No return value.
565 sub delete {
566 my ($self, @songs) = @_;
567 my $command =
568 "command_list_begin\n"
569 . join( '', map { "delete $_\n" } @songs )
570 . "command_list_end\n";
571 $self->_send_command( $command );
576 # $mpd->deleteid( $songid [, $songid [...] ]);
578 # Remove the specified $songid from the current playlist. No return value.
580 sub deleteid {
581 my ($self, @songs) = @_;
582 my $command =
583 "command_list_begin\n"
584 . join( '', map { "deleteid $_\n" } @songs )
585 . "command_list_end\n";
586 $self->_send_command( $command );
591 # $mpd->clear;
593 # Remove all the songs from the current playlist. No return value.
595 sub clear {
596 my ($self) = @_;
597 $self->_send_command("clear\n");
602 # $mpd->crop;
604 # Remove all of the songs from the current playlist *except* the current one.
606 sub crop {
607 my ($self) = @_;
609 my $status = $self->status;
610 my $cur = $status->song;
611 my $len = $status->playlistlength - 1;
613 my $command =
614 "command_list_begin\n"
615 . join( '', map { $_ != $cur ? "delete $_\n" : '' } reverse 0..$len )
616 . "command_list_end\n";
617 $self->_send_command( $command );
622 sub swap {
623 my ($self, $from, $to) = @_;
624 $self->_send_command("swap $from $to\n");
627 sub swapid {
628 my ($self, $from, $to) = @_;
629 $self->_send_command("swapid $from $to\n");
632 sub shuffle {
633 my ($self) = @_;
634 $self->_send_command("shuffle\n");
637 sub move {
638 my ($self, $song, $pos) = @_;
639 $self->_send_command("move $song $pos\n");
642 sub moveid {
643 my ($self, $song, $pos) = @_;
644 $self->_send_command("moveid $song $pos\n");
647 sub load {
648 my ($self, $playlist) = @_;
649 return unless defined $playlist;
650 $self->_send_command( qq[load "$playlist"\n] );
653 sub save {
654 my ($self, $playlist) = @_;
655 return unless defined $playlist;
656 $self->_send_command( qq[save "$playlist"\n] );
658 =begin FIXME
660 if(!$self->_process_feedback)
662 # Does the playlist already exist?
663 if(${$self->get_error}[0] eq '56' && $config{'OVERWRITE_PLAYLIST'})
665 $self->rm($playlist);
666 $self->save($playlist);
667 return 1;
670 return 1;
672 =end FIXME
674 =cut
678 sub rm {
679 my ($self, $playlist) = @_;
680 return unless defined $playlist;
681 $self->_send_command( qq[rm "$playlist"\n] );
688 # -- MPD interaction: searching collection
691 # my @songs = $mpd->search( $type, $string, [$strict] );
693 # Search through MPD's database of music for matching songs, and return a
694 # list of associated Audio::MPD::Item::Song.
696 # $type is the field to search in: "title","artist","album", or "filename",
697 # and $string is the keyword(s) to seach for. If $strict is true then only
698 # exact matches are returned.
700 sub search {
701 my ($self, $type, $string, $strict) = @_;
703 my $command = (!defined($strict) || $strict == 0 ? 'search' : 'find');
704 my @lines = $self->_send_command( qq[$command $type "$string"\n] );
705 my (@list, %param);
707 # parse lines in reverse order since "file:" comes first.
708 # therefore, let's first store every other parameter, and
709 # the "file:" line will trigger the object creation.
710 # of course, since we want to preserve the playlist order,
711 # this means that we're going to unshift the objects.
712 foreach my $line (reverse @lines) {
713 next unless $line =~ /^([^:]+):\s+(.+)$/;
714 $param{$1} = $2;
715 next unless $1 eq 'file'; # last param of this item
716 unshift @list, Audio::MPD::Item->new(%param);
717 %param = ();
719 return @list;
723 # recursively, but only dirs & files
724 sub listall {
725 my ($self, $path) = @_;
726 $path ||= '';
727 return $self->_send_command( qq[listall "$path"\n] );
728 # FIXME: return item::songs / item::directory
731 # recursive, with all tags
732 sub listallinfo {
733 my ($self, $path) = @_;
734 $path ||= '';
735 my @lines = $self->_send_command( qq[listallinfo "$path"\n] );
737 my @results;
738 my %element;
739 foreach my $line (@lines) {
740 chomp $line;
741 next unless $line =~ /^([^:]+):\s(.+)$/;
742 if ($1 eq 'file') {
743 push @results, { %element } if %element;
744 %element = ();
746 $element{$1} = $2;
748 push @results, { %element };
749 return @results;
750 # FIXME: return item::songs / item::directory
753 # only in the current path, all tags
754 sub lsinfo {
755 my ($self, $path) = @_;
756 $path ||= '';
758 my @lines = $self->_send_command( qq[lsinfo "$path"\n] );
760 my @results;
761 my %element;
762 foreach my $line (@lines) {
763 chomp $line;
764 next unless $line =~ /^([^:]+):\s(.+)$/;
765 if ($1 eq 'file' || $1 eq 'playlist' || $1 eq 'directory') {
766 push @results, { %element } if %element;
767 %element = ();
769 $element{$1} = $2;
771 push @results, { %element };
772 return @results;
773 # FIXME: return item::songs / item::directory
777 ###############################################################
778 # CUSTOM METHODS #
779 #-------------------------------------------------------------#
780 # This section contains all methods not directly accessing #
781 # MPD, but may be useful for most people using the module. #
782 ###############################################################
784 sub searchadd {
785 my ($self, $type, $string) = @_;
786 my @results = $self->search($type, $string);
788 return unless @results;
790 my $command =
791 "command_list_begin\n"
792 . join( '', map { qq[add "$_->{file}"\n] } @results )
793 . "command_list_end\n";
794 $self->_send_command( $command );
799 sub get_time_format {
800 my ($self) = shift;
802 # Get the time from MPD; example: 49:395 (seconds so far:total seconds)
803 my ($sofar, $total) = split /:/, $self->status->time;
804 return sprintf "%d:%02d/%d:%02d",
805 ($sofar / 60), # minutes so far
806 ($sofar % 60), # seconds - minutes so far
807 ($total / 60), # minutes total
808 ($total % 60);# seconds - minutes total
811 sub get_time_info {
812 my ($self) = @_;
814 # Get the time from MPD; example: 49:395 (seconds so far:total seconds)
815 my ($sofar, $total) = split /:/, $self->status->time;
816 my $left = $total - $sofar;
818 # Store seconds for everything
819 my $rv = {};
820 $rv->{seconds_so_far} = $sofar;
821 $rv->{seconds_total} = $total;
822 $rv->{seconds_left} = $left;
824 # Store the percentage; use one decimal point
825 $rv->{percentage} =
826 $rv->{seconds_total}
827 ? 100*$rv->{seconds_so_far}/$rv->{seconds_total}
828 : 0;
829 $rv->{percentage} = sprintf "%.1f",$rv->{percentage};
832 # Parse the time so far
833 my $min_so_far = ($sofar / 60);
834 my $sec_so_far = ($sofar % 60);
836 $rv->{time_so_far} = sprintf("%d:%02d", $min_so_far, $sec_so_far);
837 $rv->{minutes_so_far} = sprintf("%00d", $min_so_far);
838 $rv->{seconds_so_far} = sprintf("%00d", $sec_so_far);
841 # Parse the total time
842 my $min_tot = ($total / 60);
843 my $sec_tot = ($total % 60);
845 $rv->{time_total} = sprintf("%d:%02d", $min_tot, $sec_tot);
846 $rv->{minutes} = $min_tot;
847 $rv->{seconds} = $sec_tot;
849 # Parse the time left
850 my $min_left = ($left / 60);
851 my $sec_left = ($left % 60);
852 $rv->{time_left} = sprintf("-%d:%02d", $min_left, $sec_left);
854 return $rv;
862 __END__
864 =pod
866 =head1 NAME
868 Audio::MPD - Class for talking to MPD (Music Player Daemon) servers
871 =head1 SYNOPSIS
873 use Audio::MPD;
875 my $mpd = Audio::MPD->new();
876 $mpd->play();
877 sleep 10;
878 $mpd->next();
881 =head1 DESCRIPTION
883 Audio::MPD gives a clear object-oriented interface for talking to and
884 controlling MPD (Music Player Daemon) servers. A connection to the MPD
885 server is established as soon as a new Audio::MPD object is created.
886 Commands are then sent to the server as the class's methods are called.
889 =head1 METHODS
891 =head2 Constructor
893 =over 4
895 =item new( [$host] [, $port] [, $password] )
897 This is the constructor for Audio::MPD. One can specify a $hostname, a
898 $port, and a $password.
900 If none is specified then defaults to environment vars MPD_HOST, MPD_PORT
901 and MPD_PASSWORD. If those aren't set, defaults to 'localhost', 6600 and ''.
903 =back
906 =head2 Controlling the server
908 =over 4
910 =item $mpd->ping()
912 Sends a ping command to the mpd server.
915 =item $mpd->version()
917 Return the version number for the server we are connected to.
920 =item $mpd->kill()
922 Send a message to the MPD server telling it to shut down.
925 =item $mpd->password( [$password] )
927 Change password used to communicate with MPD server to $password.
928 Empty string is assumed if $password is not supplied.
931 =item $mpd->updatedb( [$path] )
933 Force mpd to recan its collection. If $path (relative to MPD's music directory)
934 is supplied, MPD will only scan it - otherwise, MPD will rescan its whole
935 collection.
938 =item $mpd->urlhandlers()
940 Return an array of supported URL schemes.
943 =back
946 =head2 Handling volume & output
948 =over 4
950 =item $mpd->volume( [+][-]$volume )
952 Sets the audio output volume percentage to absolute $volume.
953 If $volume is prefixed by '+' or '-' then the volume is changed relatively
954 by that value.
957 =item $mpd->output_enable( $output )
959 Enable the specified audio output. $output is the ID of the audio output.
962 =item $mpd->output_disable( $output )
964 Disable the specified audio output. $output is the ID of the audio output.
966 =back
969 =head2 Retrieving info from current state
971 =over 4
973 =item $mpd->stats()
975 Return a hashref with the number of artists, albums, songs in the database,
976 as well as mpd uptime, the playtime of the playlist / the database and the
977 last update of the database
980 =item $mpd->status()
982 Return an C<Audio::MPD::Status> object with various information on current
983 MPD server settings. Check the embedded pod for more information on the
984 available accessors.
987 =item $mpd->playlist( )
989 Return an arrayref of C<Audio::MPD::Item::Song>s, one for each of the
990 songs in the current playlist.
993 =item $mpd->pl_changes( $plversion )
995 Return a list with all the songs (as API::Song objects) added to
996 the playlist since playlist $plversion.
999 =item $mpd->current( )
1001 Return an C<Audio::MPD::Item::Song> representing the song currently playing.
1004 =item $mpd->song( [$song] )
1006 Return an C<Audio::MPD::Item::Song> representing the song number C<$song>. If
1007 C<$song> is not supplied, returns the current song.
1010 =item $mpd->songid( [$songid] )
1012 Return an C<Audio::MPD::Item::Song> representing the song with id C<$songid>.
1013 If C<$songid> is not supplied, returns the current song.
1015 =back
1018 =head2 Altering MPD settings
1020 =over 4
1022 =item $mpd->repeat( [$repeat] )
1024 Set the repeat mode to $repeat (1 or 0). If $repeat is not specified then
1025 the repeat mode is toggled.
1028 =item $mpd->random( [$random] )
1030 Set the random mode to $random (1 or 0). If $random is not specified then
1031 the random mode is toggled.
1034 =item $mpd->fade( [$seconds] )
1036 Enable crossfading and set the duration of crossfade between songs.
1037 If $seconds is not specified or $seconds is 0, then crossfading is disabled.
1039 =back
1042 =head2 Controlling playback
1044 =over 4
1046 =item $mpd->play( [$song] )
1048 Begin playing playlist at song number $song. If no argument supplied,
1049 resume playing.
1052 =item $mpd->playid( [$songid] )
1054 Begin playing playlist at song ID $songid. If no argument supplied,
1055 resume playing.
1058 =item $mpd->pause( [$state] )
1060 Pause playback. If C<$state> is 0 then the current track is unpaused,
1061 if $state is 1 then the current track is paused.
1063 Note that if C<$state> is not given, pause state will be toggled.
1066 =item $mpd->stop()
1068 Stop playback.
1071 =item $mpd->next()
1073 Play next song in playlist.
1076 =item $mpd->prev()
1078 Play previous song in playlist.
1081 =item $mpd->seek( $time, [$song])
1083 Seek to $time seconds in song number $song. If $song number is not specified
1084 then the perl module will try and seek to $time in the current song.
1087 =item $mpd->seekid( $time, $songid )
1089 Seek to $time seconds in song ID $songid. If $song number is not specified
1090 then the perl module will try and seek to $time in the current song.
1092 =back
1095 =head2 Handling playlist
1097 =over 4
1099 =item $mpd->add( $path )
1101 Add the song identified by $path (relative to MPD's music directory) to the
1102 current playlist. No return value.
1105 =item $mpd->delete( $song )
1107 Remove song number $song from the current playlist. No return value.
1110 =item $mpd->deleteid( $songid )
1112 Remove the specified $songid from the current playlist. No return value.
1115 =item $mpd->clear()
1117 Remove all the songs from the current playlist. No return value.
1120 =item $mpd->crop()
1122 Remove all of the songs from the current playlist *except* the
1123 song currently playing.
1126 =item $mpd->swap( $song1, $song2 )
1128 Swap positions of song number $song1 and $song2 on the current playlist. No
1129 return value.
1132 =item $mpd->swapid( $songid1, $songid2 )
1134 Swap the postions of song ID $songid1 with song ID $songid2 on the current
1135 playlist. No return value.
1138 =item $mpd->move( $song, $newpos )
1140 Move song number $song to the position $newpos. No return value.
1143 =item $mpd->moveid( $songid, $newpos )
1145 Move song ID $songid to the position $newpos. No return value.
1148 =item $mpd->shuffle()
1150 Shuffle the current playlist. No return value.
1153 =item $mpd->load( $playlist )
1155 Load list of songs from specified $playlist file. No return value.
1158 =item $mpd->save( $playlist )
1160 Save the current playlist to a file called $playlist in MPD's playlist
1161 directory. No return value.
1164 =item $mpd->rm( $playlist )
1166 Delete playlist named $playlist from MPD's playlist directory. No return value.
1168 =back
1171 =head2 Retrieving information from current playlist
1173 =over 4
1175 =item $mpd->get_time_format( )
1177 Returns the current position and duration of the current song.
1178 String is formatted at "M:SS/M:SS", with current time first and total time
1179 after.
1182 =item $mpd->get_time_info( )
1184 Return current timing information in various different formats
1185 contained in a hashref with the following keys:
1187 =over 4
1189 =item minutes_so_far
1191 =item seconds_so_far
1193 =item time_so_far
1195 =item minutes
1197 =item seconds
1199 =item percentage
1201 =item time_total
1203 =item seconds_total
1205 =item seconds_left
1207 =item time_left
1209 =back
1211 =back
1214 =head2 Searching the collection
1216 To search the collection, use the C<collection()> accessor, returning the
1217 associated C<Audio::MPD::Collection> object. You will then be able to call:
1219 $mpd->collection->random_song();
1221 See C<Audio::MPD::Collection> documentation for more details on available
1222 methods.
1224 =over 4
1226 =item $mpd->search( $type, $string, [$strict] )
1228 Search through MPD's database of music for matching songs, and return a
1229 list of associated Audio::MPD::Item::Song.
1231 $type is the field to search in: "title","artist","album", or "filename", and
1232 $string is the keyword(s) to seach for. If $strict is true then only exact
1233 matches are returned.
1236 =item $mpd->searchadd( $type, $string )
1238 Perform the same action as $mpd->search(), but add any
1239 matching songs to the current playlist, instead of just returning
1240 information about them.
1243 =item $mpd->listall( [$path] )
1245 Return an array of all the songs in the music database.
1246 If $path is specified, then it only returns songs matching
1247 the directory/path.
1250 =item $mpd->listallinfo( [$path] )
1252 Returns an array of hashes containing all the paths and metadata about
1253 songs in the music database. If $path is specified, then it only
1254 returns songs matching the directory/path.
1257 =item $mpd->lsinfo( [$directory] )
1259 Returns an array of hashes containing all the paths and metadata about
1260 songs in the specified directory. If no directory is specified, then only
1261 the songs/directories in the root directory are listed.
1263 =back
1266 =head1 SEE ALSO
1268 You can find more information on the mpd project on its homepage at
1269 L<http://www.musicpd.org>, or its wiki L<http://mpd.wikia.com>.
1271 Regarding this Perl module, you can report bugs on CPAN via
1272 L<http://rt.cpan.org/Public/Bug/Report.html?Queue=Audio-MPD>.
1274 Audio::MPD development takes place on <audio-mpd@googlegroups.com>: feel free
1275 to join us. (use L<http://groups.google.com/group/audio-mpd> to sign in). Our
1276 subversion repository is located at L<https://svn.musicpd.org>.
1280 =head1 AUTHORS
1282 Jerome Quelin <jquelin@cpan.org>
1284 Original code by Tue Abrahamsen <tue.abrahamsen@gmail.com>, documented by
1285 Nicholas J. Humfrey <njh@aelius.com>.
1289 =head1 COPYRIGHT AND LICENSE
1291 Copyright (c) 2005 Tue Abrahamsen <tue.abrahamsen@gmail.com>
1293 Copyright (c) 2006 Nicholas J. Humfrey <njh@aelius.com>
1295 Copyright (c) 2007 Jerome Quelin <jquelin@cpan.org>
1298 This program is free software; you can redistribute it and/or modify
1299 it under the terms of the GNU General Public License as published by
1300 the Free Software Foundation; either version 2 of the License, or
1301 (at your option) any later version.
1303 This program is distributed in the hope that it will be useful,
1304 but WITHOUT ANY WARRANTY; without even the implied warranty of
1305 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1306 GNU General Public License for more details.
1308 =cut