-
Notifications
You must be signed in to change notification settings - Fork 0
/
delay-plugin.js
81 lines (69 loc) · 1.75 KB
/
delay-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
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
/**
* DELAY PLUGIN for Volca Sampler
*
* Created by Ben Wiley 2023
*
* MIT License:
* https://github.com/benwiley4000/volca-sampler-plugins/blob/master/LICENSE
*/
samplePlugin.params = {
delayTime: {
label: "Delay time (seconds)",
value: 0.1,
min: 0.01,
max: 1,
},
level: {
label: "Level",
value: 0.5,
min: 0,
max: 1,
},
feedback: {
label: "Feedback",
value: 0.2,
min: 0,
max: 1.2,
},
tailTime: {
label: "Tail time (seconds)",
value: 0.1,
min: 0,
max: 3,
},
};
/**
* @param {AudioBuffer} audioBuffer
*/
function samplePlugin(audioBuffer) {
const delayTime = samplePlugin.params.delayTime.value;
const level = samplePlugin.params.level.value;
const feedback = samplePlugin.params.feedback.value;
const tailTime = samplePlugin.params.tailTime.value;
if (level === 0 && tailTime === 0) {
// same as if bypassed
return audioBuffer;
}
const { numberOfChannels, sampleRate, length } = audioBuffer;
const audioContext = new OfflineAudioContext({
numberOfChannels,
sampleRate,
length: length + Math.floor(tailTime * sampleRate),
});
const bufferSource = audioContext.createBufferSource();
bufferSource.buffer = audioBuffer;
const levelGain = audioContext.createGain();
levelGain.gain.value = level;
const delay = audioContext.createDelay();
delay.delayTime.value = delayTime;
const feedbackGain = audioContext.createGain();
feedbackGain.gain.value = feedback;
bufferSource.connect(levelGain);
levelGain.connect(delay);
delay.connect(feedbackGain);
feedbackGain.connect(delay);
bufferSource.connect(audioContext.destination);
delay.connect(audioContext.destination);
bufferSource.start();
return audioContext.startRendering();
}