-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsynth.js
169 lines (136 loc) · 3.89 KB
/
synth.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
class Synth {
constructor(adsr={a:0,d:0,s:0,r:0}) {
this.adsrEnv = adsr;
}
get audioContext() {
return this.ac;
}
set audioContext(value) {
this.ac = value;
this.engine();
}
engine() {
this.osc = this.ac.createOscillator();
this.osc.type = 'sine';
this.oscGain = this.ac.createGain();
this.osc.onended = _ => {
this.disconnect();
}
}
MIDINoteToFreq(midiNote) {
return (2**((midiNote-69.0)/12))*440.0;
}
MIDIVelToAmp(midiValue) {
return midiValue/127.0;
}
set onended(f) {
this.osc.onended = f;
}
get waveType() {
return this.osc.type;
}
set waveType(type) {
this.osc.type = type;
}
get note() {
return this.osc.frequency.value;
}
set note(note) {
this.osc.frequency.setValueAtTime(this.MIDINoteToFreq(note), 0);
}
get adsr() {
return this.adsrEnv;
}
set adsr(value) {
this.adsrEnv = value;
}
get vel() {
return this.oscGain.gain.value;
}
set vel(value) {
this.sVel = this.MIDIVelToAmp(value);
}
connect(destination) {
this.osc.connect(this.oscGain);
this.oscGain.connect(destination);
}
disconnect(time=0) {
this.osc.disconnect(time);
this.oscGain.disconnect(time);
}
start(currentTime=0) {
this.osc.start(0);
this.oscGain.gain.setValueAtTime(0,0);
this.oscGain.gain.linearRampToValueAtTime(1.0, this.ac.currentTime+this.adsrEnv.a);
this.oscGain.gain.linearRampToValueAtTime(this.sVel, this.ac.currentTime+this.adsrEnv.a+this.adsrEnv.d);
this.oscGain.gain.linearRampToValueAtTime(this.sVel, this.ac.currentTime+this.adsrEnv.a+this.adsrEnv.d+this.adsrEnv.s);
this.stop(this.ac.currentTime+this.adsrEnv.a+this.adsrEnv.d+this.adsrEnv.s);
}
stop(currentTime=0) {
try {
this.oscGain.gain.linearRampToValueAtTime(0.0, currentTime+this.adsrEnv.r);
this.osc.stop(currentTime+this.adsrEnv.r);
} catch(err) {
console.log(err);
}
}
}
// Synth class extended to become a FM synth
class SynthFM extends Synth {
engine() {
this.carrier = ac.createOscillator();
this.carrierGain = ac.createGain();
this.modulator = ac.createOscillator();
this.modulator.frequency.setValueAtTime(500, 0);
this.modulatorGain = ac.createGain();
this.modulatorGain.gain.setValueAtTime(50, 0);
this.carrier.onended = _ => {
this.disconnect();
}
this.modulator.connect(this.modulatorGain);
}
set waveType(type) {
this.carrier.type = type;
}
get note() {
return this.carrier.frequency.value;
}
set note(note) {
this.carrier.frequency.setValueAtTime(this.MIDINoteToFreq(note), 0);
}
get vel() {
return this.carrierGain.gain.value;
}
set vel(value) {
this.sVel = this.MIDIVelToAmp(value);
}
connect(destination) {
this.modulatorGain.connect(this.carrier.frequency);
this.carrier.connect(this.carrierGain);
this.carrierGain.connect(destination);
}
disconnect(time=0) {
this.modulator.disconnect(time);
this.modulatorGain.disconnect(time);
this.carrier.disconnect(time);
this.carrierGain.disconnect(time);
}
start(currentTime=0) {
this.modulator.start(0);
this.carrier.start(0);
this.carrierGain.gain.setValueAtTime(0,0);
this.carrierGain.gain.linearRampToValueAtTime(1.0, this.ac.currentTime+this.adsrEnv.a);
this.carrierGain.gain.linearRampToValueAtTime(this.sVel, this.ac.currentTime+this.adsrEnv.a+this.adsrEnv.d);
this.carrierGain.gain.linearRampToValueAtTime(this.sVel, this.ac.currentTime+this.adsrEnv.a+this.adsrEnv.d+this.adsrEnv.s);
this.stop(this.ac.currentTime+this.adsrEnv.a+this.adsrEnv.d+this.adsrEnv.s);
}
stop(currentTime=0) {
try {
this.carrierGain.gain.linearRampToValueAtTime(0.0, currentTime+this.adsrEnv.r);
this.carrier.stop(currentTime+this.adsrEnv.r);
this.modulator.stop(currentTime+this.adsrEnv.r);
} catch(err) {
console.log(err);
}
}
}