-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathflock.js
72 lines (57 loc) · 1.98 KB
/
flock.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
function flock(ac , num, type){
if (!ac) {
console.error('AudioContext is required!');
return;
}
if (type === "triple-tailed-tree-troubler"){
console.log("The triple-tailed-tree-troubler model causes a horrible feedback as a flock. Will debug later");
return null;
}
var CHIRP_SPREAD = 1.5;
var FREQ_SPREAD = 0.15;
var ENV_SPREAD = 0.3;
var birds = [];
var flockPanner = ac.createPanner();
flockPanner.panningModel = "equalpower";
flockPanner.distanceModel = "exponential";
flockPanner.refDistance = 0.3;
for (var i = 0; i < num; i++){
b = new bird(ac, type);
b.connect(flockPanner);
birds.push(b);
}
var defaultFreq = birds[0].frequency;
var defaultAttack = birds[0].mainEnvelope.attackTime;
var defaultDecay = birds[0].mainEnvelope.decayTime;
this.position = {x:0, y:0, z: 0};
this.orientation = {x:0, y:0, z: 0};
this.velocity = {x:0, y:0, z: 0};
this.positionSpread = 0.5;
this.orientationSpread = 0.5;
flockPanner.connect(ac.destination);
this.connect = function(audioNode){
flockPanner.connect(audioNode);
};
this.chirp = function(time){
self = this;
// flockPanner.setVelocity(this.velocity.x, this.velocity.y, this.velocity.z);
birds.forEach(function(bird){
bird.frequency = defaultFreq+((Math.random()-0.5)*FREQ_SPREAD);
bird.mainEnvelope.attackTime = defaultAttack+((Math.random()-0.5)*ENV_SPREAD);
bird.mainEnvelope.decayTime = defaultDecay+((Math.random()-0.5)*ENV_SPREAD);
bird.position = randomizeVector(self.position, self.positionSpread);
bird.orientation = randomizeVector(self.orientation, self.orientationSpread);
bird.chirp(ac.currentTime + ((Math.random()-0.5)*CHIRP_SPREAD));
});
};
function randomizeVector(inputVector, spread){
var outputVector = inputVector;
outputVector.x += Math.random()*spread;
outputVector.y += Math.random()*spread;
outputVector.x += Math.random()*spread;
return outputVector;
}
function rnd_snd() {
return (Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1);
}
}