-
Notifications
You must be signed in to change notification settings - Fork 0
/
gestureSynth.js
125 lines (110 loc) · 3.7 KB
/
gestureSynth.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
(function(){
'use strict';
var targetChannel = null;
var userChannel = null;
var accuracy = 0.0005;
var euclideanDistance = function(a,b){
if(a.length != b.length){
throw new Error('vectors differently sized');
}
var x = 0;
for(var i = 0; i < a.length; i++){
x+=Math.pow(a[i]-b[i],2);
}
return Math.sqrt(x);
};
var randomNormalisedVec3 = function(){
return [Math.random(),Math.random(),Math.random()];
};
var normalisationVector = [1/360,1/360,1/180];
var normalisationOffsetVector = [0,0.5,0.5];
var normaliseOrientation = function(o){
return [
o[0]*normalisationVector[0]+normalisationOffsetVector[0],
o[1]*normalisationVector[1]+normalisationOffsetVector[1],
o[2]*normalisationVector[2]+normalisationOffsetVector[2],
];
};
var ac = new AudioContext();
var ChannelSynth = function(pan){
this.osc = ac.createOscillator();
this.lfo = ac.createOscillator();
this.fil = ac.createBiquadFilter();
this.pan = ac.createStereoPanner();
this.lfo.connect(this.osc.frequency);
this.osc.connect(this.fil);
this.fil.connect(this.pan);
this.pan.connect(ac.destination);
this.osc.type = 'square';
this.lfo.type = 'sine';
this.fil.Q.value = 30;
this.lfo.start(0);
this.osc.start(0);
this.applyNormalisedParameterVector = function(vec){
// DEBUGGING
var log = false;
for(var i = 0; i < vec.length; i++){
if(!isFinite(vec[i])){
log = true;
}
}
if(log){
console.log(vec);
}
// /DEBUGGING
this.osc.frequency.value = 100+vec[0]*900;
this.lfo.frequency.value = 10*vec[1];
this.fil.frequency.value = 50+vec[2]*2000;
};
this.stop = function(){
this.osc.stop();
};
this.pan.pan.value = pan==="left"?-0.999:0.999;
};
document.addEventListener('DOMContentLoaded',function(){
console.log('documentReady');
var resetTargetChannelParametersButton = document.createElement('button');
resetTargetChannelParametersButton.appendChild(document.createTextNode('Reset'));
var startSoundButton = document.createElement('button');
startSoundButton.appendChild(document.createTextNode('Start'));
var targetSound = [0,0,0];
resetTargetChannelParametersButton.addEventListener("click",function(){resetTargetChannel();});
startSoundButton.addEventListener("click",function(){
if(targetChannel){
targetChannel.stop();
targetChannel = null;
}
if(userChannel){
userChannel.stop();
userChannel = null;
}
targetChannel = new ChannelSynth("right");
userChannel = new ChannelSynth("left");
resetTargetChannel();
var displayRanges = {
alpha:document.getElementById('alpha'),
beta:document.getElementById('beta'),
gamma:document.getElementById('gamma')
};
if (window.DeviceOrientationEvent) {
window.addEventListener("deviceorientation", function () {
var npv = normaliseOrientation([event.alpha,event.beta, event.gamma]);
console.log(npv);
if(Math.abs(npv[0]-targetSound[0]) < accuracy){
resetTargetChannel();
}
userChannel.applyNormalisedParameterVector(npv);
displayRanges.alpha.value = event.alpha;
displayRanges.beta.value = event.beta;
displayRanges.gamma.value = event.gamma;
}, true);
}
});
document.body.appendChild(resetTargetChannelParametersButton);
document.body.appendChild(startSoundButton);
var resetTargetChannel = function(){
targetSound = randomNormalisedVec3();
targetChannel.applyNormalisedParameterVector(targetSound);
};
});
})();