-
Notifications
You must be signed in to change notification settings - Fork 9
/
mpd.js
2900 lines (2546 loc) · 88.3 KB
/
mpd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The global MPD function is the main interface and only global varialbe of the MPD.js library.
* This function returns an object representing an MPD web client.
* All other methods documented here are member functions of the object returned by a call to the MPD function.
*
* @example
* //EXAMPLE USAGE//
* //retrives a MPD client interface on port 8800
* var mpd_client = MPD(8800);
* //set handler for when the state changes
* mpd_client.on('StateChanged', updateUiFunction);
* @class
* @param {Integer} [_port] - the portnumber our client should try to cennect to our winsockifyed MPD instance with
* @param {String} [_host=document.URL] - hostname to try to connect to, defaults to the domain of the current page
* @param {String} [_password] - password to connect with (if needed)
*/
function MPD(_port, _host, _password){
/**
* this will be the final output interface, but it is used to refer to the client as a 'this' like object
* @lends MPD
*/
var self = {};
/********************\
|* public interface *|
\********************/
/**
* adds an event handler
* @instance
* @function
* @throws {Error} an Error if you try to listen to an invalid event type
* @param {String} event_name - what sort of event to listen for. must be one of the following: 'Error', 'Event', 'UnhandledEvent', 'AuthFailure', 'DatabaseChanging', 'DataLoaded', 'OutputChanged', 'StateChanged', 'QueueChanged', 'PlaylistsChanged', 'PlaylistChanged','Connect', 'Disconnect'
* @param {errorEventHandler|disconnectEventHandler|connectEventHandler|playlistsChangedEventHandler|queueChangedEventHandler|outputChangedEventHandler|stateChangedEventHandler|dataLoadedEventHandler|databaseChangingEventHandler|unhandledEventHandler|eventHandler|errorEventHandler} handler - function called when the given event happens
*/
self.on = on;
/**
* returns an object representation of the current state of MPD as the client understands it right now
* this does NOT map to the client's functional API
* @instance
* @returns {state} object representing the current state of MPD
*/
self.getState = function(){
var ret = cloneObject(_private.state);
//there are a few things we can't easily clone, but I made a clone method for those, so we can deal with this
if(_private.state.current_queue !== null){
ret.current_queue = _private.state.current_queue.clone();
}
return ret;
};
/**
* call to turn off logging to the console
* @instance
*/
self.disableLogging = function(){
_private.do_logging = false;
};
/**
* call to turn logging to the console on (debugging)
* @instance
*/
self.enableLogging = function(){
_private.do_logging = true;
};
/**
* return the port number this client was instansiated with and thet it is (attempting to) connect with
* @instance
* @returns {Integer} the port number the MPD client is (trying to be) connected to
*/
self.getPort = function(){
return _port;
};
/**
* return the host this client was instansiated with and thet it is (attempting to) connect with
* @instance
* @returns {String} the host name the MPD client is (trying to be) connected to
*/
self.getHost = function(){
return _host;
};
/**
* gets the protocol versing reported on connection
* @instance
* @returns {String} string desxriping the protocol version i.e. "1.18.0"
*/
self.getProtocolVersion = function(){
return _private.state.version;
};
/**
* retruns if we are connected or not
* @instance
* @returns {Boolean} true if we are connected, false if we are not
*/
self.isConnected = function(){
return _private.state.connected == true;
};
/**
* Returns a string enum describing the playback state
* @instance
* @returns {String} - 'play', 'pause', 'stop'
*/
self.getPlaystate = function(){
return _private.state.playstate;
};
/**
* returns the current volume
* @instance
* @returns {Float} between 0 and 1
*/
self.getVolume = function(){
return _private.state.volume;
};
/**
* returns if we are in repeat mode or not
* @instance
* @returns {Boolean} true if we are in repeat mode, false otherwise
*/
self.isRepeat = function(){
return _private.state.repeat == true;
};
/**
* returns if we are in single mode or not
* @instance
* @returns {Boolean} true if we are in single mode, false otherwise
*/
self.isSingle = function(){
return _private.state.single == true;
};
/**
* returns if we are in consume mode or not
* @instance
* @returns {Boolean} true if we are in consume mode, false otherwise
*/
self.isConsume = function(){
return _private.state.consume == true;
};
/**
* returns if we are in random playback mode or not
* @instance
* @returns {Boolean} true if we are in random mode, false otherwise
*/
self.isRandom = function(){
return _private.state.random == true;
};
/**
* ammount of time (in seconds) the MPD server is set to crossfade songs. 0 means disabled
* @instance
* @returns {Number} true if we are in random mode, false otherwise
*/
self.getCrossfadeTime = function(){
return _private.state.crossfade;
};
/**
* honestly don't know what this is, has something to do with some sort of fading mode I never use, but MPD reports it so I'm making an accessor for it in case someone else wants to use it
* @instance
* @returns {Float}
*/
self.getMixRampThreashold = function(){
return _private.state.mix_ramp_threshold;
};
/**
* gets the currently playing song
* @instance
* @function
* @returns {Song}
*/
self.getCurrentSong = getCurrentSong;
/**
* gets the length of the current song
* @instance
* @function
* @returns {Float}
*/
self.getCurrentSongDuration = getCurrentSongDuration;
/**
* gets the time of the current song. will calculate it based on the reported time, and how long it's been since that happened
* @instance
* @function
* @returns {Float}
*/
self.getCurrentSongTime = getCurrentSongTime;
/**
* get's the queue id of the currently playing song
* @instance
* @returns {Integer}
*/
self.getCurrentSongID = function(){
return _private.state.current_song.id;
};
/**
*gets the position on the queue of the song currently playing
* @instance
* @returns {Integer}
*/
self.getCurrentSongQueueIndex = function(){
return _private.state.current_song.queue_idx;
};
/**
* gets the song next to be played
* @instance
* @function
* @returns {Song}
*/
self.getNextSong =getNextSong;
/**
* gets the queue id of the next song to play
* @instance
* @returns {Integer}
*/
self.getNextSongID = function(){
return _private.state.next_song.id;
};
/**
* returns the position on the queue of the next song on the queue to play
* @instance
* @returns {Integer}
*/
self.getNextSongQueueIndex = function(){
return _private.state.next_song.queue_idx;
};
/**
* get the whole queue
* @instance
* @returns {Queue}
*/
self.getQueue = function(){
return _private.state.current_queue;
};
/**
* returns the version of the queue, this number changes every time the queue does
* @instance
* @returns {Integer}
*/
self.getQueueVersion = function(){
return _private.state.queue_version;
};
/**
* fetches a playlist from MPD identified by it's name
* @instance
* @param {String} playlist name - the name of the playlist you want
* @param {playlistCallback} onDone - function to call with the playlist when we get it
*/
self.getPlaylist = function(name, onDone){
var ret = null;
for(var i = 0; i<_private.state.playlists.length; i++){
if(_private.state.playlists[i].playlist==name){
issueCommands({
command:'listplaylistinfo "'+name+'"',
handler:getPlaylistHandler(onDone, i)
});
return;
}
};
onDone(null);
};
/**
* returns an array of strings that is the list of the names of all available saved playlists
* @instance
* @returns {String[]}
*/
self.getPlaylists = function(){
var playlists = [];
_private.state.playlists.forEach(function(playlist){
playlists.push(playlist.playlist);
});
return playlists;
};
/**
* returns an array of Output objects
* @instance
* @returns {Output[]}
*/
self.getOutputs = function(){
return _private.outputs.map(function(source){
return MPD.Output(self, source);
});
};
/**
* returns true if the output is enabled, false otherwise
* @param {Integer} id -- the identifier of the output
* @instance
*/
self.outputIsEnabled = function(id){
return _private.outputs[id].outputenabled == 1;
};
/**
* turns on the output specified by the id
* @param {Integer} id -- the identifier of the output to turn on
* @instance
*/
self.enableOutput = function(id){
issueCommands('enableoutput '+id);
};
/**
* turns off the output specified by the id
* @param {Integer} id -- the identifier of the output to turn off
* @instance
*/
self.disableOutput = function(id){
issueCommands('disableoutput '+id);
};
/**
* turns on consume mode
* @instance
*/
self.enablePlayConsume = function(){
issueCommands('consume 1');
};
/**
* turns off consume mode
* @instance
*/
self.disablePlayConsume = function(){
issueCommands('consume 0');
};
/**
* turns on crossfade
* @param {String} time -- time to crossfade in seconds, 0 to disable
* @instance
*/
self.setCrossfade = function(time) {
issueCommands('crossfade '+time);
};
/**
* turns on random play mode
* @instance
*/
self.enableRandomPlay = function(){
issueCommands('random 1');
};
/**
* turns off random play mode
* @instance
*/
self.disableRandomPlay = function(){
issueCommands('random 0');
};
/**
* turns on repeat play mode
* @instance
*/
self.enableRepeatPlay = function(){
issueCommands('repeat 1');
};
/**
* turns of repeat play mode
* @instance
*/
self.disableRepeatPlay = function(){
issueCommands('repeat 0');
};
/**
* turns on single play mode
* @instance
*/
self.enableSinglePlay = function(){
issueCommands('single 1');
};
/**
* turns of single play mode
* @instance
*/
self.disableSinglePlay = function(){
issueCommands('single 0');
};
/**
* Sets the threshold at which songs will be overlapped. Like crossfading but doesn't fade the track volume, just overlaps. The songs need to have MixRamp tags added by an external tool. 0dB is the normalized maximum volume so use negative values, I prefer -17dB. In the absence of mixramp tags crossfading will be used. See http: // sourceforge.net/projects/mixramp
* @instance
* @param {Float} decibels
*/
self.setMixRampDb = function(decibels){
issueCommands('mixrampdb '+decibels);
};
/**
* Additional time subtracted from the overlap calculated by mixrampdb. A value of "nan" disables MixRamp overlapping and falls back to crossfading.
* @instance
* @param {(float|string)} seconds - time in seconds or "nan" to disable
*/
self.setMixRampDelay = function(seconds){
issueCommands('mixrampdelay '+seconds);
};
/**
* Sets volume, the range of volume is 0-1.
* @instance
* @param {Float} volume - 0-1
*/
self.setVolume = function(volume){
volume = Math.min(1,volume);
volume = Math.max(0,volume);
issueCommands('setvol '+Math.round(volume*100));
};
/**
* Begins playing if not playing already. optional parameter starts playing a particular song
* @instance
* @param {Integer} [queue_position=<current song>] - the song to start playing
*/
self.play = function(queue_position){
if(typeof queue_position != 'undefined'){
issueCommands('play '+queue_position);
}
else{
issueCommands('play');
}
};
/**
* Begins playing the playlist at song identified by the passed song_id.
* @instance
* @param {Integer} song_id - the queue id of the song you want to start playing
*/
self.playById = function(song_id){
issueCommands('playid '+song_id);
};
/**
* pauses/resumes playing
* @instance
* @param {Boolean} [do_pause=true] - true if you want to pause, false if you want to be unpaused
*/
self.pause = function(do_pause){
if(typeof do_pause == 'undefined' || do_pause){
issueCommands('pause 1');
}
else{
issueCommands('pause 0');
}
};
/**
* Plays next song in the queue.
* @instance
*/
self.next = function(){
issueCommands('next');
};
/**
* Plays previous song in the queue.
* @instance
*/
self.previous = function(){
issueCommands('previous');
};
/**
* Seeks to the position time (in seconds) within the current song. If prefixed by '+' or '-', then the time is relative to the current playing position.
* @instance
* @param {(float|string)} - what point in the current song to seek to or string with a signed float in it for relative seeking. i.e. "+0.1" to seek 0.1 seconds into the future, "-0.1" to seek 0.1 seconds into the past
*/
self.seek = function(time){
issueCommands('seekid '+_private.state.current_song.id+' '+time);
};
/**
* Stops playing.
* @instance
*/
self.stop = function(){
issueCommands('stop');
};
/**
* Adds the file to the playlist (directories add recursively).
* @instance
* @param {String} pathname - of a single file or directory. relative to MPD's mussic root directory
*/
self.addSongToQueueByFile = function(filename){
issueCommands('add "'+filename+'"');
};
/**
* Clears the current queue
* @instance
*/
self.clearQueue = function(){
issueCommands('clear');
};
/**
* Deletes a song from the queue
* @instance
* @param {Integer} position - index into the queue to the song you don't want to be on the queue any more
*/
self.removeSongFromQueueByPosition = function(position){
issueCommands('delete '+position);
};
/**
* Deletes a range of songs from the playlist.
* @instance
* @param {Integer} start - the queue index of the first song on the playlist you want to remove
* @param {Integer} end - the queue index of the last song on the playlist you want to remove
*/
self.removeSongsFromQueueByRange = function(start, end){
issueCommands('delete '+start+' '+end);
};
/**
* Deletes the song identified with the passed queue id from the playlist
* @instance
* @param {Integer} id - the queue id of the song you want to remove from the queue
*/
self.removeSongFromQueueById = function(id){
issueCommands('deleteid '+id);
};
/**
* a song from one position on the queue to a different position
* @instance
* @param {Integer} position - the position of the song to move
* @param {Integer} to - where you want the sang to go
*/
self.moveSongOnQueueByPosition = function(position, to){
issueCommands('move '+position+' '+to);
};
/**
* moves a range of songs on the queue
* @instance
* @param {Integer} start - the queue index of the first song on the queue you want to move
* @param {Integer} end - the queue index of the last song on the queue you want to move
* @param {Integer} to - the queue index were the first song should end up
*/
self.moveSongsOnQueueByPosition = function(start, end, to){
issueCommands('move '+start+':'+end+' '+to);
};
/**
* moves the song identified with the passed queue id to the passed queue index
* @instance
* @param {Integer} id - queue id of the song you want to move
* @param {Integer} to - the queue indes you want it to be
*/
self.moveSongOnQueueById = function(id, to){
issueCommands('moveid '+id+' '+to);
};
/**
* Shuffles the current playlist.
* @instance
*/
self.shuffleQueue = function(){
issueCommands('shuffle');
};
/**
* Swaps the positions of two songs identified by their queue indexes
* @instance
* @param {Integer} pos1 - queue index of the first song
* @param {Integer} pos2 - queue index of the second song
*/
self.swapSongsOnQueueByPosition = function(pos1, pos2){
issueCommands('swap '+pos1+' '+pos2);
};
/**
* Swaps the positions of two songs identified by their queue ids
* @instance
* @param {Integer} id1 - queue id of the first song
* @param {Integer} id2 - queue id of the second song
*/
self.swapSongsOnQueueById = function(id1, id2){
issueCommands('swapid '+id1+' '+id2);
};
/**
* Loads the given playlist to the end of the current queue.
* @instance
* @param {String} playlist_name - the name of the playlist you want to append to the queue
*/
self.appendPlaylistToQueue = function(playlist_name){
issueCommands('load "'+playlist_name+'"');
};
/**
* Loads the given playlist into the current queue replacing it.
* @instance
* @param {String} playlist_name - the name of the playlist you want to append to the queue
*/
self.loadPlaylistIntoQueue = function(playlist_name){
issueCommands([
'clear',
'load "'+playlist_name+'"'
]);
};
/**
* Saves the current queue as a the given playlist, overwrites exsisting playlist of that name if it exsists, otherwise makes a new one
* @instance
* @param {String} playlist_name - the name of the playlist you want to use as your new queue
*/
self.saveQueueToPlaylist = function(playlist_name){
issueCommands('save "'+playlist_name+'"');
};
/**
* adds the given song (filename) to the given playlist
* @instance
* @param {String} playlist_name - the playlist to add the song to
* @param {String} filename - the filename of the song you want to add
*/
self.addSongToPlaylistByFile = function(playlist_name, filename){
issueCommands('playlistadd "'+playlist_name+'" "'+filename+'"');
};
/**
* Clears the playlist leaving it still in exsistance, but empty
* @instance
* @param {String} playlist_name - the poor unfortunate playlist you want to hollow out
*/
self.clearPlaylist = function(playlist_name){
issueCommands('playlistclear "'+playlist_name+'"');
};
/**
* Deletes the song at the given position from the given playlist
* @instance
* @param {String} playlist_name - the name of the playlist with a song on it that you think shouldn't be there anymore
* @param {Integer} position - the position in the playlist of the song you want to remove
*/
self.removeSongFromPlaylistByPosition = function(playlist_name, position){
issueCommands('playlistdelete "'+playlist_name+'" '+position);
};
/**
* moves the song from one position on the playlist to another
* @instance
* @param {String} playlist_name - the name of the playlist on which you want to move a song
* @param {Integer} from - position on the playlist of the song you want to move
* @param {Integer} to - the position to which you want to move the song
*/
self.moveSongOnPlaylistByPosition = function(playlist_name, from, to){
issueCommands('playlistmove "'+playlist_name+'" '+from+' '+to);
};
/**
* Renames the playlist
* @instance
* @param {String} playlist_name - the name is it right now
* @param {String} new_name - the name it should be
*/
self.renamePlaylist = function(playlist_name, new_name){
issueCommands('rename "'+playlist_name+'" "'+new_name+'"');
};
/**
* this kills the playlist
* @instance
* @param {String} playlist_name - the name of the playlist you want to obliterate and never see any trace of again
*/
self.deletePlaylist = function(playlist_name){
issueCommands('rm "'+playlist_name+'"');
};
/**
* Updates the music database: find new files, remove deleted files, update modified files.
* @instance
*/
self.updateDatabase = function(){
issueCommands('update');
};
/**
* @instance
* @param {String} [path] - path to the directory you are interested in relative to MPD's music root directory (root is a blank string, never start with '/')
* @param {directoryContentsCallback}
*/
self.getDirectoryContents = function(path, onDone){
issueCommands({
command:'lsinfo "'+path+'"',
handler:getDirectoryHandler(onDone)
});
};
/**
* return an array of strings which are all of the valid tags
* note there might be more undocumented tags that you can use just fine not listed here (like musicbrainz)
* @instance
* @returns {String[]}
*/
self.getTagTypes = function getTagTypes(){
return cloneObject(_private.tag_types);
};
/**
* params is a {tag<string> => value<string>} object, valid tags are enumerated in getTagTypes.
* onDone is a function that should be called on complete, will be passed an array of strings that are the values of the tag identified by tag_type that are on songs that match the search critaria
*
* @example
* client.tagSearch(
* 'album',
* {artist:'bearsuit'},
* function(albums){
* //albums == ["Cat Spectacular", "Team Pingpong", "OH:IO", "The Phantom Forest"]
* //which are all of the albums of the band Bearsuit
* }
* );
* @instance
* @param {Object[]} params - Array of objects that maps a tag to a value that you want to find matches on that tag for {tag<string> => value<string>}. For a list of acceptable tag/keys @see {@link getTagTypes}. For a list of acceptable values for a given tag @see {@link getTagOptions}.
* @param {searchResultsCallback} onDone - function called when the search results have come back, is passed the results as it's only parameter
*/
self.tagSearch = function doTagSearch(tag_type, params, onDone){
var query = 'list '+tag_type;
for(key in params){
var value = params[key];
query += ' '+key+' "'+value+'"';
}
issueCommands({
command:query,
handler:getTagSearchHandler(onDone, tag_type)
});
};
/**
* params is a {tag<string> => value<string>} object, valid tags are enumerated in getTagTypes, onDone is a function that should be called on complete, will be passed an array of song objects
* @instance
* @param {Object[]} params - Array of objects that maps a tag to a value that you want to find matches on that tag for {tag<string> => value<string>}. For a list of acceptable tag/keys @see {@link getTagTypes}. For a list of acceptable values for a given tag @see {@link getTagOptions}.
* @param {searchResultsCallback} onDone - function called when the search results have come back, is passed the results as it's only parameter
*/
self.search = function(params, onDone){
var query = 'search';
for(key in params){
var value = params[key];
query += ' '+key+' "'+value+'"';
}
issueCommands({
command:query,
handler:getSearchHandler(onDone)
});
};
/**
* params is a {tag<string> => value<string>} object, valid tags are enumerated in getTagTypes, onDone is a function that should be called$
* @instance
* @param {Object[]} params - Array of objects that maps a tag to a value that you want to find matches on that tag for {tag<string> => va$
* @param {searchResultsCallback} onDone - function called when the search results have come back, is passed the results as it's only para$
*/
self.find = function(params, onDone){
var query = 'find';
for(key in params){
var value = params[key];
query += ' '+key+' "'+value+'"';
}
issueCommands({
command:query,
handler:getSearchHandler(onDone)
});
};
/**
* like search except just for finding how many results you'll get (for faster live updates while criteria are edited)
* params is a {tag<string> => value<string>} object, valid tags are enumerated in getTagTypes, onDone is a function that should be called on complete, will be passed the numver of results the search would produce
* @instance
* @param {Object[]} params - Array of objects that maps a tag to a value that you want to find matches on that tag for {tag<string> => value<string>} For a list of acceptable tag/keys @see {@link getTagTypes}. For a list of acceptable values for a given tag @see {@link getTagOptions}.
* @param {searchCountCallback} onDone - function called when the search results have come back, is passed the results as it's only parameter
*/
self.searchCount = function(params, onDone){
var query = 'count';
for(key in params){
var value = params[key];
query += ' '+key+' "'+value+'"';
}
issueCommands({
command:query,
handler:getSearchHandler(function(results){
onDone(results[0]);
})
});
};
/**
* set the password for this client
* @instance
* @param {String}
*/
self.authorize = function(password){
_password = password;
if(_private.state.connected){
//if we are not connected we will issue the password as part of our reconnection
issueCommands('password '+_password);
}
}
/****************\
|* private data *|
\****************/
var _private = {
/**
* THE web socket that is connected to the MPD server
* @private
*/
socket:null,
/**
* running string of partial responces from MPD
*/
raw_buffer:'',
/**
* running list of lines we have gotten from the server
*/
raw_lines:[],
/**
* false if we are disconnected or have not completed out initial data load yet
*/
inited: false,
/**
* events that have been held until we are consistent
*/
queued_events:[],
/**
* object {string:[function]} -- listing of funcitons to call when certain events happen
*
* valid handlers:
* Connect
* Disconnect
* Queue
* State
* SongChange
* Mpdhost
* Error
* @private
*/
handlers:{},
/**
* number -- int number of milisecond to wait until reconnecting after loosing connection
* set to something falsyto disable automatic reconnection
* @private
*/
reconnect_time: 3000,
/**
* true if we want logging turned on
* @private
*/
do_logging: false,
/**
* Our understanding of what the server looks like
* @typedef {Object} state
* @property {String} version - server protocol version
* @property {Boolean} connected - if we are currently connected to the server or not
* @property {String} playstate - enum, PLAYING, STOPPED, PAUSED
* actual MPD attribute: state (int 0,1,2)
* @property {Integer} volume - 0 to 1 the current volume
* @property {Boolean} repeat - true if the server is configured to repeat the current song
* @property {Boolean} single - true if the server is configured to just play one song then quit
* @property {Boolean} consume - true if the server is configured to not repeat songs in a playlist
* @property {Boolean} random - true if the server is configured to play music in a random order
* @property {Integer} crossfade - nonnegitive integer representing number of seconds to crossfade songs
* @property {Float} mix_ramp_threshold - not sure what this is, but it's reported
* actual MPD attribute: mixrampdb
* @property {Object} current_song - info about the currently playing song
* @property {Integer} current_song.queue_idx - which song in the current playlist is active
* actual MPD attribute: song
* @property {Float} current_song.elapsed_time - time into the currently playing song in seconds
* actual MPD attribute: elapsed (MPD >= 0.16) or time (MPD <= 0.15)
* @property {Integer} current_song.duration - song duration in seconds
* actual MPD attribute: duration (MPD >= 0.20) or time (MPD <= 0.19)
* @property {Integer} current_song.id - the id of the current song
* actual MPD attribute: songid
* @property {Object} next_song - info about the song next to play on the queue
* @property {Integer} next_song.queue_idx - which song in the current playlist is active
* actual attribute: song
* @property {Integer} next_song.id - the id of the current song
* actual attribute: songid
* @property {Queue} current_queue - the songs that are currently in rotation for playing, in the order they are to play (unless random is set to true)
* @property {Integer} queue_version - a number associated with the queue that changes every time the queue changes
* @property {String[]} playlists - names of all of the saved playlists
*/
state:{
version: null,
connected:false,
playstate: null,
volume: null,
repeat: null,
single: null,
consume: null,
random: null,
crossfade: null,
mix_ramp_threshold: null,
current_song: {
queue_idx: null,
elapsed_time: null,
duration: null,
id: null
},
next_song: {
queue_idx: null,
id: null
},
current_queue: null,
queue_version: null,
playlists:[]
},
/**
* list of tags that are acceptable for this server
*/
tag_types:[],
/**
* list of available outputs
*/
outputs:[],
/**
* when was the status last updated
* @private
*/
last_status_update_time: new Date(),
/**
*method called when we get a responce from MPD
*/
responceProcessor:null,
/**
* sequence of handlers for the sequence of commands that have been issued
* @private
*/
commandHandlers:[],
/**
* commands that are yet to be processed
*/
command_queue:[],
/**
*last error we had
*/
last_error: null
};
/*******************\
|* private methods *|
\*******************/
/****************************\