-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
177 lines (129 loc) · 4.6 KB
/
index.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
//Config
var SHOW_FILE = process.argv[2];
//All actions are advanced by anticipated control lag
var CONTROL_LAG = 0.05;
//Dependencies
var ip = require("ip");
var fs = require('fs');
var airplayer = require('airplayer')
var hue = require("node-hue-api");
var ipAddress = ip.address();
console.log(`<HueLightShow> on ${ipAddress}`);
//Hue API
var HueApi = hue.HueApi;
var HueLightState = hue.lightState;
//Load and parse our Script
var script = fs.readFileSync(SHOW_FILE, 'utf8') + '\n@end';
if(!script) {
console.log("Couldn't load " + SHOW_FILE);
}
var actionsRegex = /([\s\S]*?)\B@(\S+)/g;
var actions = {};
var nextTime = undefined;
var markersCsv = "Name\tStart\tDuration\tTime Format\tType\tDescription\n";
var START_TIME = 0;
var SAVE_MARKERS = false;
while ((action = actionsRegex.exec(script)) !== null) {
var actionJs = action[1].trim();
if (!nextTime) {
console.log("Running Light Show Script Setup");
eval(actionJs);
} else {
if(nextTime >= START_TIME) {
actions[nextTime] = actionJs;
}
}
var actionTime = action[2].split('b');
nextTime = parseFloat(actionTime[0]);
if (actionTime[1]) {
nextTime += parseFloat(actionTime[1]) * BEAT_DURATION;
}
csvTime = nextTime.toFixed(3);
markersCsv += `${action[2]}\t${csvTime}\t0\tdecimal\tCue\t\n`;
}
//Save Adobe Audition compatible markers.csv file
if(SAVE_MARKERS) {
fs.writeFileSync(SAVE_MARKERS, markersCsv);
}
//console.log(actions);
//Function which runs our light script
var startTime;
var lagCompensation = 0;
function runScript() {
var lastAction = 0;
var actionTimes = Object.keys(actions).sort((a, b) => {
return parseFloat(a) - parseFloat(b);
});
console.log(actionTimes);
var interval = setInterval(() => {
var tickTime = process.hrtime(startTime);
tickTime = tickTime[0] + tickTime[1] / 1000000000;
tickTime += START_TIME || 0;
tickTime += lagCompensation;
var actionTime = actionTimes[0];
if (!actionTime) {
console.log("Done all actions :)");
clearInterval(interval);
}
if (tickTime >= actionTime) {
var actionJs = actions[actionTime];
actionJs = actionJs.replace(/^\/\/#.*/gm, (val) => {
val = val.replace('\'', '\\\'');
return `console.log('${val}');`;
});
console.log(actionJs);
eval(actionJs);
actionTimes.shift();
}
//console.log(actionTime);
//console.log(tickTime);
}, 50);
}
//HTTP Server for AirPlay
var express = require('express');
var app = express();
app.use(express.static('./assets/'));
app.listen(8087);
console.log('Listening on port 8087');
//AirPlay the track
var list = airplayer()
list.on('update', function(player) {
console.log('Found new AirPlay device:', player.name)
if (player.name == AIRPLAY_TARGET) {
var audioFileURI = encodeURIComponent(AUDIO_FILE);
var file = `http://${ipAddress}:8087/${audioFileURI}`;
var statusInterval;
player.play(file, 0);
console.log('... Playing', file);
player.on('event', (data) => {
console.log('Event: ' + JSON.stringify(data));
if (data.state == "playing" && !startTime) {
startTime = process.hrtime();
START_TIME && player.scrub(START_TIME);
runScript();
statusInterval = setInterval(() => {
//Measure our lag at various intervals so we keep sync
player.playbackInfo(function(err, res, body) {
if (err) throw err
//console.log('Playback info:', body)
var airplayTime = body.position;
var localTime = process.hrtime(startTime);
localTime = localTime[0] + localTime[1] / 1000000000;
localTime += START_TIME || 0;
var lag = airplayTime - localTime - lagCompensation - CONTROL_LAG;
lagCompensation += Math.max(Math.min(lag, 0.02), -0.2);
if(lag > 0.01 || lag < -0.01) {
console.log(`*** lag: ${lag} lagCompensation: ${lagCompensation}`);
}
})
}, 1000);
}
if (data.state == "stopped") {
player.destroy();
list.destroy();
clearInterval(statusInterval);
statusInterval = undefined;
}
});
}
})