audio texture

This commit is contained in:
Gyuri Horák 2023-03-05 16:37:35 +01:00
parent 2135bad80c
commit 032ff9fc88
Signed by: dyuri
GPG Key ID: 4993F07B3EAE8D38
7 changed files with 174 additions and 16 deletions

View File

@ -1,7 +1,3 @@
- textures
- canvas / other shader
- audio
- mic?
- fix mouse, add button, drag - fix mouse, add button, drag
- device orientation - device orientation

53
demo/audio.html Normal file
View File

@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>&lt;repa-shader&gt; demo</title>
<script type="module" src="../src/repa-texture.js"></script>
<script type="module" src="../src/repa-shader.js"></script>
<style>
body {
margin: 0;
padding: 0;
background: repeating-linear-gradient(45deg, #333, #333 10px, #666 10px, #666 20px);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
#fsinput {
width: 90vw;
height: 50vh;
}
</style>
</head>
<body>
<audio controls loop id="audio">
<source src="demo_audio.mp3">
</audio>
<repa-shader id="demoshader" fs-input="fsinput" alpha mouse snippets="noise.glsl" width=512 height=512>
<repa-texture src="avatar.png" name="tex_avatar" wrap="mirrored_repeat"></repa-texture>
<repa-texture ref="audio" type="audio" filter="nearest"></repa-texture>
void main() {
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec3 col = .5 + .5 * cos(uv.xyx + time + vec3(0, 2, 4));
col *= texture(texture1, vec2(.5 - .5*uv.y, 1.0)).rgb * texture(tex_avatar, uv).rgb;
outColor = vec4(col, 1.0);
}
</repa-shader>
<div>
<textarea id="fsinput"></textarea>
</div>
<button id="update">Update</button>
<script>
const updateButton = document.getElementById('update');
updateButton.addEventListener('click', () => {
const fsinput = document.getElementById('fsinput');
const shader = document.querySelector('repa-shader');
shader.render(fsinput.value);
});
</script>
</body>
</html>

View File

@ -57,7 +57,7 @@
void main() { void main() {
vec2 uv = gl_FragCoord.xy / resolution.xy; vec2 uv = gl_FragCoord.xy / resolution.xy;
vec3 col = .5 + .5 * cos(uv.xyx + time + vec3(0, 2, 4)); vec3 col = .5 + .5 * cos(uv.xyx + time + vec3(0, 2, 4));
col *= texture(texture0, vec2(uv.x, 1.-uv.y)).rgb; col *= texture(texture0, vec2(uv.x, uv.y)).rgb;
float dist = distance(uv, mouse.xy); float dist = distance(uv, mouse.xy);
float circle = smoothstep(.1, .2, dist) * .5 + .5; float circle = smoothstep(.1, .2, dist) * .5 + .5;

BIN
demo/demo_audio.mp3 Normal file

Binary file not shown.

View File

@ -26,7 +26,7 @@
<repa-shader id="demoshader" fs-input="fsinput" alpha mouse snippets="noise.glsl" width=512 height=512> <repa-shader id="demoshader" fs-input="fsinput" alpha mouse snippets="noise.glsl" width=512 height=512>
<repa-texture src="avatar.png" name="tex_avatar" wrap="mirrored_repeat"></repa-texture> <repa-texture src="avatar.png" name="tex_avatar" wrap="mirrored_repeat"></repa-texture>
<repa-texture src="futas.mp4"></repa-texture> <repa-texture src="futas.mp4"></repa-texture>
<repa-texture filter="nearest">[[1, 2, 3, 4, 5, 6, 128, 255], [255, 128, 64, 32, 4, 3, 2, 1]]</repa-texture> <!-- repa-texture filter="nearest">[[1, 2, 3, 4, 5, 6, 128, 255], [255, 128, 64, 32, 4, 3, 2, 1]]</repa-texture -->
<!-- repa-texture src="webcam"></repa-texture --> <!-- repa-texture src="webcam"></repa-texture -->
<!-- repa-texture ref="demoshader"></repa-texture --> <!-- repa-texture ref="demoshader"></repa-texture -->
<script type="x-shader/x-fragmen[DELETE]t"> <script type="x-shader/x-fragmen[DELETE]t">

57
demo/video.html Normal file
View File

@ -0,0 +1,57 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>&lt;repa-shader&gt; demo</title>
<script type="module" src="../src/repa-texture.js"></script>
<script type="module" src="../src/repa-shader.js"></script>
<style>
body {
margin: 0;
padding: 0;
background: repeating-linear-gradient(45deg, #333, #333 10px, #666 10px, #666 20px);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
#fsinput {
width: 90vw;
height: 50vh;
}
</style>
</head>
<body>
<video controls loop autoplay id="video" width=200>
<source src="futas.mp4">
</video>
<repa-shader id="demoshader" fs-input="fsinput" alpha mouse snippets="noise.glsl" width=512 height=512>
<repa-texture ref="video"></repa-texture>
<repa-texture ref="video" type="audio" filter="nearest"></repa-texture>
void main() {
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec3 col = .5 + .5 * cos(uv.xyx + time + vec3(0, 2, 4));
col *= texture(texture1, vec2(.5*uv.y, 1.0)).rgb;
vec3 futas = texture(texture0, uv).rgb;
col = mix(col, futas, uv.x);
outColor = vec4(col, 1.0);
}
</repa-shader>
<div>
<textarea id="fsinput"></textarea>
</div>
<button id="update">Update</button>
<script>
const updateButton = document.getElementById('update');
updateButton.addEventListener('click', () => {
const fsinput = document.getElementById('fsinput');
const shader = document.querySelector('repa-shader');
shader.render(fsinput.value);
});
</script>
</body>
</html>

View File

@ -188,6 +188,9 @@ class RepaTexture extends HTMLElement {
get type() { get type() {
if (this._type) { if (this._type) {
return this._type; return this._type;
} else if (this.hasAttribute('type')) {
this._type = this.getAttribute('type');
return this._type;
} }
if (this.ref) { if (this.ref) {
@ -197,6 +200,8 @@ class RepaTexture extends HTMLElement {
return 'video'; return 'video';
} else if (this.ref instanceof HTMLCanvasElement) { } else if (this.ref instanceof HTMLCanvasElement) {
return 'canvas'; return 'canvas';
} else if (this.ref instanceof HTMLAudioElement) {
return 'audio';
} else if (this.ref.nodeName === 'REPA-SHADER') { } else if (this.ref.nodeName === 'REPA-SHADER') {
return 'shader'; return 'shader';
} }
@ -211,13 +216,7 @@ class RepaTexture extends HTMLElement {
return this.type !== 'raw'; return this.type !== 'raw';
} }
// Array of Arrays setContent(data) {
set content(data) {
this.ready = true;
this._type = 'raw';
this._format = 'luminance';
this._forceUpdate = true;
this._width = data[0].length; this._width = data[0].length;
this._height = data.length; this._height = data.length;
this._content = new Uint8Array(this._width * this._height); this._content = new Uint8Array(this._width * this._height);
@ -227,10 +226,21 @@ class RepaTexture extends HTMLElement {
}); });
} }
set content(data) {
this.ready = true;
this._type = 'raw';
this._format = 'luminance';
this._forceUpdate = true;
this.setContent(data);
}
get content() { get content() {
if (this.ref) { if (this.ref) {
if (this.type === 'shader') { if (this.type === 'shader') {
return this.ref.target; return this.ref.target;
} else if (this.type === 'audio') {
return this.audioData;
} }
return this.ref; return this.ref;
} }
@ -238,6 +248,47 @@ class RepaTexture extends HTMLElement {
return this._content; return this._content;
} }
get analyser() {
if (this.type !== 'audio' || !this.ref || !this.ref.currentTime) {
return this._analyser;
}
if (!this._analyser) {
const audioCtx = new AudioContext();
const analyser = audioCtx.createAnalyser();
this._binCount = analyser.frequencyBinCount;
this._freqData = new Uint8Array(this._binCount);
this._timeData = new Uint8Array(this._binCount);
const source = audioCtx.createMediaElementSource(this.ref);
source.connect(analyser);
analyser.connect(audioCtx.destination);
this._analyser = analyser;
}
return this._analyser;
}
get audioData() {
// setup
this._format = 'luminance';
this._filter = 'nearest';
const analyser = this.analyser;
if (analyser) {
analyser.getByteFrequencyData(this._freqData);
analyser.getByteTimeDomainData(this._timeData);
this.setContent([this._freqData, this._timeData]);
} else {
this.setContent([[255, 128, 64, 32, 16, 8, 4, 2], [2, 4, 8, 16, 32, 64, 128, 255]]);
}
return this._content;
}
_guessType(src) { _guessType(src) {
if (src.toLowerCase() === 'webcam') { if (src.toLowerCase() === 'webcam') {
return 'webcam'; return 'webcam';
@ -267,7 +318,8 @@ class RepaTexture extends HTMLElement {
return this.ready && return this.ready &&
(this._forceUpdate || ( (this._forceUpdate || (
this.ref && ( this.ref && (
(this.ref instanceof HTMLVideoElement && this.ref.readyState === this.ref.HAVE_ENOUGH_DATA) || (this.ref instanceof HTMLVideoElement && this.ref.readyState >= this.ref.HAVE_ENOUGH_DATA) ||
(this.ref instanceof HTMLAudioElement && this.ref.readyState >= this.ref.HAVE_ENOUGH_DATA && !this.ref.paused && !this.ref.ended && this.ref.currentTime) ||
(this.ref instanceof HTMLCanvasElement) (this.ref instanceof HTMLCanvasElement)
) )
) )
@ -283,11 +335,11 @@ class RepaTexture extends HTMLElement {
} }
get magFilter() { get magFilter() {
return this.getAttribute('mag-filter') || this.getAttribute('filter') || 'linear'; return this._filter || this.getAttribute('mag-filter') || this.getAttribute('filter') || 'linear';
} }
get minFilter() { get minFilter() {
return this.getAttribute('min-filter') || this.getAttribute('filter') || 'linear'; return this._filter || this.getAttribute('min-filter') || this.getAttribute('filter') || 'linear';
} }
get wrapS() { get wrapS() {