-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsubpoly.js
107 lines (89 loc) · 3.01 KB
/
subpoly.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
// npm support
if (typeof require !== 'undefined') {
var Monosynth = require('submono');
}
var Polysynth = function Polysynth(audioCtx, config) {
var synth;
var Synth = function Synth() {
synth = this;
synth.audioCtx = audioCtx;
synth.voices = [];
config = config || {};
config.cutoff = config.cutoff || {};
for (var i = 0, ii = config.numVoices || 16; i < ii; i++) {
synth.voices.push(new Monosynth(audioCtx, config));
}
synth.stereoWidth = config.stereoWidth || 0.5; // out of 1
synth.width(synth.stereoWidth);
return synth;
};
// apply attack, decay, sustain envelope
Synth.prototype.start = function startSynth() {
synth.voices.forEach(function startVoice(voice) {
voice.start();
});
};
// apply release envelope
Synth.prototype.stop = function stopSynth() {
synth.voices.forEach(function stopVoice(voice) {
voice.stop();
});
};
// get/set synth stereo width
Synth.prototype.width = function width(newWidth) {
if (synth.voices.length > 1 && newWidth) {
synth.stereoWidth = newWidth;
synth.voices.forEach(function panVoice(voice, i) {
var spread = 1/(synth.voices.length - 1);
var xPos = spread * i * synth.stereoWidth;
var zPos = 1 - Math.abs(xPos);
voice.pan.setPosition(xPos, 0, zPos);
});
}
return synth.stereoWidth;
};
// convenience methods for changing values of all Monosynths' properties at once
(function createSetters() {
var monosynthProperties = ['maxGain', 'attack', 'decay', 'sustain', 'release'];
var monosynthCutoffProperties = ['maxFrequency', 'attack', 'decay', 'sustain'];
var monosynthLfoProperties = ['depth', 'frequency'];
monosynthProperties.forEach(function createSetter(property) {
Synth.prototype[property] = function setValues(newValue) {
synth.voices.forEach(function setValue(voice) {
voice[property] = newValue;
});
};
});
Synth.prototype.cutoff = {};
monosynthCutoffProperties.forEach(function createSetter(property) {
Synth.prototype.cutoff[property] = function setValues(newValue) {
synth.voices.forEach(function setValue(voice) {
voice.cutoff[property] = newValue;
});
};
});
Synth.prototype.lfo = {};
monosynthLfoProperties.forEach(function createSetter(property) {
Synth.prototype.lfo[property] = function setValues(newValue) {
synth.voices.forEach(function setValue(voice) {
voice.lfo[property](newValue);
});
};
});
Synth.prototype.waveform = function waveform(newWaveform) {
synth.voices.forEach(function waveform(voice) {
voice.waveform(newWaveform);
});
};
Synth.prototype.pitch = function pitch(newPitch) {
synth.voices.forEach(function pitch(voice) {
voice.pitch(newPitch);
});
};
})();
return new Synth;
};
// npm support
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Polysynth;
}