-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
96 lines (80 loc) · 3.03 KB
/
app.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
// create audio api context
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// create 3 x osc node
var osc1 = audioCtx.createOscillator();
var osc2 = audioCtx.createOscillator();
var osc3 = audioCtx.createOscillator();
// marshall stack / FX
var analyser = audioCtx.createAnalyser();
var distortion = audioCtx.createWaveShaper();
var gainNode = audioCtx.createGain();
var biquadFilter = audioCtx.createBiquadFilter();
var convolver = audioCtx.createConvolver();
var freqValue = 50;
var xValue = 0;
var yValue = 0;
var rotation = 0;
var duck = document.getElementById("imgLogo");
if (location.protocol != "https:") {
location.href = "https:" + window.location.href.substring(window.location.protocol.length);
}
function startOscillators() {
osc1.type = 'square';
osc2.type = 'sine';
osc3.type = 'triangle';
osc1.frequency.value = freqValue;
osc2.frequency.value = freqValue;
osc3.frequency.value = freqValue;
osc1.connect(distortion);
osc2.connect(distortion);
osc3.connect(gainNode);
gainNode.connect(convolver);
convolver.connect(audioCtx.destination);
osc1.start();
osc2.start();
osc3.start();
}
function permission() {
if (typeof (DeviceMotionEvent) !== "undefined" && typeof (DeviceMotionEvent.requestPermission) === "function") {
DeviceMotionEvent.requestPermission()
.then(response => {
if (response == "granted") {
startOscillators()
window.addEventListener("deviceorientation", function (event) {
xValue = Math.round(event.gamma);
yValue = Math.round(event.beta);
rotation = Math.round(event.alpha);
document.getElementById("doTiltLR").innerHTML = Math.round(xValue);
document.getElementById("doTiltFB").innerHTML = Math.round(yValue);
document.getElementById("doDirection").innerHTML = Math.round(rotation);
duck.style.transform =
"rotate(" + xValue + "deg) rotate3d(1,0,0, " + (yValue * -1) + "deg)";
osc1.detune.value = freqValue * (xValue * 0.1);
osc2.detune.value = freqValue * (yValue * 0.1);
osc3.detune.value = freqValue * (rotation * 0.1);
}, true);
}
})
.catch(console.error);
} else {
alert("DeviceMotionEvent is not defined");
}
}
function makeDistortionCurve(amount) {
var k = typeof amount === 'number' ? amount : 50,
n_samples = 44100,
curve = new Float32Array(n_samples),
deg = Math.PI / 180,
i = 0,
x;
for (; i < n_samples; ++i) {
x = i * 2 / n_samples - 1;
curve[i] = (3 + k) * x * 20 * deg / (Math.PI + k * Math.abs(x));
}
return curve;
};
duck.addEventListener('click', function (event) {
permission();
});
distortion.curve = makeDistortionCurve(800);
distortion.oversample = '2x';