-
Notifications
You must be signed in to change notification settings - Fork 2
/
blah.js
513 lines (486 loc) · 12.3 KB
/
blah.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import React from "react";
import p5 from "p5";
import "p5/lib/addons/p5.sound";
import { css } from "glamor";
import * as u from "./utils";
import songUrl from "../assets/zedd-stay.m4a";
// import songUrl from "../assets/nocturne-15.m4a";
// some FFT documentation here:
// https://p5js.org/reference/#/p5.FFT
// https://p5js.org/reference/#/p5.FFT/getEnergy
css.global("html, body", {
padding: 0,
margin: 0,
boxSizing: "border-box",
backgroundColor: "rgb(51, 51, 51)",
overflow: "hidden"
});
const styles = {
layout: css({
position: "absolute",
top: 0,
bottom: 0,
left: 0,
right: 0,
display: "flex",
flexDirection: "row"
}),
content: css({
flex: "1"
}),
toolbar: css({
position: "absolute",
left: "100%",
top: 0,
bottom: 0,
width: 150,
backgroundColor: "rgb(51, 51, 51)",
display: "flex",
flexDirection: "column",
justifyContent: "center"
}),
show: css({
transform: "translateX(-150px)",
transition: "transform 0.2s ease-out"
}),
hide: css({
transform: "translateX(0px)",
transition: "transform 0.2s ease-in"
}),
button: css({
border: "none",
outline: "none",
background: "transparent",
color: "white",
cursor: "pointer",
padding: "4px 8px",
marginTop: 8,
textAlign: "center"
}),
link: css({
fontFamily: "sans-serif",
fontSize: 12,
textDecoration: "none",
fontWeight: "normal"
}),
dropping: css({
position: "absolute",
top: 0,
bottom: 0,
left: 0,
right: 0,
backgroundColor: "rgba(255,255,255,0.4)",
color: "white",
textTransform: "uppercase",
fontSize: "32",
fontWeight: "bold",
fontFamily: "sans-serif",
display: "flex",
justifyContent: "center",
alignItems: "center"
})
};
const getFirstFile = event => {
const dt = event.dataTransfer;
if (dt.items) {
// Use DataTransferItemList interface to access the file(s)
for (let i = 0; i < dt.items.length; i++) {
if (dt.items[i].kind == "file") {
const f = dt.items[i].getAsFile();
if (/audio/.test(f.type)) {
return f;
}
}
}
} else {
// Use DataTransfer interface to access the file(s)
for (let i = 0; i < dt.files.length; i++) {
const f = dt.files[i];
if (/audio/.test(f.type)) {
return f;
}
}
}
};
export default class Sketch extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
playing: true,
sharpness: 3,
gain: 1,
hue: 240,
sweep: -10,
radius: 0.8,
grid: false,
opacity: 0.1,
scrubbing: undefined,
moving: false,
dropping: false
};
this.scrubbers = {
sharpness: [1, 10],
gain: [0.6, 3],
hue: [0, 360],
sweep: [-40, 40],
radius: [0, 1],
opacity: [0, 1]
};
this.params = ["grid"];
if (window.location.search !== "") {
const saved = window.location.search
.slice(1)
.split("&")
.map(str => str.split("="))
.reduce(
(obj, [name, str]) => {
obj[name] = JSON.parse(str);
return obj;
},
{}
);
this.state = { ...this.state, ...saved };
}
}
componentWillUpdate(nextProps, nextState) {
const params = Object.keys(this.scrubbers)
.map(name => `${name}=${encodeURIComponent(nextState[name].toFixed(2))}`)
.concat(
this.params.map(
name => `${name}=${encodeURIComponent(nextState[name])}`
)
)
.join("&");
window.history.pushState({}, "circle", `?${params}`);
}
// save a reference to the root node
rootRef = node => {
this.root = node;
};
play = () => {
this.setState({ playing: true });
};
pause = () => {
this.setState({ playing: false });
};
renderPausePlay() {
if (this.state.playing) {
return (
<button className={styles.button} onClick={this.pause}>pause</button>
);
} else {
return (
<button className={styles.button} onClick={this.play}>play</button>
);
}
}
renderScrubber = label => {
const setScubber = () => this.setState({ scrubbing: label });
if (this.state.scrubbing === label) {
return (
<button className={styles.button} key={label}>
{label} {this.state[label].toFixed(2)}
</button>
);
} else {
return (
<button className={styles.button} key={label} onClick={setScubber}>
{label} {this.state[label].toFixed(2)}
</button>
);
}
};
renderGridBool() {
const turnOn = () => this.setState({ grid: true });
const turnOff = () => this.setState({ grid: false });
if (this.state.grid) {
return (
<button className={styles.button} onClick={turnOff}>grid off</button>
);
} else {
return (
<button className={styles.button} onClick={turnOn}>grid on</button>
);
}
}
stopScubbing = () => {
this.setState({ scrubbing: undefined });
};
onDrop = event => {
event.preventDefault();
this.upload = getFirstFile(event);
this.reload();
this.setState({
dropping: false,
uploading: false
});
};
onDragOver = event => {
// prevent default behavior
event.preventDefault();
this.setState({
dropping: true
});
};
renderDropZone() {
if (this.state.dropping || this.state.uploading) {
return (
<div className={styles.dropping}>
Drop a music file
</div>
);
} else {
return false;
}
}
setUploadingTrue = () => {
this.setState({
uploading: true
});
};
setUploadingFalse = () => {
this.setState({
uploading: false,
dropping: false
});
};
renderUpload() {
if (this.state.uploading || this.state.dropping) {
return (
<button onClick={this.setUploadingFalse} className={styles.button}>
cancel
</button>
);
} else {
return (
<button onClick={this.setUploadingTrue} className={styles.button}>
upload
</button>
);
}
}
render() {
const style = {
cursor: this.state.scrubbing
? "crosshair"
: this.state.moving ? "default" : "none"
};
return (
<div
style={style}
onDrop={this.onDrop}
onDragOver={this.onDragOver}
className={styles.layout}
>
<div
className={styles.content}
onClick={this.stopScubbing}
ref={this.rootRef}
/>
{this.renderDropZone()}
<div
className={css(
styles.toolbar,
this.state.moving || this.state.scrubbing
? styles.show
: styles.hide
)}
>
{this.renderPausePlay()}
{this.renderUpload()}
{this.renderGridBool()}
{Object.keys(this.scrubbers).map(this.renderScrubber)}
<a
href="https://github.com/ccorcos/circle"
target="_blank"
className={css(styles.link, styles.button)}
>
source code
</a>
</div>
</div>
);
}
componentDidMount() {
this.deriveSizes();
new p5(this.sketch, this.root);
window.onresize = () => {
this.deriveSizes();
this.canvas.resize(this.width, this.height);
};
document.body.addEventListener("mouseleave", () => {
this.setState({
dropping: false
});
});
}
reload() {
this.p.remove();
new p5(this.sketch, this.root);
}
deriveSizes() {
// size of the canvas
this.height = window.innerHeight;
this.width = window.innerWidth;
// edge of a square in the middle
this.edge = Math.min(this.height, this.width);
this.center = {};
this.center.x = this.width / 2;
this.center.y = this.height / 2;
this.radius = this.edge / 3;
}
startMoving() {
window.clearTimeout(this.movingTimerId);
this.movingTimerId = undefined;
if (!this.state.moving) {
this.setState({ moving: true });
}
}
stopMoving() {
if (this.movingTimerId === undefined) {
this.movingTimerId = window.setTimeout(
() => {
if (this.state.moving) {
this.setState({ moving: false });
}
},
2500
);
}
}
sketch = p => {
this.p = p;
// A0 is 27.5Hz which is below C1
const fmin = 27.5;
// 7 octaves on a piano, lets use 8
const octaves = 8;
// steps per octave to sample
const steps = 24;
// generate the frequencies for each octave band
const bands = u.range(0, octaves).map(o => {
return u.range(0, steps).map(s => {
// this calculation is related to how you find the frequency of a note on a piano: f = 2 ^ (n / 12)
return Math.pow(2, o + s / steps) * fmin;
});
});
p.preload = () => {
this.song = p.loadSound(this.upload || songUrl);
};
p.setup = () => {
this.canvas = p.createCanvas(this.width, this.height);
p.noFill();
// using microphone
// this.mic = new p5.AudioIn();
// this.mic.start();
// this.fft = new p5.FFT();
// this.fft.setInput(this.mic);
// using a song file
this.song.setVolume(1.0);
this.song.playMode("restart");
this.song.play();
this.fft = new p5.FFT();
this.fft.setInput(this.song);
};
// hue offset
let hoffset = 0;
let xy = [0, 0];
const computeMove = ([x1, y1]) => {
const [x2, y2] = xy;
const d = Math.abs(x1 - x2) + Math.abs(y1 - y2);
if (d >= 1 && !this.state.moving) {
this.startMoving();
} else if (d < 1 && this.state.moving) {
this.stopMoving();
}
xy = [x1, y1];
};
p.draw = () => {
computeMove([p.mouseX, p.mouseY]);
if (this.state.playing && !this.song.isPlaying()) {
this.song.play();
}
if (!this.state.playing && !this.song.isPaused()) {
this.song.pause();
}
Object.keys(this.scrubbers).forEach(name => {
const [min, max] = this.scrubbers[name];
if (this.state.scrubbing === name) {
this.setState({
[name]: p.map(p.mouseX, 0, this.width, min, max)
});
}
});
p.background(51);
p.noFill();
p.stroke(255, 255, 255, 255 * 1.0);
p.strokeWeight(0);
// inner and outer radius of the circle
// exponentiate amplitude to give more shape to the circles
const innerRadius = this.edge / 6 * this.state.radius;
// this returns an array of 1024 values but we're going to use getEnergy instead:
this.fft.analyze();
const drawVertex = (freq, i) => {
const radius = Math.pow(
this.fft.getEnergy(freq) / 255,
this.state.sharpness
) *
(this.radius - innerRadius) *
this.state.gain +
innerRadius;
const angle = i / steps * p.TAU;
p.vertex(
this.center.x + radius * Math.cos(angle),
this.center.y + radius * Math.sin(angle)
);
};
// HSL color sweep
let HSPEED = 0;
let hue = u.rotateHue(this.state.hue, hoffset);
bands.forEach(band => {
p.fill(
p.color(`hsla(${Math.round(hue)}, 100%, 50%, ${this.state.opacity})`)
);
p.beginShape();
band.forEach(drawVertex);
drawVertex(band[0], 0);
p.endShape();
hue = u.rotateHue(hue, this.state.sweep);
});
hoffset = u.rotateHue(hoffset, HSPEED);
if (p.keyIsDown(" ".charCodeAt()) || this.state.grid) {
p.stroke(255, 255, 255, 255 * 0.2);
p.strokeWeight(1);
[
"A",
"",
"B",
"C",
"",
"D",
"",
"E",
"F",
"",
"G",
""
].forEach((letter, i) => {
const angle = i / 12 * p.TAU;
p.line(
this.center.x,
this.center.y,
this.center.x + this.radius * Math.cos(angle),
this.center.y + this.radius * Math.sin(angle)
);
p.textSize(14);
p.textAlign(p.CENTER, p.CENTER);
p.fill(255, 255, 255, 255 * 0.2);
p.text(
letter,
this.center.x + this.radius * 1.1 * Math.cos(angle),
this.center.y + this.radius * 1.1 * Math.sin(angle)
);
});
}
};
};
}