-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
180 lines (149 loc) · 5.45 KB
/
main.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
import "./style.css";
import { sketch } from "./modules/canvas";
import { SoundGenerator } from "./modules/sound";
import { MELODYGENERATOR, MusicGenerator, NOTEPOOLS } from "./modules/musicgenerator";
console.log("%cPackage: __CLI_NAME__, version: __CLI_VERSION__, date: __CLI_TS__", "background-color: #36a5b7; color: #FFF;");
// Init app HTML
document.querySelector("#app").innerHTML = `
<div>
<div id="header">
<div class="header_segment">
<div class="header_logo">Sounds of decay</div>
<div>__CLI_VERSION__</div>
</div>
<div class="header_segment">
<div class="header_label">Note pool:</div>
<div class="header_control"><select id="sel_notepool"></select></div>
</div>
<div class="header_segment">
<div class="header_label">Melody generator:</div>
<div class="header_control"><select id="sel_melodygenerator"></select></div>
</div>
<div class="header_segment">
<div class="header_label">Intrument:</div>
<div class="header_control">
<select id="sel_instrument">
<option value="Grand Piano">Grand Piano</option>
<option value="Chorus female">Chorus female</option>
<option value="Chorus male">Chorus male</option>
<option value="Violin Tremulo">Violin Tremulo</option>
<option value="Cello Tremulo">Cello Tremulo</option>
<option value="Bass solo">Bass solo</option>
<option value="Xylophone">Xylophone</option>
</select>
</div>
</div>
<div class="header_segment">
<div class="header_label">Effects/convolver:</div>
<div class="header_control">
<select id="sel_convolver">
<option value="">- None -</option>
<option value="RoomMedium">Medium room</option>
<option value="AirportTerminal">Airport Terminal</option>
</select>
</div>
</div>
<div class="header_segment">
<div class="header_label">Gain:</div>
<div class="header_control"><input type="range" id="rng_gain" min="0" max="2" value="0.7" step="0.01"></div>
</div>
<div class="header_segment">
<div class="header_label">Fade time:</div>
<div class="header_control"><input type="range" id="rng_fade" min="0" max="2" value="0.7" step="0.1"></div>
</div>
</div>
<div id="p5-container"></div>
<div id="footer">
<div>More informations: <a href="https://github.com/toblum/sounds-of-decay" target="_blank">https://github.com/toblum/sounds-of-decay</a></div>
</div>
</div>`;
// Init P5 canvas
const containerElement = document.getElementById("p5-container");
// eslint-disable-next-line no-undef
const canvas = new p5(sketch, containerElement);
// Init sound and music generator
const soundGenerator = new SoundGenerator();
const musicGenerator = new MusicGenerator(soundGenerator);
let socket = new WebSocket("wss://decayrelay.tobiasblum.de");
socket.onopen = function () {
console.log("[open] Connection established");
};
socket.onclose = function (event) {
if (event.wasClean) {
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
// e.g. server process killed or network down
// event.code is usually 1006 in this case
console.log("[close] Connection died");
}
};
socket.onerror = function (error) {
console.log(`[error] ${error.message}`);
};
(async () => {
let isPlaying = false;
const playNote = () => {
musicGenerator.playNextNote();
};
const startPlaying = () => {
isPlaying = true;
};
socket.onmessage = function (event) {
// console.log(`[message] Data received from server: ${event.data}`);
if (isPlaying && event.data === "Decay") {
playNote();
}
canvas.addDecayParticle();
};
const pausePlaying = () => {
isPlaying = false;
canvas.pause();
};
for (const key in NOTEPOOLS) {
const notepool = NOTEPOOLS[key];
var option_notepool = document.createElement("option");
option_notepool.text = notepool.title;
option_notepool.value = key;
document.getElementById("sel_notepool").add(option_notepool);
}
document.getElementById("sel_notepool").addEventListener("change", e => {
console.log("Set notepool to:", e.target.value);
musicGenerator.setNotepool(e.target.value);
});
for (const key in MELODYGENERATOR) {
const melodygenerator = MELODYGENERATOR[key];
var option_melodygenerator = document.createElement("option");
option_melodygenerator.text = melodygenerator.title;
option_melodygenerator.value = key;
document.getElementById("sel_melodygenerator").add(option_melodygenerator);
}
document.getElementById("sel_melodygenerator").addEventListener("change", e => {
console.log("Set melody generator to:", e.target.value);
musicGenerator.setMelodygenerator(e.target.value);
});
document.getElementById("sel_instrument").addEventListener("change", e => {
console.log("Set intrument to:", e.target.value);
soundGenerator.setInstrument(e.target.value);
});
document.getElementById("sel_convolver").addEventListener("change", e => {
console.log("Set convolver to:", e.target.value);
soundGenerator.setConvolver(e.target.value);
});
document.getElementById("rng_gain").addEventListener("input", e => {
console.log("Set gain to:", e.target.value);
soundGenerator.setGain(e.target.value);
});
document.getElementById("rng_fade").addEventListener("input", e => {
console.log("Set fade to:", e.target.value);
soundGenerator.setFade(e.target.value);
});
containerElement.addEventListener("clickPlay", () => {
startPlaying();
});
containerElement.addEventListener("clickPause", () => {
pausePlaying();
});
containerElement.addEventListener("clickCanvas", () => {
playNote();
});
})();