-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.html
347 lines (313 loc) · 16.9 KB
/
index.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Sympathetic Synthesizer System Mk1</title>
<meta name="description" content="An analogue synth made with the Web Audio API"/>
<meta name="author" content="Stuart Memo"/>
<link rel="stylesheet" media="screen, projection" href="styles/synth.css"/>
<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<link rel="shortcut icon" type="image/ico" href="images/favicon.ico" />
</head>
<body>
<div class="synth">
<h1>Sympathetic Synthesizer System Mk 1</h1>
<div class="controls">
<div class="module oscillators">
<!-- Oscillator 1 -->
<div class="osc1">
<h2>Oscillator 1</h2>
<div class="left-half">
<label for="osc1-waveform">Waveform</label>
<select data-oscillator="osc1" data-param="waveform" name="osc1-waveform" class="waveform">
<option value="triangle">Triangle</option>
<option value="sawtooth">Sawtooth</option>
<option value="square" selected>Square</option>
</select>
</div>
<div class="right-half">
<label for="osc1-range">Range</label>
<select data-oscillator="osc1" data-param="range" name="osc1-range" class="range">
<option value="32" selected>32</option>
<option value="16">16</option>
<option value="8">8</option>
<option value="4">4</option>
<option value="2">2</option>
</select>
</div>
<label for="osc1-detune">Tuning</label>
<input data-oscillator="osc1" data-param="detune" type="range" min="-50" max="50" step="1" value="-6"/>
<label for="osc1-volume">Volume</label>
<input name="osc1-volume" data-oscillator="osc1" data-param="mixer" type="range" min="0" max="1" step="0.05" value="0.5"/>
</div>
<!-- Oscillator 2 -->
<div class="osc2">
<h2>Oscillator 2</h2>
<div class="left-half">
<label for="osc2-waveform">Waveform</label>
<select data-oscillator="osc2" data-param="waveform" name="osc2-waveform">
<option value="triangle" >Triangle</option>
<option value="sawtooth" selected>Sawtooth</option>
<option value="square">Square</option>
</select>
</div>
<div class="right-half">
<label for="osc2-range">Range</label>
<select data-oscillator="osc2" data-param="range" name="osc2-range" class="range">
<option value="32">32</option>
<option value="16">16</option>
<option value="8">8</option>
<option value="4" selected>4</option>
<option value="2">2</option>
</select>
</div>
<label for="osc2-detune">Tuning</label>
<input data-oscillator="osc2" data-param="detune" type="range" min="-50" max="50" step="1" value="-10"/>
<label for="osc2-volume">Volume</label>
<input name="osc2-volume" data-oscillator="osc2" data-param="mixer" type="range" min="0" max="1" step="0.1" value="0.5"/>
</div>
<!-- Oscillator 3 -->
<div class="osc3 last">
<h2>Oscillator 3</h2>
<div class="left-half">
<label for="osc3-waveform">Waveform</label>
<select data-oscillator="osc3" data-param="waveform" name="osc3-waveform">
<option value="triangle">Triangle</option>
<option value="sawtooth">Sawtooth</option>
<option value="square" selected>Square</option>
</select>
</div>
<div class="right-half">
<label for="osc3-range">Range</label>
<select data-oscillator="osc3" data-param="range" name="osc3-range">
<option value="32">32</option>
<option value="16" selected>16</option>
<option value="8">8</option>
<option value="4" >4</option>
<option value="2">2</option>
</select>
</div>
<label for="osc3-detune">Tuning</label>
<input name="osc3-detune" data-oscillator="osc3" data-param="detune" type="range" min="-50" max="50" step="1" value="10"/>
<label for="osc3-volume">Volume</label>
<input name="osc3-volume" data-oscillator="osc3" data-param="mixer" type="range" min="0" max="1" step="0.1" value="0.5"/>
</div>
</div>
<div class="module lfo">
<h2>LFO</h2>
<label for="lfo-waveform">Waveform</label>
<select data-param="lfo" name="lfo-waveType">
<option value="triangle" selected>Triangle</option>
<option value="sawtooth">Sawtooth</option>
<option value="square">Square</option>
</select>
<label for="lfo-frequency">Frequency</label>
<input data-param="lfo" name="lfo-frequency" type="range" min="0" max="30" step="1" value="0"/>
<label for="lfo-depth">Depth</label>
<input data-param="lfo" name="lfo-depth" type="range" min="1" max="10" step="1" value="1"/>
</div>
<!-- Filter -->
<div class="module filter">
<h2>Filter</h2>
<div class="left-half">
<label for="filter-attack">Attack</label>
<input name="filter-attack" data-param="filter" type="range" min="0" max="500" value="10"/>
<label for="filter-decay">Decay</label>
<input name="filter-decay" type="range" data-param="filter" min="0" max="25" value="5"/>
<label for="filter-sustain">Sustain</label>
<input name="filter-sustain" type="range" data-param="filter" min="0" max="10" value="5"/>
<label for="filter-release">Release</label>
<input name="filter-release" type="range" data-param="filter" min="0" max="5" value="2"/>
</div>
<div class="right-half">
<label for="cutoffFrequency">Cutoff frequency</label>
<input name="cutoffFrequency" data-param="filter" type="range" min="20" max="20000" step="200" value="10000"/>
<label for="emphasis">Emphasis</label>
<input name="emphasis" data-param="filter" type="range" min="0" max="10" value="5"/>
<label for="contour">Contour</label>
<input name="contour" data-param="filter" type="range" min="0" max="10" value="5"/>
</div>
</div>
<div class="module volume-filter">
<h2>Volume Filter</h2>
<label for="attack">Attack</label>
<input name="attack" data-param="volume-filter" type="range" min="0" max="5" step="0.1" value="0"/>
<label for="decay">Decay</label>
<input name="decay" data-param="volume-filter" type="range" min="0.1" max="5" step="0.1" value="1"/>
<label for="sustain">Sustain</label>
<input name="sustain" data-param="volume-filter" type="range" min="0.1" max="1" step="0.01" value="0.5"/>
<label for="release">Release</label>
<input name="release" data-param="volume-filter" type="range" min="0" max="10" step="0.1" value="0"/>
</div>
<div class="module noise">
<h2>Noise</h2>
<label for="level">Level</label>
<input name="level" data-param="noise" type="range" min="0" max="1" step="0.1" value="0"/>
</div>
<div class="module output">
<h2>Output</h2>
<label for="volume">Volume</label>
<input name="volume" data-param="volume" type="range" min="0" max="1" step="0.01"/>
</div>
<div class="module config-bar">
MIDI
</div>
</div>
<div id="keyboard"></div>
</div>
<script src="bower_components/tsw/dist/tsw.js"></script>
<script src="scripts/synth.js"></script>
<script src="bower_components/qwerty-hancock/dist/qwerty-hancock.min.js"></script>
<script>
// Check if browser is capable
if ((typeof webkitAudioContext === 'undefined') && (typeof AudioContext === 'undefined')) {
document.querySelectorAll('.synth')[0].insertAdjacentHTML('afterbegin',
'<h1>Sorry, your browser doesn\'t support the Web Audio API. ' +
'Try <a href="http://google.com/chrome">Chrome</a> instead!</h1>');
}
var synth = new Synth({
context: tsw.context(),
speakersOn: true
}),
keyboard = new QwertyHancock({
id: 'keyboard',
width: 908,
height: 150,
startKey: 'A3',
hoverColour: '#FFE976'
});
var inputs = document.getElementsByTagName('input'),
selects = document.getElementsByTagName('select');
// Turn NodeLists into Arrays
inputs = Array.prototype.slice.call(inputs);
selects = Array.prototype.slice.call(selects);
inputs = inputs.concat(selects);
var updateSettings = function () {
var osc = this.getAttribute('data-oscillator'),
param = this.getAttribute('data-param');
switch (param) {
case 'range':
synth.oscillators[osc][param] = this.value;
break;
case 'waveform':
synth.oscillators[osc][param] = this.value;
break;
case 'detune':
synth.oscillators[osc][param] = this.value;
synth.activeOscillators.forEach(function (oscillator) {
oscillator.detune.value = synth.oscillators[osc][param];
});
break;
case 'filter':
switch (this.name) {
case 'filter-attack':
synth.filterEnvelopeSettings.attack = parseInt(this.value, 10);
break;
case 'filter-decay':
synth.filterEnvelopeSettings.decay = parseInt(this.value, 10);
break;
case 'filter-sustain':
synth.filterEnvelopeSettings.sustain = parseInt(this.value, 10);
break;
case 'filter-release':
synth.filterEnvelopeSettings.release = parseInt(this.value, 10);
break;
case 'cutoffFrequency':
synth.filterEnvelopeSettings.startLevel = parseInt(this.value, 10);
break;
case 'emphasis':
synth.filterEnvelopeSettings.Q = parseInt(this.value, 10);
break;
case 'contour':
//synth.filter.node.Q.value = parseInt(this.filter.emphasis, 10);
break;
default:
break;
};
break;
case 'volume-filter':
synth.volumeEnvelopeSettings[this.name] = parseFloat(this.value, 10);
break;
case 'mixer':
synth.mixer[osc].node.gain(this.value);
break;
case 'lfo':
break;
switch (this.name) {
case 'lfo-waveType':
synth.lfoSettings.waveType = this.value;
break;
case 'lfo-frequency':
synth.lfoSettings.frequency = this.value;
break;
case 'lfo-depth':
synth.lfoSettings.node.depth = this.value;
break;
default:
break;
};
case 'volume':
synth.masterVolume.gain(this.value);
break;
case 'noise':
synth.noiseLevel.gain(this.value);
break;
default:
break;
};
};
for (var i = 0; i < inputs.length; i++) {
inputs[i].addEventListener('change', function () {
updateSettings.call(this);
});
updateSettings.call(inputs[i]);
}
keyboard.keyDown = function (note, frequency) {
synth.playNote(note);
};
keyboard.keyUp = function (note, frequency) {
synth.stopNote(note);
};
var messageReceived = function (message) {
var note = tsw.midiNote(message.data[1]),
keys = document.querySelectorAll('#keyboard li');
// Only accept note data.
if (message.data[0] === 144) {
if (message.data[2]) {
// Change colour of key on keyboard.
for (var i = 0; i < keys.length; i++) {
if (keys[i].attributes.title.value === note) {
keys[i].style.backgroundColor = '#FFE976';
}
}
synth.playNote(note);
} else {
// Chance colour of key on keyboard.
for (var i = 0; i < keys.length; i++) {
if (keys[i].attributes.title.value === note) {
if (keys[i].attributes.title.value.match('#')) {
keys[i].style.backgroundColor = 'black';
} else {
keys[i].style.backgroundColor = 'white';
}
}
}
synth.stopNote(tsw.midiNote(message.data[1]));
}
}
};
tsw.getUserMidi(function (midi) {
window.midi = midi
var inputs = midi.inputs();
//document.querySelector('.config-bar').style.display = 'block';
if (inputs.length) {
var input = inputs[0];
input.addEventListener('midimessage', messageReceived);
}
}, function () {
console.log('no midi support')
});
</script>
<canvas id="volume-graph" style="border: 1px solid white; display: none;"></canvas>
</body>
</html>