File size: 1,373 Bytes
68fdb58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useEffect } from 'react'
import { getSoundtrack } from './audio'
import { SHOTS } from './scenes'

export default function ADebug() {
  useEffect(() => {
    const t0 = performance.now()
    getSoundtrack(undefined, true).then(b => {
      const d = b.getChannelData(0)
      const rms = SHOTS.map(s => {
        let a = 0, pk = 0; const i0 = Math.floor(s.start * b.sampleRate), i1 = Math.floor(s.end * b.sampleRate)
        for (let i = i0; i < i1; i++) { a += d[i] * d[i]; pk = Math.max(pk, Math.abs(d[i])) }
        return `${s.index}:${Math.sqrt(a / (i1 - i0)).toFixed(3)}/${pk.toFixed(2)}`
      })
      const L = b.getChannelData(0), R = b.getChannelData(1), pcm = new Int16Array(L.length * 2)
      for (let i = 0; i < L.length; i++) { pcm[2 * i] = Math.max(-1, Math.min(1, L[i])) * 32767; pcm[2 * i + 1] = Math.max(-1, Math.min(1, R[i])) * 32767 }
      const u8 = new Uint8Array(pcm.buffer); let bin = ''
      for (let i = 0; i < u8.length; i += 0x8000) bin += String.fromCharCode(...u8.subarray(i, i + 0x8000))
      ;(window as unknown as { __pcm: string }).__pcm = btoa(bin)
      console.log('INFO dur=' + b.duration.toFixed(2) + ' ms=' + (performance.now() - t0).toFixed(0) + ' ' + rms.join(' '))
    }).catch(e => console.log('INFO ERROR ' + e)).finally(() => { (window as unknown as { __done: boolean }).__done = true })
  }, [])
  return null
}