[project @ 5748]
[audio-mpd.git] / scripts / mpc.pl
blob451cd1bc47b576022c866134fb087a8d73111234
1 #!/usr/bin/perl -w
2 use strict;
3 use Encode;
4 use Audio::MPD;
5 use constant VERSION => '0.10.0';
7 my $x = Audio::MPD->new('localhost',6600);
9 # mpctime() - For getting the time in the same format as `mpc` writes it
10 sub mpctime
12 my($psf,$tst) = split /:/, $x->{'time'};
13 return sprintf("%d:%02d (%d%%)",
14 ($psf / 60), # minutes so far
15 ($psf % 60), # seconds - minutes so far
16 $psf/($tst/100)); # Percent
19 sub help
21 print "mpc version: ".VERSION."\n";
22 print "mpc\t\t\t\tDisplays status\n";
23 print "mpc add <filename>\t\tAdd a song to the current playlist\n";
24 print "mpc del <playlist #>\t\tRemove a song from the current playlist\n";
25 print "mpc play <number>\t\tStart playing a <number> (default: 1)\n";
26 print "mpc next\t\t\tPlay the next song in the current playlist\n";
27 print "mpc prev\t\t\tPlay the previous song in the current playlist\n";
28 print "mpc pause\t\t\tPauses the currently playing song\n";
29 print "mpc stop\t\t\tStop the currently playing song\n";
30 print "mpc seek <0-100>\t\tSeeks to the position specified in seconds\n";
31 print "mpc clear\t\t\tClears the current playlist\n";
32 print "mpc shuffle\t\t\tShuffle the current playlist\n";
33 print "mpc move <from> <to>\t\tMove song in playlist\n";
34 print "mpc playlist\t\t\tPrint the current playlist\n";
35 print "mpc listall [<song>]\t\tList all songs in the music dir\n";
36 print "mpc ls [<dir>]\t\t\tList the contents of <dir>\n";
37 print "mpc lsplaylists\t\t\tLists currently available playlists\n";
38 print "mpc load <file>\t\t\tLoad <file> as a playlist\n";
39 print "mpc save <file>\t\t\tSaves a playlist as <file>\n";
40 print "mpc rm <file>\t\t\tRemoves a playlist\n";
41 print "mpc volume [+-]<num>\t\tSets volume to <num> or adjusts by [+-]<num>\n";
42 print "mpc repeat <on|off>\t\tToggle repeat mode, or specify state\n";
43 print "mpc random <on|off>\t\tToggle random mode, or specify state\n";
44 #print "mpc search <type> <queries>\tSearch for a song\n";
45 print "mpc crossfade [sec]\t\tSet and display crossfade settings\n";
46 print "mpc update\t\t\tScans music directory for updates\n";
47 print "mpc version\t\t\tReports version of MPD\n";
48 print "For more information about these and other options look man 1 mpc\n";
49 exit;
52 # status() - For showing the current status
53 sub status
55 $x->_get_status;
56 my $repeat = ($x->{repeat} == 1 ? 'on ' : 'off'); # Let's show the repeat-status a bit nicer
57 my $random = ($x->{random} == 1 ? 'on ' : 'off'); # And the same for random
58 if($x->{state} eq 'play' || $x->{state} eq 'pause') { # If MPD is either playing or paused
59 print decode_utf8($x->get_title)."\n";
60 print "[".($x->{state} eq 'play' ? 'playing' : 'paused')."] #".($x->{song}+1)."/".$x->{playlistlength}."\t".mpctime."\n";
61 print "volume: ".$x->{volume}."% repeat: ".$repeat." random: ".$random."\n";
62 } elsif($x->{state} eq 'stop') { # If MPD is stopped, we don't show much
63 print "volume: ".$x->{volume}."% repeat: ".$repeat." random: ".$random."\n";
65 exit;
68 sub play { $x->play($ARGV[1] ? ($ARGV[1]-1) : 0); status; }
69 sub stop { $x->stop(); status; }
70 sub pause { $x->pause(); status; }
71 sub add {
72 if($ARGV[1] || $ARGV[1] eq '') {
73 $x->add($ARGV[1]);
74 } else {
75 while(<STDIN>) {
76 chomp;
77 $x->add($_);
81 sub del {
82 if(defined($ARGV[1])) {
83 $x->delete($ARGV[1]-1);
84 } else {
85 help;
88 sub next { $x->next(); status; }
89 sub prev { $x->prev(); status; }
90 sub seek {
91 if(defined($ARGV[1])) {
92 $x->seek($ARGV[1]); status;
93 } else {
94 help;
97 sub clear { $x->clear(); }
98 sub shuffle { $x->shuffle(); }
99 sub move {
100 if(defined($ARGV[1]) && defined($ARGV[2])) {
101 $x->move($ARGV[1]-1,$ARGV[2]-1);
102 } else {
103 help;
106 sub playlist
108 my $playlist = $x->playlist;
109 for(my $i = 0 ; $i < $x->{playlistlength} ; $i++)
111 my $title = ($playlist->[$i]{'Artist'} && $playlist->[$i]{'Title'} ? $playlist->[$i]{'Artist'}." - ".$playlist->[$i]{'Title'} : $playlist->[$i]{'file'});
112 print "#".($i+1).") ".$title."\n";
115 sub listall
117 my @list = $x->listall($ARGV[1]);
118 foreach my $item (@list)
120 print "$1\n" if $item =~ /(?:file):\s(.+)/;
123 sub ls
125 foreach my $tmp ($x->lsinfo($ARGV[1]))
127 my %hash = %{$tmp};
128 if($hash{'directory'} || $hash{'file'})
130 print $hash{'directory'} || $hash{'file'};
131 print "\n";
135 sub lsplaylists
137 #while(my %hash = $x->nextinfo)
138 foreach my $tmp ($x->lsinfo())
140 my %hash = %{$tmp};
141 print $hash{'playlist'}."\n" if $hash{'playlist'};
144 sub load {
145 if(defined($ARGV[1])) {
146 $x->load($ARGV[1]);
147 } else {
148 help;
151 sub save {
152 if(defined($ARGV[1])) {
153 $x->save($ARGV[1]);
154 } else {
155 help;
158 sub rm {
159 if(defined($ARGV[1])) {
160 $x->rm($ARGV[1]);
161 } else {
162 help;
165 sub volume {
166 if(defined($ARGV[1])) {
167 $x->set_volume($ARGV[1]);
168 status;
169 } else {
170 help;
173 sub repeat {
174 if(defined($ARGV[1])) {
175 $x->set_repeat($ARGV[1]);
176 status;
177 } else {
178 help;
181 sub random {
182 if(defined($ARGV[1])) {
183 $x->set_random($ARGV[1]);
184 status;
185 } else {
186 help;
189 sub search
191 die('No way!') if $ARGV[1] !~ /^(filename|artist|title|album)$/;
192 my @list = $x->search($ARGV[1],$ARGV[2]);
193 foreach my $hash (@list)
195 my %song = %$hash;
196 print $song{'file'}."\n";
199 sub crossfade {
200 if(defined($ARGV[1])) {
201 $x->set_fade($ARGV[1]);
202 } else {
203 help;
206 sub update { $x->updatedb(); }
207 sub version { print "mpd version: ".$x->{version}."\n"; }
209 # main() - Main sub
210 sub main
212 status if !$ARGV[0];
213 help if $ARGV[0] !~ /^(add|del|play|next|prev|pause|stop|seek|clear|shuffle|move|playlist|listall|ls|lsplaylists|load|save|rm|volume|repeat|random|search|crossfade|update|version)$/;
214 goto &{ $ARGV[0] };
217 # Let's start!
218 main;