-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.js
49 lines (41 loc) · 1.35 KB
/
block.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
function Block(order, freq) {
this.id = guid();
this.order = order;
this.active = false;
this.freq = freq;
this.initAudio(freq);
}
// Initialize oscillator and amp objects, attaching them to the conext variable
Block.prototype.initAudio = function(frequency) {
oscillator = context.createOscillator();
fixOscillator(oscillator);
oscillator.frequency.value = frequency;
amp = context.createGainNode();
amp.gain.value = 0;
// Connect ooscillator to amp and amp to the mixer of the context.
// This is like connecting cables between jacks on a modular synth.
oscillator.connect(amp);
amp.connect(context.destination);
oscillator.start(0)
this.oscillator = oscillator;
this.amp = amp;
this.amp.gain.linearRampToValueAtTime(0.0, context.currentTime + 0);
}
// Play a sound for a given block
Block.prototype.play = function() {
this.amp.gain.linearRampToValueAtTime(0.5, context.currentTime + 0.1);
this.amp.gain.linearRampToValueAtTime(0.0, context.currentTime + 0.5);
}
// Change when a block is active
Block.prototype.toggleActive = function() {
this.active = !this.active;
}
// Used for generating random ids
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
};
function guid() {
return s4() + s4() + s4() + s4();
}