-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
429 lines (357 loc) · 11.8 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
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
const express = require("express");
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const MidiPlayer = require('midi-player-js');
const path = require('path');
//const fs = require('fs');
const keyRegister = require('./key-register');
// This list of available songs
const songLibrary = require('./midis/song-list');
/**
* App Variables
*/
// Set up client browser
const port = process.env.PORT || "8000";
app.use(express.static(path.join(__dirname, 'public')));
// Set up Raspberry Pi
const numModules = 11;
const registerSize = 8;
const SER_Pin = 11; // Serial Input SER (18 on chip)
const RCK_Pin = 13; // Register Clock (RCLK, 7 on chip) - Latch pin
const SRCK_Pin = 15; // Shift-Register Clock (SRCLK, 8 on chip) - Clock pin
const SRCLR_PIN = 16; // Shift_Register clear (SRCLR - 3 on chip) - Set to high to enable storage transfer
const CLIENT_INTERVAL = 250;
// The register is all of the shift registers representing the keys
let register = new keyRegister(SER_Pin, SRCK_Pin, RCK_Pin, SRCLR_PIN);
// The keys array keeps track of the current state and is used to send to the register
let keys = Buffer.alloc(numModules * registerSize).fill(0);
// Used for running key tests
let keyIndex = 0; // Key index is used for testing one key at a time
let myInterval; // Timer interval for running key tests
// This is the current playlist or queue, just starts empty
let playlist = [];
let randomizedPlaylist = [];
let playlistChanged = false;
let playlistRepeat = false;
let playlistRandom = false;
let currentPlaylistIndex = 0;
let betweenSongs = false;
let isPlaying = false;
// Keep track of song information
let currentSongId = 0;
let currentSongTime = 0;
let currentSongTimeRemaining = 0;
let currentSongTotalTicks = 0;
let songLoaded = false;
// Initialize player and register event handler
let Player = new MidiPlayer.Player(function (event) {
let keyIndex = 21;
let keyVal = 0; // Default to "Note off" with a 0
// When we get a "Note on" or "Note off" event, update the values in the shift register
// Also, update the clients in case they are playing the audio locally
if (event.name === "Note on" || event.name === "Note off") {
// "Note off" is also represented as a velocity of 0
// So, we default to 0, but change the value if velocity is not 0
if (event.velocity !== 0) {
keyVal = 1;
}
keyIndex = event.noteNumber - 21; // Piano key midi notes start at 21
keys[keyIndex] = keyVal;
register.send(keys);
io.emit('update keys', JSON.stringify(keys));
// Calculate time remaining using the current tick
let remainingTicks = currentSongTotalTicks - event.tick;
currentSongTimeRemaining = Math.floor(remainingTicks / currentSongTotalTicks * currentSongTime);
}
});
Player.on('endOfFile', function() {
console.log("End of File reached");
betweenSongs = true;
io.emit('song over');
});
let songDelay = 0;
// Set an interval to update all connected clients
function updateClients() {
if (betweenSongs) {
if (songDelay >= 2000) {
songDelay = 0;
betweenSongs = false;
if (isPlaying) {
playNextSong();
}
return;
}
songDelay += CLIENT_INTERVAL;
}
// Only update client playlist if it's changed
if (playlistChanged) {
io.emit('update playlist', JSON.stringify(playlist));
playlistChanged = false;
}
// Let the clients know what song to play
io.emit("set song", currentSongId);
io.emit("update time", currentSongTime, currentSongTimeRemaining);
io.emit("settings", playlistRepeat, playlistRandom);
}
function runEachKeyTest() {
keyIndex = 0;
keys.fill(0);
if (myInterval) {
clearInterval(myInterval);
}
myInterval = setInterval(testEachKey, 500);
}
function testEachKey() {
keys[keyIndex] = 1;
if (keyIndex > 0) {
keys[keyIndex-1] = 0;
}
if (keyIndex >= numModules * registerSize) {
clearInterval(myInterval);
}
register.send(keys);
io.emit('update keys', JSON.stringify(keys));
//console.log("Keys: " + JSON.stringify(keys));
keyIndex++;
}
function runAllKeysTest() {
keyIndex = 0;
keys.fill(0);
if (myInterval) {
clearInterval(myInterval);
}
myInterval = setInterval(testAllKeys, 500);
}
function testAllKeys() {
keys[keyIndex] = 1;
if (keyIndex >= numModules * registerSize) {
register.send(keys);
io.emit('update keys', JSON.stringify(keys));
clearInterval(myInterval);
}
register.send(keys);
io.emit('update keys', JSON.stringify(keys));
keyIndex++;
}
function stopTests() {
if (myInterval) {
clearInterval(myInterval);
}
Player.stop();
keys.fill(0);
register.send(keys);
io.emit('update keys', JSON.stringify(keys));
}
function playMusic() {
if(!isPlaying) {
// Check if a song has been loaded, if pick a random song to load
if (!songLoaded) {
let i = Math.floor(Math.random() * Math.floor(songLibrary.songs.length));
currentSongId = songLibrary.songs[i].id;
setSong(currentSongId);
}
Player.play();
}
isPlaying = true;
}
function pauseMusic() {
if (myInterval) {
clearInterval(myInterval);
}
Player.pause();
isPlaying = false;
}
function setCurrentSongTime(percent) {
if (Player.isPlaying()) {
//console.log("skipping to " + percent);
Player.skipToPercent(percent);
Player.play();
} else {
Player.skipToPercent(percent);
}
}
function resetKeys() {
keys.fill(0);
register.send(keys);
}
// If random (shuffle) is enabled, then need to create a random ordered playlist to play from
function createRandomizedPlaylist() {
// First, copy the playlist
let temp = [...playlist];
randomizedPlaylist = [];
// Next copy one at a time at random
for (let i = 0; i < playlist.length; i++) {
let j = Math.floor(Math.random() * temp.length);
console.log("Pushing position: " + j);
randomizedPlaylist.push(temp.splice(j,1));
}
if (playlistRandom) {
// If playing a random playlist, then reset the index to start over.
currentPlaylistIndex = 0;
}
console.log("Playlist.length = " + playlist.length + " Randomized list length: " + randomizedPlaylist.length);
console.log("Playlist: " + JSON.stringify(playlist));
console.log("Randomized: " + JSON.stringify(randomizedPlaylist));
}
function updateSettings(playRandom, playRepeat) {
// Check if the random setting has changed and if so, create a randomized playlist
if (playRandom != playlistRandom) {
playlistRandom = playRandom;
// Recreate the random playlist
if (playlistRandom) {
createRandomizedPlaylist();
}
}
playlistRepeat = playRepeat;
}
function playNextSong() {
console.log("Playing next song");
let list;
if (playlistRandom) {
console.log("Playing from random list");
list = randomizedPlaylist;
} else {
console.log("Playing from normal list");
list = playlist;
}
if (list.length === 0) return;
currentPlaylistIndex++;
if (currentPlaylistIndex >= list.length - 1) {
currentPlaylistIndex = 0;
if (!playlistRepeat) return; // Done with the list
}
setSong(list[currentPlaylistIndex].id);
playMusic();
}
function setSong(song_id) {
// Stop any tests
if (myInterval) {
clearInterval(myInterval);
}
currentSongId = song_id;
// Reset all keys
resetKeys();
console.log("Received song: " + song_id);
let song = songLibrary.songs.find(el => el.id === Number.parseInt(song_id));
console.log("Playing Song: " + song["title"]);
let songPath = song["path"];
// Load a MIDI file
if (Player.isPlaying()) {
Player.stop();
}
Player.loadFile(songPath);
songLoaded = true;
let lastTick = 0;
// Try to estimate the song length in ticks by finding the largest tick value
Player.tracks.forEach(function(track){
track.events.forEach(function(event){
if(event.name == "Note on" || event.name == "Note off") {
if (event.tick > lastTick) {
lastTick = event.tick;
}
}
});
});
currentSongTime = Player.getSongTime();
currentSongTimeRemaining = currentSongTime;
currentSongTotalTicks = lastTick;
console.log("Song length: " + Player.getSongTime() + ", Song Total Ticks: " + currentSongTotalTicks);
}
function playSong(song_id) {
// Stop any tests
if (myInterval) {
clearInterval(myInterval);
}
currentSongId = song_id;
// Reset all keys
resetKeys();
console.log("Received song: " + song_id);
let song = songLibrary.songs.find(el => el.id === Number.parseInt(song_id));
console.log("Playing Song: " + song["title"]);
let songPath = song["path"];
// Load a MIDI file
if (Player.isPlaying()) {
Player.stop();
}
Player.loadFile(songPath);
console.log("Song length: " + Player.getSongTime());
//io.emit("set song time", Player.getSongTime());
/*Player.tracks.forEach(function(track){
track.events.forEach(function(event){
//console.log("Instrument: " + JSON.stringify(event));
if(event.name !== "Note on") {
//let trackNameEvent = event.values[0].values[0];
//store track instrument in config
//console.log("Event: " + JSON.stringify(event));
//console.log("Instrument: " + trackNameEvent.string.trim());
}
});
//console.log("Track: " + track.name);
});
*/
Player.play();
}
io.on('connection', function (socket) {
console.log('a user connected');
//io.emit('update switches', JSON.stringify(keys));
//console.log("Sending: " + songs);
//io.emit('song list', songs);
io.emit('song list', JSON.stringify(songLibrary));
io.emit('update playlist', JSON.stringify(playlist));
socket.on("test each key", function() {
runEachKeyTest();
});
socket.on("test all keys", function() {
runAllKeysTest();
});
socket.on("stop tests", function() {
stopTests();
});
socket.on("set song", function(id) {
setSong(id);
});
socket.on("update settings", function(playRandom, playRepeat) {
updateSettings(playRandom, playRepeat);
});
//socket.on("play", function() {
// playSong();
//});
socket.on("play", function() {
playMusic();
});
socket.on("pause", function(id) {
pauseMusic(id);
});
socket.on("set time", function(percent) {
setCurrentSongTime(percent);
});
socket.on('update key', function (id, keyState) {
if (id >= 0 && id <= keys.length) {
keys[id] = keyState;
io.emit('update keys', JSON.stringify(keys));
console.log('Updated keys[' + id + ']' + ": " + keyState);
register.send(keys);
}
});
socket.on('update playlist', function (p) {
console.log('incoming playlist: ' + p);
playlist = JSON.parse(p);
playlistChanged = true; // Update all connected clients' playlist
createRandomizedPlaylist();
//io.emit('update playlist', JSON.stringify(p));
//io.emit('update switches', JSON.stringify(keys));
});
socket.on('disconnect', function () {
console.log('user disconnected');
});
});
/**
* Server Activation
*/
http.listen(port, function () {
console.log(`listening to requests on http://localhost:${port}`);
});
//setSong(currentSongId);
// Start updating clients
setInterval(updateClients,CLIENT_INTERVAL); // Update all clients every 250ms