-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple-key.js
44 lines (31 loc) · 890 Bytes
/
simple-key.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
// A square wave key, for simplicity and performance
const SimpleKey = function(){
this.isDown = false;
this.ratio = 2.4;
this.frequency = 1;
this.switch = context.createGain();
this.output = context.createGain();
this.oscillator = context.createOscillator();
this.oscillator.type = "triangle";
this.oscillator.frequency.value = this.frequency;
this.oscillator.start();
this.oscillator.connect(this.switch);
this.switch.connect(this.output);
this.keyDown();
this.keyUp();
return this;
}
SimpleKey.prototype.setFrequency = function(frequency) {
this.frequency = frequency;
this.oscillator.frequency.value = frequency;
}
SimpleKey.prototype.keyDown = function(){
if(this.isDown) return;
this.isDown = true;
this.switch.gain.value = 1.0;
}
SimpleKey.prototype.keyUp = function(){
if(!this.isDown) return;
this.isDown = false;
this.switch.gain.value = 0.0;
}