]>
git.wh0rd.org - chrome-ext/music-player-client.git/blob - js/mpc.js
1 // Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain. Suck it.
3 function Mpc(socket
, cb_update_state
, debug_enabled
) {
5 this._cb_update_state
= cb_update_state
;
6 this._debug_enabled
= debug_enabled
;
7 this._queue
= ['init'];
11 Mpc
.prototype.log = function(lvl
, msg
, obj
) {
12 if (this._debug_enabled
& lvl
)
13 console
.log('mpc: ' + msg
, obj
);
16 Mpc
.prototype.err = function(msg
, obj
) {
17 console
.error('mpc: ' + msg
, obj
);
20 Mpc
.prototype.set_debug = function(val
) {
21 this._debug_enabled
= val
;
24 Mpc
.prototype.send = function(msg
) {
26 this._queue
.push(msg
);
27 this._socket
.send(msg
, function(x
) {
28 _this
.log(0x1, 'send: ' + msg
+ ':', x
);
32 Mpc
.prototype.recv_msg = function(lines
) {
33 curr
= this._queue
.shift();
34 this.log(0x2, 'recv: [' + curr
+ ']:', lines
.join('\n'));
35 curr
= curr
.split(' ');
38 // Needs to return a list of dicts (see above for dicts).
39 //case 'playlistinfo':
45 lines
.forEach(function(line
) {
46 i
= line
.indexOf(':');
48 return; // Ignores the OK line
49 key
= line
.substr(0, i
);
51 val
= line
.substr(i
+ 2);
55 // When mpd is stopped, it gives us back crap values for some things.
56 if ('state' in state
&& state
.state
== 'stop') {
57 if ('volume' in state
&& state
.volume
== '-1')
58 keys
.splice(keys
.indexOf('volume'), 1);
60 // Now merge the current state with the previous one so that we don't
61 // lose information like volume or song position.
62 curr_state
= this.state
;
63 keys
.forEach(function(key
) {
64 curr_state
[key
] = state
[key
];
67 this._cb_update_state(curr_state
);
70 this._cb_update_state(lines
, curr
);
75 Mpc
.prototype.recv = function(msg
) {
76 /* We can get back a bunch of responses in a row, so parse them out */
77 /* XXX: Do we have to handle partial reads ? like long playlists ... */
78 lines
= msg
.split('\n');
80 while (i
< lines
.length
) {
81 if (lines
[i
] == 'OK' || lines
[i
].substr(0, 3) == 'OK ') {
82 this.recv_msg(lines
.splice(0, i
+ 1));
90 * Command generator helpers.
93 Mpc
.__make_send_void = function(cmd
) {
94 return function() { this.send(cmd
); }
97 Mpc
.__make_send_arg1 = function(cmd
) {
100 this.err(cmd
+ ': function requires one argument');
102 this.send(cmd
+ ' ' + a1
);
106 Mpc
.__make_send_arg2 = function(cmd
) {
107 return function(a1
, a2
) {
108 if (a1
=== undefined || a2
=== undefined)
109 this.err(cmd
+ ': function requires two arguments');
111 this.send(cmd
+ ' ' + a1
+ ' ' + a2
);
115 Mpc
.__make_send_arg3 = function(cmd
) {
116 return function(a1
, a2
, a3
) {
117 if (a1
=== undefined || a2
=== undefined || a3
== undefined)
118 this.err(cmd
+ ': function requires three arguments');
120 this.send(cmd
+ ' ' + a1
+ ' ' + a2
+ ' ' + a3
);
124 Mpc
.__make_send_opt = function(cmd
) {
125 return function(arg
) {
126 if (arg
=== undefined)
128 this.send(cmd
+ ' ' + arg
);
132 Mpc
.__make_send_range = function(cmd
, min
, max
, def
) {
133 return function(arg
) {
134 if (arg
=== undefined)
136 if (arg
>= min
&& arg
<= max
)
137 this.send(cmd
+ ' ' + arg
);
139 this.err(cmd
+ ': arg must be [' + min
+ ',' + max
+ '] but got "' + arg
+ '"');
144 * Querying MPD's status
145 * http://www.musicpd.org/doc/protocol/ch03.html#idp118752
149 Mpc
.prototype.clearerror
= Mpc
.__make_send_void('clearerror');
151 Mpc
.prototype.currentsong
= Mpc
.__make_send_void('currentsong');
152 // idle [SUBSYSTEMS...]
155 Mpc
.prototype.status
= Mpc
.__make_send_void('status');
157 Mpc
.prototype.stats
= Mpc
.__make_send_void('stats');
161 * http://www.musicpd.org/doc/protocol/ch03s02.html
165 Mpc
.prototype.consume
= Mpc
.__make_send_range('consume', 0, 1, 1);
166 // crossfade {SECONDS}
167 Mpc
.prototype.crossfade
= Mpc
.__make_send_arg1('crossfade');
168 // mixrampdb {deciBels}
169 Mpc
.prototype.mixrampdb
= Mpc
.__make_send_arg1('mixrampdb');
170 // mixrampdelay {SECONDS|nan}
171 // Note: Probably should handle javascript NaN here.
172 Mpc
.prototype.mixrampdelay
= Mpc
.__make_send_arg1('mixrampdelay');
174 Mpc
.prototype.random
= Mpc
.__make_send_range('random', 0, 1, 1);
176 Mpc
.prototype.repeat
= Mpc
.__make_send_range('repeat', 0, 1, 1);
178 Mpc
.prototype.setvol
= Mpc
.__make_send_range('setvol', 0, 100);
180 Mpc
.prototype.single
= Mpc
.__make_send_range('single', 0, 1, 1);
181 // replay_gain_mode {MODE}
182 Mpc
.prototype.replay_gain_mode
= Mpc
.__make_send_arg1('replay_gain_mode');
183 // replay_gain_status
186 * Controlling playback
187 * http://www.musicpd.org/doc/protocol/ch03s03.html
191 Mpc
.prototype.next
= Mpc
.__make_send_void('next');
193 Mpc
.prototype.pause
= Mpc
.__make_send_range('pause', 0, 1, 1);
195 Mpc
.prototype.play
= Mpc
.__make_send_opt('play');
197 Mpc
.prototype.playid
= Mpc
.__make_send_opt('playid');
199 Mpc
.prototype.previous
= Mpc
.__make_send_void('previous');
200 // seek {SONGPOS} {TIME}
201 Mpc
.prototype.seek
= Mpc
.__make_send_arg2('seek');
202 // seekid {SONGID} {TIME}
203 Mpc
.prototype.seekid
= Mpc
.__make_send_arg2('seekid');
205 Mpc
.prototype.seekcur
= Mpc
.__make_send_arg1('seek');
207 Mpc
.prototype.stop
= Mpc
.__make_send_void('stop');
210 * The current playlist
211 * http://www.musicpd.org/doc/protocol/ch03s04.html
215 Mpc
.prototype.add
= Mpc
.__make_send_arg1('add');
216 // addid {URI} [POSITION]
217 // TODO: handle position
218 Mpc
.prototype.addid
= Mpc
.__make_send_arg1('addid');
220 Mpc
.prototype.clear
= Mpc
.__make_send_void('clear');
221 // delete [{POS} | {START:END}]
222 Mpc
.prototype.delete = Mpc
.__make_send_arg1('delete');
224 Mpc
.prototype.deleteid
= Mpc
.__make_send_arg1('deleteid');
225 // move [{FROM} | {START:END}] {TO}
226 Mpc
.prototype.move = Mpc
.__make_send_arg2('move');
227 // moveid {FROM} {TO}
228 Mpc
.prototype.moveid
= Mpc
.__make_send_arg2('moveid');
230 Mpc
.prototype.playlist
= Mpc
.__make_send_void('playlist');
231 // playlistfind {TAG} {NEEDLE}
232 Mpc
.prototype.playlistfind
= Mpc
.__make_send_arg2('playlistfind');
233 // playlistid {SONGID}
234 Mpc
.prototype.playlistid
= Mpc
.__make_send_arg1('playlistid');
235 // playlistinfo [[SONGPOS] | [START:END]]
236 Mpc
.prototype.playlistinfo
= Mpc
.__make_send_opt('playlistinfo');
237 // playlistsearch {TAG} {NEEDLE}
238 Mpc
.prototype.playlistsearch
= Mpc
.__make_send_arg2('playlistsearch');
239 // plchanges {VERSION}
240 Mpc
.prototype.plchanges
= Mpc
.__make_send_arg1('plchanges');
241 // plchangesposid {VERSION}
242 Mpc
.prototype.plchangesposid
= Mpc
.__make_send_arg1('plchangesposid');
243 // prio {PRIORITY} {START:END...}
244 Mpc
.prototype.prio
= Mpc
.__make_send_arg2('prio');
245 // prioid {PRIORITY} {ID...}
246 Mpc
.prototype.prioid
= Mpc
.__make_send_arg2('prioid');
247 // shuffle [START:END]
248 Mpc
.prototype.shuffle
= Mpc
.__make_send_opt('shuffle');
249 // swap {SONG1} {SONG2}
250 Mpc
.prototype.swap
= Mpc
.__make_send_arg2('swap');
251 // swapid {SONG1} {SONG2}
252 Mpc
.prototype.swapid
= Mpc
.__make_send_arg2('swapid');
256 * http://www.musicpd.org/doc/protocol/ch03s05.html
259 // listplaylist {NAME}
260 Mpc
.prototype.listplaylist
= Mpc
.__make_send_arg1('listplaylist');
261 // listplaylistinfo {NAME}
262 Mpc
.prototype.listplaylistinfo
= Mpc
.__make_send_arg1('listplaylistinfo');
264 Mpc
.prototype.listplaylists
= Mpc
.__make_send_void('listplaylists');
265 // load {NAME} [START:END]
266 // TODO: handle optional start:end
267 Mpc
.prototype.load
= Mpc
.__make_send_arg1('load');
268 // playlistadd {NAME} {URI}
269 Mpc
.prototype.playlistadd
= Mpc
.__make_send_arg2('playlistadd');
270 // playlistclear {NAME}
271 Mpc
.prototype.playlistclear
= Mpc
.__make_send_arg1('playlistclear');
272 // playlistdelete {NAME} {SONGPOS}
273 Mpc
.prototype.playlistdelete
= Mpc
.__make_send_arg2('playlistdelete');
274 // playlistmove {NAME} {SONGID} {SONGPOS}
275 Mpc
.prototype.playlistmove
= Mpc
.__make_send_arg3('playlistmove');
276 // rename {NAME} {NEW_NAME}
277 Mpc
.prototype.rename
= Mpc
.__make_send_arg2('rename');
279 Mpc
.prototype.rm
= Mpc
.__make_send_arg1('rm');
281 Mpc
.prototype.save
= Mpc
.__make_send_arg1('save');
285 * http://www.musicpd.org/doc/protocol/ch03s06.html
289 * Connection settings
290 * http://www.musicpd.org/doc/protocol/ch03s08.html
294 Mpc
.prototype.close
= Mpc
.__make_send_void('close');
296 Mpc
.prototype.kill
= Mpc
.__make_send_void('kill');
297 // password {PASSWORD}
298 Mpc
.prototype.password
= Mpc
.__make_send_arg1('password');
300 Mpc
.prototype.ping
= Mpc
.__make_send_void('ping');
303 * Audio output devices
304 * http://www.musicpd.org/doc/protocol/ch03s09.html
307 // disableoutput {ID}
308 Mpc
.prototype.disableoutput
= Mpc
.__make_send_arg1('disableoutput');
310 Mpc
.prototype.enableoutput
= Mpc
.__make_send_arg1('enableoutput');
312 Mpc
.prototype.outputs
= Mpc
.__make_send_void('outputs');
316 * http://www.musicpd.org/doc/protocol/ch03s10.html
320 Mpc
.prototype.config
= Mpc
.__make_send_void('config');
322 Mpc
.prototype.commands
= Mpc
.__make_send_void('commands');
324 Mpc
.prototype.notcommands
= Mpc
.__make_send_void('notcommands');
326 Mpc
.prototype.tagtypes
= Mpc
.__make_send_void('tagtypes');
328 Mpc
.prototype.urlhandlers
= Mpc
.__make_send_void('urlhandlers');
330 Mpc
.prototype.decoders
= Mpc
.__make_send_void('decoders');
334 * http://www.musicpd.org/doc/protocol/ch03s11.html
338 Mpc
.prototype.subscribe
= Mpc
.__make_send_arg1('subscribe');
339 // unsubscribe {NAME}
340 Mpc
.prototype.unsubscribe
= Mpc
.__make_send_arg1('unsubscribe');
342 Mpc
.prototype.channels
= Mpc
.__make_send_void('channels');
344 Mpc
.prototype.readmessages
= Mpc
.__make_send_void('readmessages');
345 // sendmessage {CHANNEL} {TEXT}
346 Mpc
.prototype.sendmessage
= Mpc
.__make_send_arg2('sendmessage');