-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
384 lines (326 loc) · 8.7 KB
/
sketch.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
Alt-Enter: Evaluate Line in Live Mode
Alt-Shift-Enter: Evaluate Block in Live Mode
*/
/*
Clone of the Dato DUO
Synth for two
*/
let player = 'synth' //synth or seq
let toneSig = new Tone.Signal()
let tonePitchshift = new Tone.Multiply()
let sawPitchshift = new Tone.Multiply()
let pulseWav = new Tone.PulseOscillator().start()
let sawWav = new Tone.Oscillator({type:'sawtooth'}).start()
let toneMixer = new Tone.Multiply()
let sawMixer = new Tone.Multiply()
let cutoffSig = new Tone.Signal()
let filterEnvelope = new Tone.Envelope()
let filterDepth = new Tone.Multiply()
let filterMultiplier = new Tone.Multiply()
let filter = new Tone.Filter()
let ampEnvelope = new Tone.Envelope()
let amp = new Tone.Multiply()
let dist = new Tone.Distortion(0.9)
let crusher = new Tone.BitCrusher(16)
let delay = new Tone.FeedbackDelay()
let masterOut = new Tone.Multiply(0.05).toDestination()
//let scope = new Oscilloscope('Canvas3')
//connect the initial signal to multipliers for pitch shift
//connect those to the oscillators
toneSig.connect(tonePitchshift), tonePitchshift.connect(pulseWav.frequency)
toneSig.connect(sawPitchshift), sawPitchshift.connect(sawWav.frequency)
toneSig.value = 500;
tonePitchshift.factor.value = 1;
sawPitchshift.factor.value = 1;
//connect the oscillators to a mixer and add them together
pulseWav.connect(toneMixer), toneMixer.connect(filter)
sawWav.connect(sawMixer), sawMixer.connect(filter)
toneMixer.factor.value = 1
sawMixer.factor.value = 1
//Connect the filter (VCF)
filterEnvelope.connect(filterDepth)
cutoffSig.connect(filter.frequency)
filterDepth.connect(filter.frequency)
cutoffSig.value = 1000
filterDepth.factor.value = 1000
filterEnvelope.attack = 0.2
filterEnvelope.decay = 0.1
filterEnvelope.sustain = 0.4
filterEnvelope.release = 0.2
filter.rolloff = -24
filter.Q.value = 1
//Connect the ASDR (VCA)
filter.connect(amp)
ampEnvelope.connect(amp.factor)
ampEnvelope.attack = 0.1
ampEnvelope.delay = 0.1
ampEnvelope.sustain = 0.9
ampEnvelope.release = 0.1
//effects chain
amp.connect(dist)
dist.connect(crusher)
crusher.connect(delay)
delay.connect(masterOut)
//masterOut.connect(scope.input)
//MIDI integration
/*
setNoteOnHandler( (note,vel)=>{
})
setNoteOffHandler( (note,vel)=>{
})
*/
//GUI
//All the effects start on even when they not supposed to
//implement portamento on the sequencer side
//accent should also increase volume. is there a way to get some make up
//gain whenever I hit accent
//beatpads don't work when you hit them twice fast
//hone in the sound design of the vcf envelope and vca envelope
//really all the different parameters to make it sound like the duo
const gui = new p5( sketch, 'p5-container-synth' )
/*
gui.setTheme('default')
gui.listThemes()
gui.setThemeParameters({
titleFont: 'Helvetica',
borderColor: [200,200,200]
//backgroundColor: [222,220,216]
})
*/
let distortion_toggle = gui.Toggle({
label:'Accent',
mapto: dist.wet,
x: 85, y:30
})
distortion_toggle.accentColor = [51,145,219]
dist.wet.value = 0
let crusher_toggle = gui.Toggle({
label:'bitcrusher',
mapto: crusher.bits,
min:16, max:2,
x: 90, y:70
})
crusher_toggle.accentColor = [46,152,99]
crusher.wet.value = 1
let glide_toggle = gui.Toggle({
label:'Glide',
callback: function(){}, //portamento on sequencer side
x: 15, y:30
})
glide_toggle.accentColor = [51,145,219]
let delay_toggle = gui.Toggle({
label:'Delay',
mapto: delay.wet,
x: 10, y:70
})
delay_toggle.accentColor = [46,152,99]
delay.wet.value = 0
let wave_fader = gui.Slider({
label:'wave',
mapto: pulseWav.width,
x: 39, y: 5, size: 2,
min:0, max: 1,
orientation: 'vertical',
showValue: false,
})
wave_fader.accentColor = [247, 5, 5]
wave_fader.borderColor = [20, 20, 20]
wave_fader.set(0.5)
let freq_fader = gui.Slider({
label:'freq',
callback: function(x){ cutoffSig.value = x},
x: 49, y: 5, size: 2,
min:500, max: 2000,
orientation: 'vertical',
showValue: false
})
freq_fader.accentColor = [247, 5, 5]
freq_fader.borderColor = [20, 20, 20]
freq_fader.set(1250)
let release_fader = gui.Slider({
label:'release',
callback: function(x){ filterEnvelope.release = x},
x: 59, y: 5, size: 2,
min:0, max: 5,
orientation: 'vertical',
showValue: false
})
release_fader.accentColor = [247, 5, 5]
release_fader.borderColor = [20, 20, 20]
release_fader.set(2.5)
let resonance_knob = gui.Knob({
label:'res',
callback: function(x){ filter.Q.value = x},
x: 50, y: 84, size:.25,
min:0.99999, max: 100, curve: 2,
showValue: false
})
resonance_knob.accentColor = [49,48,55]
resonance_knob.set( 1 )
let detune_knob = gui.Knob({
label:'detune',
mapto: tonePitchshift.factor,
x: 22, y: 52, size:.25,
min:0.99999, max: 3, curve: 1,
showValue: false
})
detune_knob.accentColor = [49,48,55]
detune_knob.set( 1 )
let speaker_knob = gui.Knob({
label:'gain',
mapto: masterOut.factor,
x: 78, y: 52, size:.25,
min:0, max: 0.1, curve: 2,
showValue: false
})
speaker_knob.accentColor = [49,48,55]
speaker_knob.set( 0.05 )
//sampler - beatpads
let kick = "audio/drums-003.mp3"
let snare = "audio/snare.mp3"
const kickPlayer = new Tone.Player(kick).toDestination()
const snarePlayer = new Tone.Player(snare).toDestination()
kickPlayer.playbackRate = 1
snarePlayer.playbackRate = 1
//trigger playback of the loaded soundfile
let kick_trigger = gui.Button({
label:'kick',
callback: function(){ kickPlayer.start()},
size: 1, border: 20,
x:30, y:80
})
kick_trigger.accentColor = [20,20,20]
let snare_trigger = gui.Button({
label:'snare',
callback: function(){ snarePlayer.start()},
size: 1, border: 20,
x:70, y:80
})
snare_trigger.accentColor = [20,20,20]
let seqgui = new p5(sketch, 'p5-container-seq')
//define our scale, sequence, octave, and index
let pitches = [60,60,60,60, 60,60,60,60]
let scale = [0,3,5, 7, 10]
let octave = 4
let index = 0
let transpose = 0
let isBoost = false
let isRandom = false
//convert scale degrees to midi notes
const scaleToMidi = function(degree){
//if our degree is larger than the length of the scale
let cur_octave = Math.floor(degree/scale.length)
degree = degree % scale.length
return scale[degree] + cur_octave * 12
}
const sequence = new Tone.Sequence( (time, note) => {
//calculate freq for note
let pitch = Tone.Midi(pitches[index]+octave*12+transpose).toFrequency()
toneSig.setValueAtTime(pitch, time);
if (isRandom){
let pitch = Tone.Midi(pitches[Math.floor(Math.random()*8)]+octave*12+transpose).toFrequency()
toneSig.setValueAtTime(pitch, time);
}
//vco.frequency.exponentialRampToValueAtTime(pitch, time+1);
ampEnvelope.triggerAttackRelease(.1, time);
ampEnvelope.triggerAttackRelease(.1, time);
//update index
index = ( index+1 ) % pitches.length
},
pitches, // Sequence of note names - ignored
'8n'// Time interval between each note
);
console.log(sequence.get())
let seq_knobs = []
let fader_spacing = 8
for( let i=0;i<pitches.length;i++){
seq_knobs.push(seqgui.Fader({
label: (i).toString(),showLabel:0,
callback: function(x){
pitches[i]= scaleToMidi(Math.floor(x))
},
min:0,max:12, value:Math.random()*12,
size: 1, x: 10 + i*fader_spacing, y: 60
}))
}
let isTransportRunning = false
let toggleButton = seqgui.Toggle({
label:'On/Off',
callback:
function toggleTransport() {
if (isTransportRunning) {
Tone.Transport.stop();
} else {
Tone.Transport.start();
}
isTransportRunning = !isTransportRunning;
},
x: 50, y:20
})
let tempoKnob = seqgui.Knob({
label: 'Tempo',
callback: function(x){
Tone.Transport.bpm.value = x;
},
x: 80, y: 20,
min:30, max:250, curve: 1
})
let lengthKnob = seqgui.Knob({
label: 'Note Length',
callback: function(x){ampEnvelope.decay = x
ampEnvelope.release = x},
min: 0.1, max: 1, curve: 2, size: 1,
x: 30, y: 20
})
let transposeAdd = seqgui.Button({
label: 'Transpose +',
callback: function() {
transpose += 1
},
x: 80, y: 50
})
let transposeSubtract = seqgui.Button({
label: 'Transpose -',
callback: function subtractnote(){
transpose -= 1
},
x: 10, y: 50
})
let booster = seqgui.Toggle({
label: 'Boost',
callback: function boosted(){
if (isBoost) {
sequence.playbackRate = 2;
} else {
sequence.playbackRate = 1;
}
isBoost = !isBoost;
},
x: 90, y: 70
})
let rand = seqgui.Toggle({
label: 'Random',
callback: function(x){
isRandom = x
},
x: 10, y:20
})
isRandom = false
//start sequence
sequence.start()
//sequence.stop()
let startEnable = 0
//this start button exists just to enable audio
//there are probably other ways to call Tone.start(). . . .
startButton.addEventListener('click', () => {
if (startEnable == 0) {
// Start the hihat if it's not already playing
Tone.start()
console.log('start');
startEnable = 1
} else {
console.log('stop');
startEnable = 0
}
});