~ knotless Search/
↑↓ move↵ open⇧↵ all results

cameron.stream/prism

Audio-reactive WebGL visualizer for OBS streams

0
0
main
44 lines · 6.9 KB Raw History
'use strict';
// Run with Playwright available via NODE_PATH; targets staging only by default.
const {chromium}=require('playwright'),assert=require('node:assert/strict');
(async()=>{const browser=await chromium.launch({channel:'chrome',headless:true});try{const page=await browser.newPage({viewport:{width:640,height:360}});const errors=[];page.on('pageerror',e=>errors.push(e.message));await page.goto(process.env.TEST_URL||'http://127.0.0.1:4180');await page.waitForFunction(()=>window.prismPresets);const presets=await page.evaluate(()=>prismPresets.list());assert.equal(presets.length,12);
for(const preset of presets){await page.evaluate(i=>prismPresets.select(i),preset.index);await page.waitForTimeout(180);assert.equal((await page.evaluate(()=>prismHealth())).preset,preset.id);assert.equal(await page.evaluate(()=>gl.getError()),0);console.log('PASS',preset.id)}
// Deterministic audio for new modes; never changes the live audio bridge.
await page.route('**/audio',route=>route.abort());await page.reload();await page.waitForFunction(()=>window.prismPresets);
for(let i=8;i<12;i++){
 await page.evaluate(i=>{settings.playing=false;prismPresets.select(i);transitionStart=-Infinity;target=.7;peakTarget=.85;energy=.7;peak=.85;lastSample=performance.now();},i);
 await page.waitForTimeout(250);
 const brightness=await page.evaluate(()=>{gl.bindFramebuffer(gl.FRAMEBUFFER,targets[read].framebuffer);const pixels=new Uint8Array(width*height*4);gl.readPixels(0,0,width,height,gl.RGBA,gl.UNSIGNED_BYTE,pixels);gl.bindFramebuffer(gl.FRAMEBUFFER,null);let total=0;for(let j=0;j<pixels.length;j+=4)total+=pixels[j]+pixels[j+1]+pixels[j+2];return total/(width*height*3)});
 assert.ok(brightness>.1,'new mode must render visible light');
 await page.screenshot({path:require('node:path').join(__dirname,'next',presets[i].id+'.png')});
 await page.evaluate(()=>{target=0;peakTarget=0;energy=0;peak=0;beat=0;bands.fill(0);bandTargets.fill(0);bass=mid=high=flux=bassTarget=midTarget=highTarget=fluxTarget=0;lastSample=performance.now();for(const t of targets){gl.bindFramebuffer(gl.FRAMEBUFFER,t.framebuffer);gl.clearColor(0,0,0,1);gl.clear(gl.COLOR_BUFFER_BIT)}gl.bindFramebuffer(gl.FRAMEBUFFER,null)});
 await page.waitForTimeout(120);
 const dark=await page.evaluate(()=>{gl.bindFramebuffer(gl.FRAMEBUFFER,targets[read].framebuffer);const pixels=new Uint8Array(width*height*4);gl.readPixels(0,0,width,height,gl.RGBA,gl.UNSIGNED_BYTE,pixels);gl.bindFramebuffer(gl.FRAMEBUFFER,null);let max=0;for(let j=0;j<pixels.length;j+=4)max=Math.max(max,pixels[j],pixels[j+1],pixels[j+2]);return max});
 assert.equal(dark,0,'silence must generate no new light');assert.equal(await page.evaluate(()=>gl.getError()),0);
 console.log('PASS audio/silence',presets[i].id,brightness);
}
for(const [key,index]of [['9',8],['0',9],['-',10],['=',11]]){await page.keyboard.press(key);assert.equal((await page.evaluate(()=>prismPresets.settings())).index,index)}
await page.evaluate(()=>{prismPresets.select(11);settings.playing=true;nextDue=performance.now()-1});await page.waitForTimeout(150);assert.equal((await page.evaluate(()=>prismHealth())).preset,'molten-diffraction');
await page.evaluate(()=>{prismPresets.select(4);settings.playing=true;nextDue=performance.now()-1});await page.waitForTimeout(150);assert.equal((await page.evaluate(()=>prismHealth())).preset,'monochrome-chrome');await page.keyboard.press('p');assert.equal((await page.evaluate(()=>prismPresets.settings())).playing,false);await page.keyboard.press('ArrowLeft');assert.equal((await page.evaluate(()=>prismHealth())).preset,'pressure-chamber');await page.evaluate(()=>prismPresets.setInterval(30));await page.reload();await page.waitForFunction(()=>window.prismPresets);const saved=await page.evaluate(()=>prismPresets.settings());assert.equal(saved.seconds,30);assert.equal(saved.playing,false);assert.equal(saved.index,4);assert.equal(await page.locator('body').innerText(),'');assert.deepEqual(errors,[]);console.log('PASS rotation, keyboard, persistence, no text, no JS/WebGL errors');
// Every preset must respond to WHERE the energy is, not just how much: bass-heavy and
// treble-heavy spectra at identical loudness must render clearly different scenes.
const profiles={bass:{bands:Array.from({length:64},(_,i)=>i<20?230:8),bass:.9,mid:.08,high:.02},treble:{bands:Array.from({length:64},(_,i)=>i>=40?230:8),bass:.02,mid:.08,high:.9}};
for(const preset of presets){const shots={};
 for(const [name,profile] of Object.entries(profiles)){
  shots[name]=await page.evaluate(([i,profile])=>new Promise(done=>{settings.playing=false;prismPresets.select(i);
   const feed=()=>ingest({status:'audio',source:'spectrum',rms:.1,peak:.3,flux:.1,kick:0,...profile});
   let n=0;const tick=()=>{feed();if(n===2){transitionStart=-Infinity;bands.fill(0);spectrumPixels.fill(0);for(const t of targets){gl.bindFramebuffer(gl.FRAMEBUFFER,t.framebuffer);gl.clearColor(0,0,0,1);gl.clear(gl.COLOR_BUFFER_BIT)}gl.bindFramebuffer(gl.FRAMEBUFFER,null)}
    if(++n<45)return requestAnimationFrame(tick);
    gl.bindFramebuffer(gl.FRAMEBUFFER,targets[read].framebuffer);const px=new Uint8Array(width*height*4);gl.readPixels(0,0,width,height,gl.RGBA,gl.UNSIGNED_BYTE,px);gl.bindFramebuffer(gl.FRAMEBUFFER,null);done({px:Array.from(px),spectral:prismHealth().spectral})};requestAnimationFrame(tick)}),[preset.index,profile]);
  assert.ok(shots[name].spectral,'spectrum input not recognised');
 }
 const a=shots.bass.px,b=shots.treble.px;let diff=0,ma=0,mb=0;for(let j=0;j<a.length;j+=4)for(let c=0;c<3;c++){diff+=Math.abs(a[j+c]-b[j+c]);ma+=a[j+c];mb+=b[j+c]}
 const ratio=diff/Math.max(1,ma,mb);assert.ok(ratio>.3,preset.id+' barely reacts to frequency content (bass vs treble ratio '+ratio.toFixed(3)+')');
 console.log('PASS frequency response',preset.id,'bass/treble difference',ratio.toFixed(2));
}
// A preset switch under loud audio must not jump: the crossfade starts from the frame last shown.
const jump=await page.evaluate(()=>new Promise(done=>{settings.playing=false;prismPresets.select(4);const loud=()=>{target=.9;peakTarget=.95;lastSample=performance.now()};let n=0;
 const grab=()=>{const px=new Uint8Array(width*height*4);gl.readPixels(0,0,width,height,gl.RGBA,gl.UNSIGNED_BYTE,px);return px};
 const diff=(a,b)=>{let s=0;for(let i=0;i<a.length;i+=4)s+=Math.abs(a[i]-b[i])+Math.abs(a[i+1]-b[i+1])+Math.abs(a[i+2]-b[i+2]);return s/(a.length/4*3)};
 const tick=()=>{loud();if(++n<90)return requestAnimationFrame(tick);const before=grab();requestAnimationFrame(()=>{loud();const steady=diff(before,grab());const last=grab();prismPresets.select(8);requestAnimationFrame(()=>{loud();const a=grab();requestAnimationFrame(()=>done({steady,switched:Math.max(diff(last,a),diff(a,grab()))}))})})};requestAnimationFrame(tick)}));
assert.ok(jump.switched<=Math.max(1,jump.steady*2),'preset switch jumped: '+JSON.stringify(jump));console.log('PASS seamless switch',JSON.stringify(jump));}finally{await browser.close()}})().catch(error=>{console.error(error);process.exitCode=1});