-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (117 loc) · 2.68 KB
/
index.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
var regl = require('regl')()
var analyse = require('web-audio-analyser')
var soundcloudBadge = require('soundcloud-badge')
var camera = require('regl-camera')(regl, {
center: [0, 0, 0],
theta: 0.717,
phi: 0.717,
distance: 3,
damping: 0
})
var audio = document.createElement('audio')
audio.crossOrigin = 'anonymous'
audio.controls = true
audio.loop = true
var analyzer = analyse(audio, { audible: true, stereo: false })
var drawWaveform = regl({
vert: `
precision mediump float;
attribute vec3 xyz;
uniform mat4 projection, view;
void main () {
gl_Position = projection * view * vec4(xyz,1);
}
`,
frag: `
void main () {
gl_FragColor = vec4(0,0.3333,1,1);
}
`,
attributes: {
xyz: () => Array.from(analyzer.waveform()).map((freq, i) => {
return [0, (freq - 128) / 128, i / 1023 * 2 - 1]
})
},
primitive: 'line strip',
count: 1024
})
var drawFrequency = regl({
vert: `
precision mediump float;
attribute vec3 xyz;
uniform mat4 projection, view;
void main () {
gl_Position = projection * view * vec4(xyz,1);
}
`,
frag: `
void main () {
gl_FragColor = vec4(0,1,0,1);
}
`,
attributes: {
xyz: () => Array.from(analyzer.frequencies()).slice(0, 256).map((freq, i) => {
return [(freq) / 512, 0, i / 256 * 2 - 1]
})
},
primitive: 'line strip',
count: 256
})
regl.frame(() => {
regl.clear({ color: [0, 0, 0, 1] })
camera(() => {
drawWaveform()
drawFrequency()
})
})
var CLIENT_ID = 'your_client_id'
var html = require('choo/html')
var choo = require('choo')
var app = choo()
app.use(playStore)
app.route('*', mainView)
var el = app.start()
el.appendChild(audio)
document.body.appendChild(el)
function mainView (state, emit) {
return html`
<div class="controls" onload=${onLoad}>
<form onsubmit=${onSubmit}>
<input type="text" placeholder="SoundCloud URL">
</form>
</div>
`
function onSubmit (e) {
const input = e.target.children[0]
emit('play', input.value)
input.value = ''
e.preventDefault()
}
function onLoad () {
emit('play', 'https://soundcloud.com/aquilo/human')
}
}
function playStore (state, emitter) {
state.trackDiv = undefined
emitter.on('play', function (uri) {
soundcloudBadge({
client_id: CLIENT_ID,
song: uri,
dark: false,
getFonts: false
},
function (err, src, data, div) {
if (err) throw err
var wasPlaying = !audio.paused
audio.src = src
audio.load()
if (state.trackDiv) {
state.trackDiv.remove()
}
if (wasPlaying) {
audio.play()
}
state.trackDiv = div
})
})
}