-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathaudio-stream-test.html
209 lines (185 loc) · 5.43 KB
/
audio-stream-test.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<title>audio-stream-test</title>
<style>
body {
margin: 0;
font-family: monospace;
}
canvas {
display: block;
width: 100vw;
height: 100vh;
}
#b {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 2;
color: white;
}
a:visited, a:link, a:active {
color: white;
}
audio {
display: none;
}
#click, #load {
font-family: sans-serif;
position: absolute;
left: 0;
top: 0;
z-index: 2;
background-color: rgba(0,0,0,0.8);
width: 100%;
height: 100%;
display: flex;
display: -webkit-flex;
flex-flow: column;
-webkit-flex-flow: column;
justify-content: center;
align-content: center;
align-items: center;
-webkit-justify-content: center;
-webkit-align-content: center;
-webkit-align-items: center;
min-height: auto;
}
#click>div, #load>div {
font-size: 24pt;
color: white;
background-color: blue;
border-radius: 1em;
padding: 1em;
}
#load {
z-index: 1;
}
#load>div {
color: black;
background-color: green;
}
#error {
display: none;
color: white;
background-color: red;
}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="b">
<div>audio-stream-test - for mobile</div>
<div>music: <a href="http://youtu.be/eUX39M_0MJ8">DOCTOR VOX - Level Up</a></div>
</div>
<div id="click">
<div>tap to play</div>
</div>
<div id="load">
<div>loading...</div>
</div>
<div id="error">
<div></div>
</div>
</body>
<script id="vs" type="notjs">
attribute float a_spread;
attribute float a_height;
varying vec4 v_color;
vec3 hsv2rgb(vec3 c) {
c = vec3(c.x, clamp(c.yz, 0.0, 1.0));
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
void main() {
gl_Position = vec4(a_spread, a_height * 2.0 - 1.0, 0, 1);
v_color = vec4(hsv2rgb(vec3(a_spread * 0.5 + 0.5, a_height, 1)), 1);
}
</script>
<script id="fs" type="notjs">
precision mediump float;
varying vec4 v_color;
void main() {
gl_FragColor = v_color;
}
</script>
<script src="js/twgl-full.min.js"></script>
<script>
"use strict";
function start() {
const context = new (window.AudioContext || window.webkitAudioContext)();
const analyser = context.createAnalyser();
twgl.setDefaults({attribPrefix: "a_"});
const gl = document.getElementById("c").getContext("webgl");
const programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);
const numPoints = analyser.frequencyBinCount;
const spreadArray = new Float32Array(numPoints);
const heightArray = new Uint8Array(numPoints);
for (let ii = 0; ii < numPoints; ++ii) {
spreadArray[ii] = ii / numPoints * 2 - 1; // make clipspace positions
}
const arrays = {
spread: { data: spreadArray, numComponents: 1 },
height: { data: heightArray, numComponents: 1, drawType: gl.DYNAMIC_DRAW },
};
const bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays);
function render() {
twgl.resizeCanvasToDisplaySize(gl.canvas);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.lineWidth(2);
gl.useProgram(programInfo.program);
analyser.getByteFrequencyData(heightArray);
twgl.setAttribInfoBufferFromArray(gl, bufferInfo.attribs.a_height, heightArray);
twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
twgl.drawBufferInfo(gl, bufferInfo, gl.LINE_STRIP);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
// Make a audio node
const audio = new Audio();
audio.loop = true;
audio.autoplay = true;
// this line is only needed if the music you are trying to play is on a
// different server than the page trying to play it.
// It asks the server for permission to use the music. If the server says "no"
// then you will not be able to play the music
// Note if you are using music from the same domain
// **YOU MUST REMOVE THIS LINE** or your server must give permission.
audio.crossOrigin = "anonymous";
// call `handleCanplay` when it music can be played
audio.addEventListener('canplay', () => {
document.querySelector('#load').style.display = 'none';
// connect the audio element to the analyser node and the analyser node
// to the main Web Audio context
try {
const source = context.createMediaElementSource(audio);
source.connect(analyser);
analyser.connect(context.destination);
} catch (e) {
showError(e.toString());
}
});
audio.addEventListener('error', (e) => {
showError(e.toString());
});
audio.src = "sounds/DOCTOR VOX - Level Up.mp3";
audio.load();
function showError(msg) {
const errorElem = document.querySelector('#error');
errorElem.style.display = 'block';
errorElem.textContent = msg;
}
}
const clickElem = document.querySelector("#click");
clickElem.addEventListener('click', (e) => {
clickElem.style.display = 'none';
start();
});
</script>
</html>