-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.js
141 lines (123 loc) · 2.78 KB
/
play.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
var events = require('events');
var emitter = new events.EventEmitter();
// Get the Client and connect
var mpd = require('mpd'),
cmd = mpd.cmd;
var client = mpd.connect({
port: 6600,
host: 'localhost',
});
client.on('ready', function() {
console.log("We are ready to receive music");
controls.changevol();
emitter.emit("ready");
emitter.on("status", function(msg) {
//console.log(msg);
same = 0;
});
});
client.on('system', function(name) {
console.log("update", name);
});
client.on('system-player', function() {
client.sendCommand(cmd("status", []), function(err, msg) {
if(err){
emitter.emit("error",err);
}else{
getSong(function(song){
msg += "\ntitle: "+song;
emitter.emit("status",msg);
});
}
});
});
client.on('system-mixer', function(){
getVol(function(volume){
emitter.emit("volume",volume);
});
});
function getSongId(cb){
client.sendCommand(cmd("currentsong",[]),function(err,msg){
id = msg.match(/\nId: (.*?)\n/i)[1];
console.log("Song id is "+id+".");
cb(id);
});
}
function getVol(cb){
client.sendCommand(cmd("currentsong",[]),function(err,msg){
var volume = msg.match(/\nvolume: (.*?)\n/i)[1];
try{
vol = parseInt(volume,10);
cb(vol);
}catch(e){throw e;}
});
}
function getSong(cb){
client.sendCommand(cmd("currentsong",[]),function(err,msg){
if(err) throw err;
song = msg.match(/\nTitle: (.*?)\n/i)[1];
cb(song);
});
}
// Is the same song playing?
var same = 0;
var currId = -1;
var vol = 100;
var controls = new Object();
controls.volumeUpPressed = function(){
if (vol < 100) {
vol = vol + 5;
this.changevol();
}
};
controls.volumeDownPressed = function(){
if (vol > 0) {
vol = vol - 5;
this.changevol();
}
};
controls.changevol = function(){
console.log("Setting volume to: "+vol);
client.sendCommand(cmd("setvol", [vol]));
emitter.emit("status","volume: "+vol);
};
controls.play = function(){
client.sendCommand("play");
};
controls.pause = function(){
client.sendCommand(cmd("pause", [1]));
};
controls.stop = function(){
client.sendCommand("stop");
};
controls.toggle = function(){
client.sendCommand("pause");
};
controls.happyPressed = function(){
if(same) return;
var song = "N";
getSong(function(song){
console.log("We are happy while "+song+" is playing.");
emitter.emit("happy",song);
same = 1;
});
};
controls.skip = function(){
client.sendCommand("next");
console.log("Skipping");
same = 0;
};
controls.skipPressed = function(){
this.skip();
};
controls.getStatus = function(cb){
client.sendCommand(cmd("status",[]),function(err,msg){
if(err) throw err;
getSong(function(){
msg += "\ntitle: "+song;
cb(msg);
});
});
};
controls.events = emitter;
module.exports = controls;