forked from webmedia-lab/accelerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynth.html
More file actions
143 lines (110 loc) · 4.17 KB
/
synth.html
File metadata and controls
143 lines (110 loc) · 4.17 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Accelerator</title>
</head>
<body>
<audio id='celu_1_0' src="./sounds/celu_1_0.mp3" autoplay loop="true"></audio>
<audio id='celu_1_1' src="./sounds/celu_1_1.wav" autoplay loop="true"></audio>
<!-- separator -->
<audio id='celu_2_0' src="./sounds/celu_2_0.mp3" autoplay loop="true"></audio>
<audio id='celu_2_1' src="./sounds/celu_2_1.wav" autoplay loop="true"></audio>
<!-- separator -->
<audio id='celu_3_0' src="./sounds/celu_3_0.wav" autoplay loop="true"></audio>
<audio id='celu_3_1' src="./sounds/celu_3_1.mp3" autoplay loop="true"></audio>
<!-- separator -->
<script>
var context;
var tracks = {};
var ws = new WebSocket("ws://localhost:9090");
ws.onmessage = function (e) {
var data = JSON.parse(e.data);
// console.log(data.id, tracks[data.id]);
tracks[data.id].update(data.x, data.y, data.z);
}
function Track(a1, a2, f) {
var audio1 = context.createMediaElementSource(a1);
var audio2 = context.createMediaElementSource(a2);
var gain2 = context.createGain();
var gain1 = context.createGain();
var master = context.createGain();
var filter = context.createBiquadFilter();
//var last = {x: 0, y: 0, z: 0, delta: 0, time: 0};
function init() {
a1.loop = true;
a2.loop = true;
gain1.gain.value = 0;
gain2.gain.value = 0;
filter.type = f.type;
filter.frequency.value = f.freq;
filter.Q.value = f.q;
master.gain.value = f.master || 1;
audio1.connect(gain1);
gain1.connect(filter);
audio2.connect(gain2);
gain2.connect(filter);
filter.connect(master);
master.connect(context.destination);
}
function updateGain(z) {
var gain = z / 20;
if (gain > 0.5) { gain = 0.5 };
if (gain < -0.5) { gain = -0.5 };
gain1.gain.value = 0.5 + gain;
gain2.gain.value = 0.5 - gain;
}
function updateFilter(y) {
var d = y;
if (y > 10) { d = 10 };
if (y < -10) { d= -10 };
var center = f.center;
var range = f.variation;
d = center + d * (2 * range / 20);
filter.detune.value = d;
console.log(d);
}
function updateMaster(x,y,z) {
var dx = Math.abs(last.x - x);
var dy = Math.abs(last.y - y);
var dz = Math.abs(last.z - z);
var d = dx + dy + dz;
if (last.delta < 0.2) {
var gain = master.gain.value - 1 / 60;
if (gain < 0) { gain = 0 }
} else {
var gain = master.gain.value + 1 / 60;
if (gain > 1) { gain = 1 }
}
master.gain.value = gain;
last.time = context.currentTime;
last.delta = 0;
last.x = x;
last.y = y;
last.z = z;
last.delta = last.delta + d / 60;
}
function update(x,y,z) {
updateGain(z);
updateFilter(y);
//updateMaster(x,y,z);
}
init();
return { update: update };
}
document.addEventListener('DOMContentLoaded', function() {
try {
// Fix up for prefixing
window.AudioContext = window.AudioContext||window.webkitAudioContext;
context = new AudioContext();
}
catch(e) {
alert('Web Audio API is not supported in this browser');
}
tracks['1'] = Track(document.querySelector('#celu_1_0'), document.querySelector('#celu_1_1'), { center: 1000, variation: 1100, type: 'lowpass', q: 15, freq: 1000, master: 0.15 });
tracks['2'] = Track(document.querySelector('#celu_2_0'), document.querySelector('#celu_2_1'), { center: 800, variation: 1000, type: 'lowpass', q: 5, freq: 800, master: 0.2 });
tracks['3'] = Track(document.querySelector('#celu_3_0'), document.querySelector('#celu_3_1'), { center: 100, variation: 1000, type: 'bandpass', q: 8, freq: 300 });
});
</script>
</body>
</html>