-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
336 lines (266 loc) · 7.82 KB
/
Copy pathscript.js
File metadata and controls
336 lines (266 loc) · 7.82 KB
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
let mainCanvas;
const playButton = document.getElementById("play-button");
const vibratoSlider = document.getElementById("vibrato-slider");
let particles = [];
// Set initial BPM
Tone.Transport.bpm.value = 160;
let isAudioPlaying = false;
let currentNote = -1;
let previousOctave;
let randomNum;
let FMinor7 = ["F4", "Ab4", "C5", "Eb5"];
let Db7 = ["Db4", "F4", "Ab4", "C5"];
let chordProgression = [
["0:0:0", FMinor7],
["0:1:2", FMinor7],
["0:2:0", FMinor7],
["0:3:0", FMinor7],
["1:0:0", FMinor7],
["1:1:2", FMinor7],
["1:2:0", FMinor7],
["1:3:0", FMinor7],
["2:0:0", Db7],
["2:1:2", Db7],
["2:2:0", Db7],
["2:3:0", Db7],
["3:0:0", Db7],
["3:1:2", Db7],
["3:2:0", Db7],
["3:3:0", Db7],
]
// let melody = ["F3", "Ab3", "C4", "G3", "Bb3", "C4", "Eb4", "C4", "Ab4"];
let melody = ["F", "Ab", "C", "G", "Bb", "C", "Eb", "Db", "Ab"];
let fMinorScale = ["F", "G", "Ab", "Bb", "C", "Db", "Eb"];
let octave = 3;
let detuner = 0;
let bassline = ["F2", null, null, "F2", null, null, "F2", "C2", "F2", null, null, "F2", null, null, "F2", "C2", "Db2", null, null, "Db2", null, null, "Db2", "Ab2", "Db2", null, null, "Db2", null, null, "Db2", "Ab2"];
let snareLine = [null, null, 1, null, null, null, 1, 1, null, 1, null, 1, 1, null, 1, null]; // Using 1 for hits, null for rests
let synth = new Tone.PolySynth({
envelope: {
attack: 0.5,
decay: 0.2,
sustain: 0.7,
release: 0.1
}
}).toDestination();
let melodicSynth = new Tone.Synth({
envelope: {
attack: 0.02,
decay: 0.5,
sustain: 0.8,
release: 1.0
},
portamento: 0.1
}).toDestination();
let kick = new Tone.MembraneSynth().toDestination();
let bassSynth = new Tone.FMSynth({
envelope:{
attack: 0.02,
decay: 0.5,
sustain: 0.5,
release: 0.2
}
}).toDestination();
let snare = new Tone.NoiseSynth({
envelope: {
attack: 0.005,
decay: 0.2,
sustain: 0,
release: 0.2
}
}).toDestination();
const filter = new Tone.Filter(500, "lowpass").toDestination();
const highpass = new Tone.Filter(1200, "highpass").toDestination();
kick.connect(filter);
snare.connect(highpass);
const part = new Tone.Part((time, chord) => {
synth.triggerAttack(chord, time);
// Release after two measures for long attack
synth.triggerRelease(chord, time + Tone.Time("8n"));
// synth.triggerAttackRelease(chord, "8n", time);
}, chordProgression).start();
part.loop = true;
part.loopStart = "0:0:0"; // Start from the beginning
part.loopEnd = "4:0:0"; // Loop every 4 measures
const kickPart = new Tone.Loop((time) => {
kick.triggerAttackRelease("C2", "8n", time);
}, "4n").start();
const bassPart = new Tone.Sequence((time, note) => {
bassSynth.triggerAttackRelease(note, "8n", time);
}, bassline, "8n").start();
const snarePart = new Tone.Sequence((time, hit) => {
if (hit) {
snare.triggerAttackRelease("8n", time); // For noise synth, we only need duration
}
}, snareLine, "8n").start();
const vibrato = new Tone.Vibrato({
frequency: 0.1,
depth: 0.1,
}).toDestination();
const tremolo = new Tone.Tremolo({
frequency: 5,
depth: 0.1
}).toDestination();
// Start with melodicSynth routed to vibrato (no tremolo active)
melodicSynth.connect(vibrato);
// Tremolo toggle (spacebar hold)
let tremoloHeld = false;
function applyTremoloRouting() {
// Ensure context is started
Tone.start();
tremolo.start();
// Route main synths through the tremolo
// try { synth.disconnect(); } catch (e) {}
// synth.connect(tremolo);
// try { bassSynth.disconnect(); } catch (e) {}
// bassSynth.connect(tremolo);
// Route melodic synth through tremolo as well
try { melodicSynth.disconnect(); } catch (e) {}
melodicSynth.connect(tremolo);
}
function removeTremoloRouting() {
tremolo.stop();
// Restore original routings
// try { synth.disconnect(); } catch (e) {}
// synth.toDestination();
// try { bassSynth.disconnect(); } catch (e) {}
// bassSynth.toDestination();
try { melodicSynth.disconnect(); } catch (e) {}
melodicSynth.connect(vibrato);
}
window.addEventListener('keydown', (e) => {
if (e.code === 'Space' && !e.repeat) {
e.preventDefault();
if (!tremoloHeld) {
tremoloHeld = true;
applyTremoloRouting();
}
}
});
window.addEventListener('keyup', (e) => {
if (e.code === 'Space') {
e.preventDefault();
if (tremoloHeld) {
tremoloHeld = false;
removeTremoloRouting();
}
}
});
// synth.connect(filter);
// synth.chain(vibrato, freeverb);
// bassSynth.connect(filter);
playButton.addEventListener("click", () => {
if(!isAudioPlaying){
isAudioPlaying = true;
console.log("start audio");
playButton.innerText = "Stop";
synth.volume.value = -16;
bassSynth.volume.value = 0;
snare.volume.value = -20;
// Make sure our part is properly started
part.stop(); // Stop first to ensure clean state
kickPart.stop();
bassPart.stop();
snarePart.stop();
part.start(0); // Start from beginning
kickPart.start(0);
bassPart.start(0);
snarePart.start(0);
// Start the transport
Tone.Transport.start();
} else {
isAudioPlaying = false;
playButton.innerText = "Play";
console.log("stop audio");
Tone.Transport.stop();
part.stop();
kickPart.stop();
bassPart.stop();
snarePart.stop();
// Release any hanging notes
synth.releaseAll();
kick.releaseAll();
bassSynth.releaseAll();
snare.releaseAll();
}
})
window.addEventListener("mousemove", (event) => {
})
function setup(){
mainCanvas = createCanvas(600, 400);
background(200);
}
function draw(){
cursor("crosshair");
background(200);
let colorVal = map(mouseX, 0, width, 0, 255);
let colorValY = map(mouseY, 0, height, 0, 255);
background(colorVal, 100, colorValY);
noStroke();
if(mouseIsPressed === true){
ellipse(mouseX, mouseY, 30, 30);
}
if(mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height){
if(mouseY <= 400 && mouseY >300){
octave = 2;
} else if(mouseY < 300 && mouseY > 200){
octave = 3;
} else if (mouseY < 200 && mouseY > 100) {
octave = 4;
} else if (mouseY < 100 && mouseY > 0){
octave = 5;
}
}
}
vibratoSlider.addEventListener("input", (event) => {
const vibratoValue = parseFloat(event.target.value);
vibrato.frequency.value = vibratoValue;
vibrato.depth.value = vibratoValue;
});
function mousePressed(){
randomNum = Math.floor(Math.random() * fMinorScale.length);
melodicSynth.triggerAttack(`${fMinorScale[randomNum]}${octave}`);
previousOctave = octave;
// let newTuner = map(mouseX, 0, width, 0.1, 100);
// vibrato.frequency.value = newTuner;
// vibrato.depth.value = newTuner;
}
function mouseDragged(){
if (!randomNum || !previousOctave) return
if(octave != previousOctave){
melodicSynth.triggerAttack(`${fMinorScale[randomNum]}${octave}`);
previousOctave = octave;
}
}
function mouseReleased(){
melodicSynth.triggerRelease();
// vibrato.frequency.value = 0.1;
// vibrato.depth.value = 0.1;
}
function keyPressed(){
if(key === "w"){
const vibratoValue = vibrato.frequency.value + 0.1;
vibrato.frequency.value = vibratoValue;
vibrato.depth.value = vibratoValue;
}
if(key === "s"){
const vibratoValue = vibrato.frequency.value - 0.1;
vibrato.frequency.value = vibratoValue;
vibrato.depth.value = vibratoValue;
}
if(key === "r"){
const vibratoValue = 0.1;
vibrato.frequency.value = vibratoValue;
vibrato.depth.value = vibratoValue;
console.log("reset");
}
if(key === " "){
console.log("space pressed");
const tremoloValue = 10;
tremolo.frequency.value = tremoloValue;
tremolo.depth.value = 0.5;
} else {
tremolo.frequency.value = 5;
tremolo.depth.value = 0.1;
}
}