-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathorgan.js
70 lines (49 loc) · 1.81 KB
/
organ.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
// A complex instrument, which contains many keys and set their frequencies, post-process their output, and so on
var Organ = {
// This corresponds to the drawbar harmonics on say a real Hammond B3 organ
harmonicRatios: [1/2, 1, 3/2, 2, 3, 4, 5, 6, 8],
// And these are the actual volume of each harmonic component. Here's a very bluesy preset.
harmonicLevels: [1, 1, 0, 0, 0, 0, 0, 1, 0],
//harmonicLevels: [1, 1, 1, 1, 1, 1, 1, 1, 1], // Uniform harmonic content
//harmonicLevels: [0, 1, 0, 0, 0, 0, 0, 0, 0], // Fundamental tone
//harmonicLevels: [1, 1, 1, 0, 0, 0, 0, 0, 0], // "Jimmy Smith" preset
keys: [],
init: function() {
this.output = context.createGain();
// Tremolo
this.tremoloFreq = context.createOscillator();
this.tremoloDepth = context.createGain()
this.tremoloNode = context.createGain();
this.tremoloFreq.type = "sine";
this.tremoloFreq.frequency.value = 10;
//this.tremoloFreq.start();
this.tremoloDepth.gain.value = .2;
this.tremoloNode.gain.value = 1.0;
this.tremoloFreq.connect(this.tremoloDepth);
//this.tremoloDepth.connect(this.tremoloNode.gain);
this.tremoloNode.connect(this.output);
// Init keys
this.keys = [];
for(var row = 0; row < 4; row++)
for(var col = 0; col < 12; col++) {
const key = window.fastMode ? new SimpleKey() : new Key();
key.output.connect(this.tremoloNode);
this.keys.push(key);
}
},
setBaseFrequency: function(baseFrequency) {
this.baseFrequency = baseFrequency;
this.keys.forEach(key => {
key.setFrequency( baseFrequency * key.ratio );
});
},
setScale: function(scale) {
this.keys.forEach((key, i) => {
const col = i % 12;
const row = (i-col)/12;
const index = col % scale.length;
const octave = (col-index)/scale.length;
key.ratio = Math.pow(2, row+octave) * scale[index];
});
}
}