-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
212 lines (132 loc) · 3.74 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
/*
MSync
SWR EXPERIMENTALSTUDIO
Maurice Oeser
2023
Davor Vincze - FLUCHT
Smartphone Soundfile Controler
ermöglicht es Soundfiles synchron auf den Handys des Publikums abzuspielen und zu steuern
*/
// **************** Server setup ****************************
const fs = require('fs');
const path = require('path');
const express = require('express');
const app = express();
const http = require('http');
const httpServer = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(httpServer);
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
httpServer.listen(3001, () => {
console.log('*** HTTP-SERVER ON PORT 3001 STARTED AND LISTENING ***');
});
// ==========================================================
/*
Voice-Counter, der entscheidet welche Stimme das nächste sich verbindene Handy bekommt
*/
let voiceCounter = 0;
const numMaxVoices = 8;
function getNextVoice()
{
voiceCounter = (voiceCounter + 1) % numMaxVoices;
return voiceCounter;
}
// ******************* State control ********************************
let clients = []; // list of all connected clients
let lastStartTime= 0; // if we are currently playing, time of last started soundfile
let currentScene = 0;
let isPlaying = false;
// ********************** Socket.IO *************************
io.on('connection', (socket) => {
socket.voice = getNextVoice();
socket.emit('connected', {voice: socket.voice});
clients.push(socket);
socket.on('disconnect', () => {
clients.splice(clients.indexOf(socket), 1);
});
socket.on('activate', () => {
let timeToJump = isPlaying ? (Date.now() - lastStartTime) / 1000 : 0;
socket.emit("activation", { playing: isPlaying, scene: currentScene, time: timeToJump});
})
socket.on('ping', () => {
socket.emit('pong');
})
});
// ======================= OSC ADMIN CONTROL (from Max) =======================
const OSCserver = require('node-osc').Server;
const OSCClient = require('node-osc').Client;
const oscToMax = new OSCClient('127.0.0.1', 5555); // TODO evtl. dynamisch wenn nicht auf gleichem Rechner wie Max
let oscServer = new OSCserver(3333, '0.0.0.0', () => {
console.log('*** OSC CONNECTION STARTED AND LISTENING ON PORT 3333 ***');
});
// message handling
oscServer.on('message', function (msg) {
let AP = msg[0]; // AdressPattern
switch(AP)
{
case '/start':
currentScene = msg[1];
loadScene(currentScene);
oscToMax.send('/server', `started scene ${currentScene}`);
break;
case '/stop':
stopPlayback();
oscToMax.send('/server', "stopped");
break;
case '/colorall':
let R = msg[1];
let G = msg[2];
let B = msg[3];
setClientColors(R,G,B);
break;
case '/users':
oscToMax.send("/users", clients.length);
break;
case '/mode':
let mode = msg[1];
setMode(mode);
break;
case '/organ':
let voice = msg[1];
let pitch = msg[2];
let velocity = msg[3];
playOrgan(voice, pitch, velocity);
break;
}
});
// ================================= Client Control =========================
function loadScene(scene)
{
if(scene === 0)
{
isPlaying = false;
}
else
{
lastStartTime = Date.now();
isPlaying = true;
}
io.emit('start', scene);
}
function stopPlayback()
{
isPlaying = false;
io.emit('stop');
}
function setMode(mode)
{
console.log("mode", mode);
io.emit('mode', mode);
}
function setClientColors(R,G,B)
{
io.emit('color', R,G,B);
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}