-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsubmono.js
126 lines (111 loc) · 4.38 KB
/
submono.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var Monosynth = function Monosynth(audioCtx, config) {
var synth;
var Synth = function Synth() {
synth = this;
config = config || {};
config.cutoff = config.cutoff || {};
config.lfo = config.lfo || {};
synth.audioCtx = audioCtx,
synth.amp = audioCtx.createGain(),
synth.filter = audioCtx.createBiquadFilter(),
synth.osc = audioCtx.createOscillator(),
synth.pan = audioCtx.createPanner(),
synth.maxGain = config.maxGain || 0.9, // out of 1
synth.attack = config.attack || 0.1, // in seconds
synth.decay = config.decay || 0.0, // in seconds
synth.sustain = config.sustain || 1.0, // out of 1
synth.release = config.release || 0.8, // in seconds
// low-pass filter
synth.cutoff = synth.filter.frequency;
synth.cutoff.maxFrequency = config.cutoff.maxFrequency || 7500; // in hertz
synth.cutoff.attack = config.cutoff.attack || 0.1; // in seconds
synth.cutoff.decay = config.cutoff.decay || 2.5; // in seconds
synth.cutoff.sustain = config.cutoff.sustain || 0.2; // out of 1
// low frequency oscillator
synth.lfo.osc = audioCtx.createOscillator();
synth.lfo.amp = audioCtx.createGain();
synth.lfo.osc.connect(synth.lfo.amp);
synth.lfo.amp.connect(synth.osc.frequency); // TODO: allow routing to any destination (for now, hard-coded as pitch vibrato)
synth.lfo.osc.start();
synth.lfo.depth(config.lfo.depth || 0);
synth.lfo.frequency(config.lfo.frequency || 6);
// primary oscillator
synth.amp.gain.setValueAtTime(0, 0);
synth.filter.type = 'lowpass';
synth.filter.connect(synth.amp);
synth.amp.connect(audioCtx.destination);
synth.pan.panningModel = 'equalpower';
synth.pan.setPosition(0, 0, 1); // start with stereo image centered
synth.osc.connect(synth.pan);
synth.pan.connect(synth.filter);
synth.osc.start();
synth.waveform(config.waveform || 'sine');
synth.pitch(config.pitch || 440);
return synth;
};
function getNow() {
var now = synth.audioCtx.currentTime;
synth.amp.gain.cancelScheduledValues(now);
synth.amp.gain.setValueAtTime(synth.amp.gain.value, now);
return now;
}
Synth.prototype = {
pitch: function pitch(newPitch) {
if (typeof newPitch !== 'undefined') {
var now = synth.audioCtx.currentTime;
synth.osc.frequency.setValueAtTime(newPitch, now);
return newPitch;
}
return synth.osc.frequency.value;
},
waveform: function waveform(newWaveform) {
if (typeof newWaveform !== 'undefined') {
synth.osc.type = newWaveform;
}
return synth.osc.type;
},
// apply attack, decay, sustain envelope
start: function startSynth() {
var atk = parseFloat(synth.attack);
var dec = parseFloat(synth.decay);
var cAtk = parseFloat(synth.cutoff.attack);
var cDec = parseFloat(synth.cutoff.decay);
var now = getNow();
synth.cutoff.cancelScheduledValues(now);
synth.cutoff.linearRampToValueAtTime(synth.cutoff.value, now);
synth.cutoff.linearRampToValueAtTime(synth.cutoff.maxFrequency, now + cAtk);
synth.cutoff.linearRampToValueAtTime(synth.cutoff.sustain * synth.cutoff.maxFrequency, now + cAtk + cDec);
synth.amp.gain.linearRampToValueAtTime(synth.maxGain, now + atk);
synth.amp.gain.linearRampToValueAtTime(synth.sustain * synth.maxGain, now + atk + dec);
},
// apply release envelope
stop: function stopSynth() {
var rel = parseFloat(synth.release);
var now = getNow();
synth.amp.gain.linearRampToValueAtTime(0, now + rel);
},
lfo: {
depth: function lfoDepth(newDepth) {
if (typeof newDepth !== 'undefined') {
var now = synth.audioCtx.currentTime;
synth.lfo.amp.gain.setValueAtTime(newDepth, now);
return newDepth;
}
return synth.lfo.amp.gain.value;
},
frequency: function lfoFrequency(newFrequency) {
if (typeof newFrequency !== 'undefined') {
var now = synth.audioCtx.currentTime;
synth.lfo.osc.frequency.setValueAtTime(newFrequency, now);
return newFrequency;
}
return synth.lfo.frequency.value;
},
}
};
return new Synth();
};
// npm support
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Monosynth;
}