The ESP-IDF project, docs, tools, tests and web UI move from v2/ to the root, and the Makefile, README, build scripts and test runner drop the v2/ prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 line
8.0 KiB
HTML
1 line
8.0 KiB
HTML
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Audio and beats</title><style>body{font-family:sans-serif;max-width:32em;margin:2em auto;padding:0 1em}td{padding:.2em 1em .2em 0}#bar{height:1.5em;background:#ddd;border-radius:4px;overflow:hidden}#fill{height:100%;width:0;background:#2a9d4b;transition:width .2s}.dot{display:inline-block;width:3em;height:3em;border-radius:50%;background:#ddd;vertical-align:middle}#onset{width:1.2em;height:1.2em;margin-left:1em}.bb{display:inline-block;width:1.6em;height:1.6em;line-height:1.6em;text-align:center;border-radius:4px;background:#eee;margin-right:.3em;font-size:.9em}.bb.on{background:#457b9d;color:#fff}.bb.one.on{background:#e63946}#barpos.guess .bb{opacity:.45}#tl{width:100%;height:90px;background:#f4f4f4;border-radius:4px}kbd{border:1px solid #bbb;border-radius:3px;padding:0 .3em}@keyframes down{from{background:#e63946;box-shadow:0 0 0 .5em #e6394655}to{background:#ddd}}#beat.down{animation:down .5s}@keyframes beat{from{background:#e63946}to{background:#ddd}}@keyframes onset{from{background:#457b9d}to{background:#ddd}}#beat.on{animation:beat .3s}#onset.on{animation:onset .15s}</style></head><body><p><a href="/">← Controller</a></p><h1>Audio and beats</h1><table><tr><td>IP</td><td id="ip"></td></tr><tr><td>MAC</td><td id="mac"></td></tr><tr><td>Uptime</td><td id="up"></td></tr><tr><td>Free heap</td><td id="heap"></td></tr><tr><td>Mic RMS</td><td id="rms"></td></tr><tr><td>Mic peak</td><td id="peak"></td></tr><tr><td>Approx. SPL</td><td id="spl"></td></tr><tr><td>Tempo</td><td id="bpm">–</td></tr><tr><td>Confidence</td><td id="conf"></td></tr><tr><td>Time</td><td id="meter">–</td></tr></table><div id="bar"><div id="fill"></div></div><p><span class="dot" id="beat"></span><span class="dot" id="onset"></span></p><p id="barpos"></p><p><button id="tap" title="Press on the first beat of a bar to correct the bar position (or press T)">Tap beat 1</button></p><h2>You vs the computer</h2><p>Press <kbd>Space</kbd> on every beat.</p><canvas id="tl"></canvas><table><tr><td>Your tempo</td><td id="ytempo">–</td></tr><tr><td>Your timing</td><td id="yoff">–</td></tr><tr><td>Agreement</td><td id="ymatch">–</td></tr></table><p><button id="listen">Listen</button> <span id="lat"></span></p><p><a href="/status">/status</a> (JSON) · <a href="/audio.wav">/audio.wav</a> (live) · <a href="/beats">/beats</a> (event stream)</p><script>const $=id=>document.getElementById(id);async function tick(){try{const s=await(await fetch('/status')).json();$('ip').textContent=s.ip;$('mac').textContent=s.mac;$('up').textContent=s.uptime_s+' s';$('heap').textContent=s.free_heap+' bytes';$('rms').textContent=s.mic_rms_dbfs.toFixed(1)+' dBFS';$('peak').textContent=s.mic_peak_dbfs.toFixed(1)+' dBFS';$('spl').textContent=s.mic_spl_db.toFixed(0)+' dB';$('fill').style.width=Math.max(0,Math.min(100,(s.mic_rms_dbfs+90)/90*100))+'%';}catch(e){}setTimeout(tick,250)}tick();function flash(id,c){const d=$(id);d.classList.remove('on','down');void d.offsetWidth;d.classList.add(c||'on');}function bar(m,n){const b=$('barpos');if(b.children.length!=m){b.innerHTML='';for(let i=1;i<=m;i++){const s=document.createElement('span');s.className='bb'+(i==1?' one':'');s.textContent=i;b.appendChild(s);}}[...b.children].forEach((s,i)=>s.classList.toggle('on',i+1==n));}function tap(){fetch('/tap',{method:'POST'});}$('tap').addEventListener('pointerdown',e=>{e.preventDefault();tap();});document.addEventListener('keydown',e=>{if((e.key=='t'||e.key=='T')&&!e.repeat)tap();});let off=null,rtt=1e9,cbpm=0;const comp=[],taps=[];async function sync(){rtt*=1.5;for(let i=0;i<4;i++){try{const a=performance.now(),r=await(await fetch('/time',{cache:'no-store'})).json(),b=performance.now();if(b-a<=rtt){rtt=b-a;off=(a+b)/2-r.us/1000;}}catch(e){}}}sync();setInterval(sync,15000);document.addEventListener('keydown',e=>{if(e.code=='Space'){e.preventDefault();if(!e.repeat)taps.push(performance.now());}});document.addEventListener('keyup',e=>{if(e.code=='Space')e.preventDefault();});function median(a){if(!a.length)return null;const s=[...a].sort((x,y)=>x-y);return s[s.length>>1];}function stats(now){const lim=now-20000;while(comp.length&&comp[0].t<lim)comp.shift();while(taps.length&&taps[0]<lim)taps.shift();const iv=[];for(let i=1;i<taps.length;i++){const d=taps[i]-taps[i-1];if(d>=300&&d<=1000)iv.push(d);}const ti=median(iv.slice(-8));$('ytempo').textContent=ti?(60000/ti).toFixed(1)+' BPM (computer '+(cbpm?cbpm.toFixed(1):'-')+')':'-';const pairs=[];for(const t of taps){if(now-t<400)continue;let b=null;for(const c of comp)if(b===null||Math.abs(c.t-t)<Math.abs(b-t))b=c.t;if(b!==null)pairs.push([t,b]);}const rec=pairs.slice(-16),hit=rec.filter(p=>Math.abs(p[0]-p[1])<=70).map(p=>p[0]-p[1]);if(hit.length){const m=hit.reduce((a,b)=>a+b,0)/hit.length,sd=Math.sqrt(hit.reduce((a,b)=>a+(b-m)*(b-m),0)/hit.length);$('yoff').textContent=(m>=0?'+':'')+m.toFixed(0)+' ms '+(Math.abs(m)<5?'(together)':m>0?'(you tap later)':'(you tap earlier)')+', spread '+sd.toFixed(0)+' ms';}else $('yoff').textContent='-';$('ymatch').textContent=rec.length?hit.length+' of '+rec.length+' taps within 70 ms of a computer beat':'-';return pairs;}let pairs=[];setInterval(()=>{pairs=stats(performance.now());},200);function draw(){const now=performance.now(),c=$('tl'),w=c.clientWidth,h=c.clientHeight,r=devicePixelRatio||1;if(c.width!=w*r){c.width=w*r;c.height=h*r;}const g=c.getContext('2d');g.setTransform(r,0,0,r,0,0);g.clearRect(0,0,w,h);const X=t=>w-(now-t)/4000*w;g.fillStyle='#666';g.font='11px sans-serif';g.fillText('computer',4,12);g.fillText('you',4,h-4);g.strokeStyle='#bbb';for(const p of pairs){const a=X(p[0]),b=X(p[1]);if(a<0)continue;g.beginPath();g.moveTo(b,h/2-6);g.lineTo(a,h/2+6);g.stroke();}for(const b of comp){const x=X(b.t);if(x<0||x>w)continue;g.fillStyle=b.d?'#e63946':'#457b9d';g.fillRect(x-2,b.d?4:12,4,b.d?h/2-10:h/2-18);}g.fillStyle='#2a9d4b';for(const t of taps){const x=X(t);if(x<0)continue;g.fillRect(x-2,h/2+6,4,h/2-12);}requestAnimationFrame(draw);}requestAnimationFrame(draw);const es=new EventSource('/beats');es.addEventListener('beat',e=>{const s=JSON.parse(e.data);flash('beat',s.bar==1&&s.sure?'down':'on');if(off!==null&&s.us){comp.push({t:s.us/1000+off,d:s.bar==1&&s.sure});cbpm=s.bpm;}if(s.bar){bar(s.meter,s.bar);$('barpos').classList.toggle('guess',!s.sure);$('meter').textContent=s.meter+'/4'+(s.sure?'':' (guessing beat 1)');}else{$('meter').textContent='listening\u2026';}});es.addEventListener('onset',()=>flash('onset'));es.addEventListener('tempo',e=>{const s=JSON.parse(e.data);$('bpm').textContent=s.bpm?s.bpm.toFixed(1)+' BPM'+(s.locked?'':' (not locked)'):'\u2013';$('conf').textContent=s.confidence.toFixed(2);});let ctx=null,ctl=null;$('listen').onclick=async()=>{if(ctl){ctl.abort();ctl=null;ctx.close();$('listen').textContent='Listen';$('lat').textContent='';return;}const ac=ctx=new AudioContext(),my=ctl=new AbortController();$('listen').textContent='Stop';try{const r=await fetch('/audio.wav',{signal:my.signal,cache:'no-store'});if(!r.ok)throw new Error(r.status==409?'another listener is connected':'HTTP '+r.status);const rd=r.body.getReader();let skip=44,carry=null,t=0;for(;;){const{value,done}=await rd.read();if(done)break;let b=value;if(skip){const k=Math.min(skip,b.length);b=b.subarray(k);skip-=k;}if(carry){const m=new Uint8Array(carry.length+b.length);m.set(carry);m.set(b,carry.length);b=m;carry=null;}if(b.length&1){carry=b.slice(-1);b=b.subarray(0,b.length-1);}if(!b.length)continue;const n=b.length/2,dv=new DataView(b.buffer,b.byteOffset,b.length),ab=ac.createBuffer(1,n,16000),d=ab.getChannelData(0);for(let i=0;i<n;i++)d[i]=dv.getInt16(2*i,true)/32768;const now=ac.currentTime;if(t<now+0.03||t>now+0.4)t=now+0.15;const src=ac.createBufferSource();src.buffer=ab;src.connect(ac.destination);src.start(t);t+=ab.duration;$('lat').textContent='buffer '+Math.round((t-now)*1000)+' ms';}throw new Error('stream ended');}catch(e){if(e.name!=='AbortError'){$('lat').textContent='Error: '+e.message;}if(ctl===my){ctl=null;ac.close();$('listen').textContent='Listen';}}};</script></body></html> |