-
Notifications
You must be signed in to change notification settings - Fork 0
/
limiter-plugin.js
53 lines (45 loc) · 1.15 KB
/
limiter-plugin.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
/**
* LIMITER PLUGIN for Volca Sampler
*
* Created by Ben Wiley 2023
*
* MIT License:
* https://github.com/benwiley4000/volca-sampler-plugins/blob/master/LICENSE
*/
samplePlugin.params = {
gainReduction: {
label: "Gain reduction (dB)",
value: 3,
min: 0,
max: 100,
},
attack: {
label: "Attack time (seconds)",
value: 0.01,
min: 0,
max: 0.3,
},
};
/**
* @param {AudioBuffer} audioBuffer
*/
function samplePlugin(audioBuffer) {
const gainReduction = samplePlugin.params.gainReduction.value;
const attack = samplePlugin.params.attack.value;
const { numberOfChannels, sampleRate, length } = audioBuffer;
const audioContext = new OfflineAudioContext({
numberOfChannels,
sampleRate,
length,
});
const bufferSource = audioContext.createBufferSource();
bufferSource.buffer = audioBuffer;
const limiter = audioContext.createDynamicsCompressor();
limiter.ratio.value = 20;
limiter.attack.value = attack;
limiter.threshold.value = 0 - gainReduction;
bufferSource.connect(limiter);
limiter.connect(audioContext.destination);
bufferSource.start();
return audioContext.startRendering();
}