-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.html
712 lines (607 loc) · 25.5 KB
/
index.html
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
let AudioContext, audioCtx
let frequencies = {
'C0': 16.35,
'Db0': 17.32,
'D0': 18.35,
'Eb0': 19.45,
'E0': 20.60,
'F0': 21.83,
'Gb0': 23.12,
'G0': 24.50,
'Ab0': 25.96,
'A0': 27.50,
'Bb0': 29.14,
'B0': 30.87,
'C1': 32.70,
'Db1': 34.65,
'D1': 36.71,
'Eb1': 38.89,
'E1': 41.20,
'F1': 43.65,
'Gb1': 46.25,
'G1': 49.00,
'Ab1': 51.91,
'A1': 55.00,
'Bb1': 58.27,
'B1': 61.74,
'C2': 65.41,
'Db2': 69.30,
'D2': 73.42,
'Eb2': 77.78,
'E2': 82.41,
'F2': 87.31,
'Gb2': 92.50,
'G2': 98.00,
'Ab2': 103.83,
'A2': 110.00,
'Bb2': 116.54,
'B2': 123.47,
'C3': 130.81,
'Db3': 138.59,
'D3': 146.83,
'Eb3': 155.56,
'E3': 164.81,
'F3': 174.61,
'Gb3': 185.00,
'G3': 196.00,
'Ab3': 207.65,
'A3': 220.00,
'Bb3': 233.08,
'B3': 246.94,
'C4': 261.63,
'Db4': 277.18,
'D4': 293.66,
'Eb4': 311.13,
'E4': 329.63,
'F4': 349.23,
'Gb4': 369.99,
'G4': 392.00,
'Ab4': 415.30,
'A4': 440.00,
'Bb4': 466.16,
'B4': 493.88,
'C5': 523.25,
'Db5': 554.37,
'D5': 587.33,
'Eb5': 622.25,
'E5': 659.25,
'F5': 698.46,
'Gb5': 739.99,
'G5': 783.99,
'Ab5': 830.61,
'A5': 880.00,
'Bb5': 932.33,
'B5': 987.77,
'C6': 1046.50,
'Db6': 1108.73,
'D6': 1174.66,
'Eb6': 1244.51,
'E6': 1318.51,
'F6': 1396.91,
'Gb6': 1479.98,
'G6': 1567.98,
'Ab6': 1661.22,
'A6': 1760.00,
'Bb6': 1864.66,
'B6': 1975.53,
'C7': 2093.00,
'Db7': 2217.46,
'D7': 2349.32,
'Eb7': 2489.02,
'E7': 2637.02,
'F7': 2793.83,
'Gb7': 2959.96,
'G7': 3135.96,
'Ab7': 3322.44,
'A7': 3520.00,
'Bb7': 3729.31,
'B7': 3951.07,
'C8': 4186.01,
'Db8': 4434.92,
'D8': 4698.63,
'Eb8': 4978.03,
'E8': 5274.04,
'F8': 5587.65,
'F#8/Gb8': 5919.91,
'G8': 6271.93,
'Ab8': 6644.88,
'A8': 7040.00,
'Bb8': 7458.62,
'B8': 7902.13,
}
let scales = {
'minor-pentatonic': ['C', 'Eb', 'F', 'G', 'Bb'],
'major-pentatonic': ['C', 'D', 'E', 'G', 'A'],
'suspended': ['C', 'D', 'F', 'G', 'Bb'],
'blues-minor': ['C', 'Eb', 'F', 'Ab', 'Bb'],
'blues-major': ['C', 'D', 'F', 'G', 'A']
}
let currentScale = 'minor-pentatonic'
let notes = scales[currentScale]
let octaves = [1, 2, 3, 4, 5, 6]
let beats = [16, 8, 4, 2, 1]
let volumes = [0.1, 0.2, 0.4, 0.6, 0.8, 1]
let loopLength = 8
let noteVariation = 2
let chanceForNewLoop = 0.1
let tracks = []
let randomInstrument = () => {
let waves = ['triangle', 'saw', 'square', 'sine']
let waveId = Math.floor(Math.random() * waves.length)
let wave = waves[waveId]
let attack, decay
if (Math.random() >= 0.5) {
attack = Math.random()
decay = Math.min(1 - attack, Math.random())
} else {
decay = Math.random()
attack = Math.min(1 - decay, Math.random())
}
let sustain = Math.random()
let release = Math.random() * 2
return { wave, attack, decay, sustain, release }
}
let randomLoop = () => {
let loop = []
let noteId = Math.floor(Math.random() * notes.length)
for (i = 0; i < loopLength; i++) {
noteId += Math.floor(Math.random() * noteVariation * 2 - noteVariation)
if (noteId < 0) noteId = 0
if (noteId >= notes.length) noteId = notes.length - 1
let note = notes[noteId]
loop.push(note)
}
return loop
}
let randomTrack = () => {
let playing = true
let instrument = randomInstrument()
let octave = octaves[Math.floor(Math.random() * octaves.length)]
let beat = beats[Math.floor(Math.random() * beats.length)]
let volume = volumes[Math.floor(Math.random() * volumes.length)]
let loop = randomLoop()
return { playing, instrument, octave, beat, volume, loop }
}
let playNote = (freq, instrument, noteLength, volume) => {
let t = audioCtx.currentTime
let osc = audioCtx.createOscillator()
osc.type = instrument.wave
osc.frequency.value = freq
let env = audioCtx.createGain()
env.connect(audioCtx.destination)
env.gain.cancelScheduledValues(t)
env.gain.setValueAtTime(0, t)
// attack
let attack = noteLength * instrument.attack
env.gain.linearRampToValueAtTime(volume, t + attack)
// decay
let decay = noteLength * instrument.decay
env.gain.linearRampToValueAtTime(instrument.sustain * volume, t + attack + decay)
// release
let release = instrument.release
env.gain.linearRampToValueAtTime(0, t + noteLength)
osc.connect(env)
osc.start()
osc.stop(t + noteLength + release)
}
let playTracks = () => {
let noteIds = tracks.map(() => 0)
let playNextNote = () => {
tracks.forEach((track, i) => {
if (noteIds[i] % track.beat === 0) {
let loopId = Math.floor(noteIds[i] / track.beat)
if (loopId >= loopLength) {
loopId = 0
noteIds[i] = 0
if (Math.random() < chanceForNewLoop) {
track.loop = randomLoop()
}
}
if (track.playing) {
let note = track.loop[loopId]
let freq = frequencies[note + track.octave]
let volume = track.volume / track.octave
playNote(freq, track.instrument, track.beat * 0.125, volume)
}
}
noteIds[i]++
})
setTimeout(playNextNote, 1000 / 8)
}
playNextNote()
}
let renderInstrumentSVG = (instrument) => {
let attackX = instrument.attack * 100
let attackPt = `${attackX},0`
let decayX = attackX + instrument.decay * 100
let sustainY = 100 - instrument.sustain * 100
let decayPt = `${decayX},${sustainY}`
let sustainPt = `100,${sustainY}`
let releaseX = 100 + instrument.release * 100
let releasePt = `${releaseX},100`
let color = ''
if (instrument.wave === 'triangle') color = '#ff7f50'
if (instrument.wave === 'square') color = '#ffd700'
if (instrument.wave === 'saw') color = '#00fa9a'
if (instrument.wave === 'sine') color = '#40e0d0'
return `
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<polyline points="0,100 ${attackPt} ${decayPt} ${sustainPt} ${releasePt}" stroke="none" fill="${color}" />
</svg>
`
}
let renderDropdown = (selected, values, labels) => {
let select = document.createElement('select')
values.forEach((value, i) => {
let option = document.createElement('option')
option.innerText = labels ? labels[i] : value
option.value = value
if (value === selected) option.selected = 'selected'
select.appendChild(option)
})
return select
}
let renderModifier = (label, value, valueList, onChange) => {
let container = document.createElement('div')
container.className = 'modifier'
let index = valueList.findIndex(x => x === value)
let labelEl = document.createElement('div')
labelEl.innerText = label
container.appendChild(labelEl)
let valueContainer = document.createElement('div')
container.appendChild(valueContainer)
let prevButton = document.createElement('button')
prevButton.innerText = '↙'
prevButton.addEventListener('click', () => {
index--
if (index < 0) index = 0
onChange(valueList[index])
valueEl.innerText = index + 1
updateShareUrl()
})
valueContainer.appendChild(prevButton)
let valueEl = document.createElement('div')
valueEl.innerHTML = index + 1
valueContainer.appendChild(valueEl)
let nextButton = document.createElement('button')
nextButton.innerText = '↗'
nextButton.addEventListener('click', () => {
index++
if (index >= valueList.length) index = valueList.length - 1
onChange(valueList[index])
valueEl.innerText = index + 1
updateShareUrl()
})
valueContainer.appendChild(nextButton)
return container
}
let renderTrack = (track, trackId) => {
let container = document.createElement('div')
container.className = 'track'
let playCheckbox = document.createElement('input')
playCheckbox.type = 'checkbox'
playCheckbox.checked = track.playing
playCheckbox.addEventListener('change', e => {
track.playing = e.target.checked
updateShareUrl()
})
container.appendChild(playCheckbox)
let instrumentButton = document.createElement('button')
instrumentButton.className = 'instrument-button'
instrumentButton.innerHTML = renderInstrumentSVG(track.instrument)
instrumentButton.addEventListener('click', () => {
track.instrument = randomInstrument()
instrumentButton.innerHTML = renderInstrumentSVG(track.instrument)
updateShareUrl()
})
container.appendChild(instrumentButton)
let speedModifier = renderModifier('speed', track.beat, beats, x => track.beat = x)
container.appendChild(speedModifier)
let volumeModifier = renderModifier('volume', track.volume, volumes, x => track.volume = x)
container.appendChild(volumeModifier)
let octaveModifier = renderModifier('pitch', track.octave, octaves, x => track.octave = x)
container.appendChild(octaveModifier)
let randomButton = document.createElement('button')
randomButton.className = 'random-button'
randomButton.innerText = '⟳'
randomButton.addEventListener('click', () => {
tracks[trackId] = randomTrack()
renderTrackList()
updateShareUrl()
})
container.appendChild(randomButton)
return container
}
let renderTrackList = () => {
let trackList = document.getElementById('tracks')
trackList.innerHTML = ''
tracks.forEach((track, trackId) => {
let trackEl = renderTrack(track, trackId)
trackList.appendChild(trackEl)
})
}
let updateScale = (newScale) => {
if (typeof newScale !== 'string') {
if (currentScale === 'minor-pentatonic') currentScale = 'major-pentatonic'
else if (currentScale === 'major-pentatonic') currentScale = 'suspended'
else if (currentScale === 'suspended') currentScale = 'blues-minor'
else if (currentScale === 'blues-minor') currentScale = 'blues-major'
else if (currentScale === 'blues-major') currentScale = 'minor-pentatonic'
}
let notes = scales[currentScale]
tracks.forEach(track => {
track.loop = randomLoop()
})
document.getElementById('scale-button').innerText = currentScale.replace('-', ' ') + ' scale'
updateShareUrl()
}
let updateShareUrl = () => {
let shareLink = document.getElementById('share')
shareLink.href = getUrlData()
}
let getUrlData = () => {
// let baseUrlEnd = window.location.href.indexOf('?')
// let url = window.location.href.slice(0, baseUrlEnd)
url = 'https://zenzoa.github.io/beepster/?beeps='
url += 'S' + Object.keys(scales).indexOf(currentScale) + '-'
url += tracks.map(track => {
let data = 'T'
data += 'p' + (track.playing ? 1 : 0) + ','
data += 'o' + track.octave + ','
data += 'b' + track.beat + ','
data += 'v' + Math.floor(track.volume * 10) + ','
data += 'w' + track.instrument.wave.slice(0, 2) + ','
data += 'a' + Math.floor(track.instrument.attack * 10000) + ','
data += 'd' + Math.floor(track.instrument.decay * 10000) + ','
data += 's' + Math.floor(track.instrument.sustain * 10000) + ','
data += 'r' + Math.floor(track.instrument.release * 10000)
return data
}).join('-')
return url
}
let loadUrlData = (url) => {
dataStarts = url.indexOf('?beeps=')
if (dataStarts < 0) return
url = url.slice(dataStarts + 7)
let urlParts = url.split('-')
tracks = []
urlParts.forEach(data => {
if (data[0] === 'S') {
let scaleList = Object.keys(scales)
let scaleIndex = parseInt(data.slice(1))
if (scaleIndex >= 0 && scaleIndex < scaleList.length) {
currentScale = scaleList[scaleIndex]
updateScale(currentScale)
}
return
}
let track = randomTrack()
data = data.slice(1)
let parts = data.split(',')
parts.forEach(part => {
let partType = part[0]
part = part.slice(1)
if (partType === 'p') {
track.playing = part !== '0'
}
else if (partType === 'o') {
let octave = parseInt(part)
if (!isNaN(octave) && octaves.includes(octave)) {
track.octave = octave
}
}
else if (partType === 'b') {
let beat = parseInt(part)
if (!isNaN(beat) && beats.includes(beat)) {
track.beat = beat
}
}
else if (partType === 'v') {
let volume = parseInt(part) / 10
if (!isNaN(volume) && volumes.includes(volume)) {
track.volume = volume
}
}
else if (partType === 'w') {
if (part === 'tr') track.instrument.wave = 'triangle'
else if (part === 'sa') track.instrument.wave = 'saw'
else if (part === 'si') track.instrument.wave = 'sine'
else if (part === 'sq') track.instrument.wave = 'square'
}
else if (partType === 'a') {
let attack = parseInt(part) / 10000
if (!isNaN(attack) && attack >= 0 && attack < 1) {
track.instrument.attack = attack
}
}
else if (partType === 'd') {
let decay = parseInt(part) / 10000
if (!isNaN(decay) && decay >= 0 && decay < 1) {
track.instrument.decay = decay
}
}
else if (partType === 's') {
let sustain = parseInt(part) / 10000
if (!isNaN(sustain) && sustain >= 0 && sustain < 1) {
track.instrument.sustain = sustain
}
}
else if (partType === 'r') {
let release = parseInt(part) / 10000
if (!isNaN(release) && release >= 0 && release < 2) {
track.instrument.release = release
}
}
})
tracks.push(track)
})
}
let initialize = () => {
AudioContext = window.AudioContext || window.webkitAudioContext
audioCtx = new AudioContext()
tracks = Array(4).fill(0).map(() => randomTrack())
let scaleButton = document.getElementById('scale-button')
scaleButton.innerText = 'minor pentatonic scale'
scaleButton.addEventListener('click', updateScale)
loadUrlData(window.location.href)
updateShareUrl()
renderTrackList()
playTracks()
}
window.onload = () => {
let startButton = document.getElementById('start-button')
startButton.addEventListener('click', () => {
initialize()
document.getElementById('start-button').className = 'hidden'
document.getElementById('tracks').className = ''
document.getElementById('scale-button').className = ''
})
}
</script>
<style>
body {
margin: 0;
padding: 20px 30px;
font-family: sans-serif;
font-size: 16px;
color: #444;
text-align: center;
}
h1 {
margin: 0;
padding: 0;
font-size: xx-large;
}
main {
margin: 20px auto;
max-width: 700px;
}
.track {
display: flex;
flex-direction: row;
align-items: center;
margin: 10px 0;
background: #eee;
border-radius: 6px;
font-size: small;
}
.track + .track {
margin-top: 10px;
}
.track > * {
margin: 10px;
}
.modifier {
flex-grow: 1;
}
.modifier > div {
display: flex;
flex-direction: row;
justify-content: center;
}
.modifier > div:first-child {
height: 20px;
}
.modifier > div > button {
flex-grow: 1;
height: 30px;
z-index: 1;
}
.modifier > div > button:first-child {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.modifier > div > button:last-child {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.modifier > div > div {
display: flex;
align-items: center;
justify-content: center;
width: 30px;
height: 30px;
background: #888;
color: white;
}
input[type="checkbox"] {
margin-left: 20px;
}
button {
margin: 0;
padding: 5px 10px;
border: none;
border-radius: 3px;
background: #444;
font-family: inherit;
font-size: inherit;
color: white;
cursor: pointer;
}
button:hover, button:focus {
box-shadow: 0 0 0 3px hotpink;
}
.instrument-button {
height: 50px;
}
.instrument-button > svg {
height: 80%;
margin: 5px;
}
.random-button {
width: 50px;
height: 50px;
background: none;
color: hotpink;
font-size: larger;
font-weight: bold;
}
main > button {
width: 100%;
height: 40px;
padding: 0 20px;
}
footer {
font-size: small;
}
a, a:active, a:visited {
color: inherit;
text-decoration: underline;
}
.hidden {
display: none;
}
@media (max-width: 650px) {
.modifier {
flex-basis: 100%;
}
.track {
flex-wrap: wrap;
justify-content: center;
}
.instrument-button {
flex-grow: 1;
}
}
</style>
</head>
<body>
<header>
<h1>⧞ beepster</h1>
</header>
<main>
<button id="start-button">let's get started!</button>
<div id="tracks" class="hidden"></div>
<button id="scale-button" class="hidden"></button>
</main>
<footer>
<a href="https://zenzoa.com" target="_blank">sg 2019</a> |
<a id="share" href="" target="_blank">share these beeps</a>
</footer>
</body>
</html>