-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynth.js
256 lines (194 loc) · 5.74 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
let midiNotes = [];
let env;
let osc;
let fft;
let durationSlider;
let noise;
let filt;
let freq = 400;
let lfo, freqSlider, ampSlider;
let note = 0;
let noiseOn = false;
let mainOscOn = false;
let playing = false;
let beatLengthSlider, beatRateSlider, beatFrequencySlider;
let beatLength;
let beatRate;
let beatFrequency;
let beatInterval;
let main_osc;
let main_freq = 400;
let lfo1, freq1Slider, amt1Slider;
let lfo2, freq2Slider, amt2Slider;
function setup() {
//envelope
env = new p5.Envelope();
osc = new p5.Oscillator();
osc.setType('sine');
// Start the oscillator but don't play it immediately
osc.start();
osc.amp(0);
// Set the envelope parameters
env.setADSR(0.1, 0.5, 0.1, 0.5);
env.setRange(0.5, 0);
// Create a slider for note duration
durationSlider = select('#durationSlider');
durationSlider.input(updateDuration);
text('Note ');
lfo = new p5.Oscillator();
lfo.start();
lfo.disconnect();
filt = new p5.Filter('bandpass');
noise = new p5.Noise();
noise.disconnect();
noise.connect(filt);
filt.freq(lfo);
// Ensure that the noise is off when the page is loaded
noise.stop();
lfo1 = new p5.Oscillator();
lfo1.start();
// so that lfo is not sent system audio
lfo1.disconnect();
lfo2 = new p5.Oscillator();
lfo2.start();
lfo2.disconnect();
//Modulating an oscillator (main) output with a Low Frequency Oscillator (LFO1) which is controlled by another one (LFO2)!
main_osc = new p5.Oscillator();
main_osc.freq(lfo1);
lfo1.freq(lfo2);
freqSlider = select('#audioRateSlider');
freqSlider.input(updateAudioRate);
ampSlider = select('#audioAmountSlider');
ampSlider.input(updateAudioAmount);
beatLengthSlider = select('#beatLengthSlider');
beatLengthSlider.input(updateBeatLength);
beatRateSlider = select('#beatRateSlider');
beatRateSlider.input(updateBeatRate);
beatFrequencySlider = select('#beatFrequencySlider');
beatFrequencySlider.input(updateBeatFrequency);
// Play the click beat on a loop
beatInterval = setInterval(playClickBeat, beatRate); // Update to beatInterval
freq1Slider = select('#L1F_Slider');
freq1Slider.input(updateL1F);
amt1Slider = select('#L1A_Slider');
amt1Slider.input(updateL1A);
freq2Slider = select('#L2F_Slider');
freq2Slider.input(updateL2F);
amt2Slider = select('#L2A_Slider');
amt2Slider.input(updateL2A);
}
function draw() {
lfo.freq(freqSlider.value());
lfo.amp(ampSlider.value());
filt.freq(lfo);
lfo1.freq(freq1Slider.value());
lfo1.amp(amt1Slider.value());
lfo2.freq(freq2Slider.value());
lfo2.amp(amt2Slider.value());
// beatLengthSlider.value()
// beatRateSlider.value()
// beatFrequencySlider.value()
}
function playNote(note) {
// Set the frequency of the oscillator based on the MIDI note
let frequency = midiToFreq(note);
osc.freq(frequency);
// Set the duration based on the slider value
let duration = durationSlider.value();
// Set the envelope duration dynamically
env.setADSR(0.1, duration, 0.1, 0.5);
// Trigger the envelope to play the note
env.play(osc, 0, 0.1);
// Stop the sound after the envelope release phase
setTimeout(() => {
osc.amp(0); // To stop the sound
}, duration * 1000);
}
function toggleNoise() {
noiseOn = !noiseOn; // Toggle the state
if (noiseOn) {
noise.start(); // Start the noise
noise.amp(0.2);
} else {
noise.stop(); // Stop the noise
noise.amp(0);
}
}
function toggleMainOscillator() {
mainOscOn = !mainOscOn;
if (mainOscOn) {
main_osc.start();// Turn on the main oscillator
main_osc.amp(0.2);
} else {
main_osc.amp(0); // Turn off the main oscillator
}
}
// function playClickBeat() {
// if (!playing) {
// playClick();
// // Update based on the current beatRateSlider value
// clearInterval(beatInterval);
// beatInterval = setInterval(playClickBeat, beatRateSlider.value());
// }
// }
// function playClick() {
// const clickSound = new p5.Oscillator();
// clickSound.setType('sine');
// clickSound.freq(beatFrequencySlider.value());
// clickSound.start();
// // Stop the beat after a short duration
// setTimeout(() => {
// clickSound.stop();
// }, beatLengthSlider.value());
// }
function playClickBeat() {
if (!playing) {
playClick();
// Update the interval dynamically based on the current beatRateSlider value
console.log(beatRateSlider.value())
clearInterval(beatInterval);
beatInterval = setInterval(playClickBeat, beatRateSlider.value()); // Update to beatInterval
// console.log(beatRateSlider.value())
}
}
function playClick() {
// Simulate a click sound (adjust as needed)
const clickSound = new p5.Oscillator();
clickSound.setType('sine');
clickSound.freq(beatFrequencySlider.value());
clickSound.start();
// Stop the click sound after a short duration
setTimeout(() => {
clickSound.stop();
}, beatLengthSlider.value());
}
function updateDuration() {
select('#durationValue').html(durationSlider.value());
}
function updateAudioRate() {
select('#audioRateValue').html(freqSlider.value());
}
function updateAudioAmount() {
select('#audioAmountValue').html(ampSlider.value());
}
function updateBeatLength() {
// Update the displayed duration value
select('#beatLengthValue').html(beatLengthSlider.value());
}
function updateBeatRate() {
select('#beatRateValue').html(beatRateSlider.value());
}
function updateBeatFrequency() {
select('#beatFrequencyValue').html(beatFrequencySlider.value());
}
function updateL1F() {
select('#L1F_Value').html(freq1Slider.value());
}
function updateL1A() {
select('#L1A_Value').html(amt1Slider.value());
}
function updateL2F() {
select('#L2F_Value').html(freq2Slider.value());
}function updateL2A() {
select('#L2A_Value').html(amt2Slider.value());
}