-
Notifications
You must be signed in to change notification settings - Fork 3
/
devices.js
141 lines (127 loc) · 4.84 KB
/
devices.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Copyright 2020 David Whiting
This work is licensed under a Creative Commons Attribution 4.0 International License
https://creativecommons.org/licenses/by/4.0/
*/
/** Simple monosynth with decay-enveloped amp and lowpass filter */
class MonoSynth {
constructor(audio, type="sawtooth") {
this.osc = audio.createOscillator();
this.amp = audio.createGain();
this.filter = audio.createBiquadFilter();
this.osc.connect(this.filter);
this.filter.connect(this.amp);
this.out = this.amp;
this.osc.type = type;
this.osc.frequency.setValueAtTime(110, audio.currentTime);
this.amp.gain.setValueAtTime(0.1, audio.currentTime);
this.osc.start();
this.filter.type = "lowpass";
this.filter.frequency.setValueAtTime(440, audio.currentTime);
this.filter.Q.setValueAtTime(10, audio.currentTime);
this.audio = audio;
}
connect(target) {
this.out.connect(target);
}
play(pitch, filterFreq, decay) {
let ctime = this.audio.currentTime+0.01;
const attack = 0.04;
const glide = 0.005;
const error = Math.random()*0.01 - 0.005;
this.osc.frequency.cancelScheduledValues(ctime);
this.osc.frequency.setTargetAtTime((1 + error) * pitch, ctime, glide);
this.amp.gain.cancelScheduledValues(ctime);
this.amp.gain.setTargetAtTime(0.15, ctime, attack);
this.amp.gain.setTargetAtTime(0.0, ctime + attack, decay);
this.filter.frequency.cancelScheduledValues(ctime);
this.filter.frequency.setTargetAtTime(filterFreq, ctime, attack);
this.filter.frequency.setTargetAtTime(filterFreq / 2, ctime + attack, decay/2);
}
}
/** Descending sine-based kick sound */
class Kick {
constructor(audio) {
this.osc = audio.createOscillator();
this.osc.type = "sine";
this.amp = audio.createGain();
this.amp.gain.setValueAtTime(0, audio.currentTime);
this.osc.connect(this.amp);
this.osc.start(audio.currentTime);
this.out = this.amp;
this.audio = audio;
}
connect(target) {
this.out.connect(target);
}
play() {
let ctime = this.audio.currentTime+0.01;
this.amp.gain.cancelScheduledValues(ctime);
this.amp.gain.setTargetAtTime(0.5, ctime, 0.002);
this.amp.gain.setTargetAtTime(0, ctime +0.002, 0.1);
this.osc.frequency.cancelScheduledValues(ctime);
this.osc.frequency.setTargetAtTime(440, ctime, 0.002);
this.osc.frequency.setTargetAtTime(55, ctime +0.002, + 0.02);
}
}
class Hat {
constructor(audio) {
this.osc = this.noise(audio);
this.amp = audio.createGain();
this.amp.gain.setValueAtTime(0, audio.currentTime);
this.osc.connect(this.amp);
this.out = this.amp;
this.audio = audio;
}
connect(target) {
this.out.connect(target);
}
play(gain = 0.2, decay = Math.random() * 0.02+0.01) {
let ctime = this.audio.currentTime + 0.01;
this.amp.gain.cancelScheduledValues(ctime);
this.amp.gain.setTargetAtTime(gain*0.4, ctime, 0.002);
this.amp.gain.setTargetAtTime(0, ctime + 0.002, decay);
}
noise(audio) {
const bufferSize = 4096;
const whiteNoise = audio.createScriptProcessor(bufferSize, 1, 1);
whiteNoise.onaudioprocess = function (e) {
const output = e.outputBuffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) {
output[i] = Math.random() * 2 - 1;
}
};
return whiteNoise;
}
}
/** Delay line with feedback */
class FeedbackDelay {
constructor(audio, delayTime, feedback) {
this.delay = audio.createDelay();
this.delay.delayTime.setValueAtTime(delayTime, audio.currentTime);
this.delayFeedbackGain = audio.createGain();
this.delayFeedbackGain.gain.setValueAtTime(feedback, audio.currentTime);
this.feedback = this.delayFeedbackGain.gain;
this.delay.connect(this.delayFeedbackGain);
this.in = this.delay;
this.out = this.delayFeedbackGain;
this.delayFeedbackGain.connect(this.delay);
}
}
/** Panner unit (should be just StereoPanner but that's not supported by Safari) */
class Panner {
constructor(audio, pan) {
if (audio.createStereoPanner) {
this.panner = audio.createStereoPanner();
this.panner.pan.setValueAtTime(pan, audio.currentTime);
} else {
// https://stackoverflow.com/questions/52809552/how-to-pan-audio-in-ios-web-audio-implementation
this.panner = audio.createPanner();
this.panner.panningModel = "equalpower";
this.panner.setPosition(pan,0,1-Math.abs(pan));
}
this.in = this.panner;
this.out = this.panner;
}
}
export {MonoSynth, FeedbackDelay, Kick, Panner, Hat}