generated from stagas/ts
/
web.ts
33 lines (27 loc) · 1.07 KB
/
web.ts
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
import { fetchAudioBuffer } from 'webaudio-tools'
import { SpectrogramElement } from '../dist/esm'
customElements.define('x-spectrogram', SpectrogramElement)
document.body.innerHTML = `
<div id="demo" style="display:inline-flex;height:80px;">
<x-spectrogram autoresize></x-spectrogram>
</div>
`
const ctx = new AudioContext({ sampleRate: 44100, latencyHint: 'playback' })
const analyser = ctx.createAnalyser()
analyser.fftSize = 4096
analyser.smoothingTimeConstant = 0
analyser.maxDecibels = 0
analyser.minDecibels = -100
// @ts-ignore
const url = new URL('alpha_molecule.ogg', import.meta.url).toString()
fetchAudioBuffer(ctx, url).then(audioBuffer => {
const source = ctx.createBufferSource()
source.buffer = audioBuffer
source.loop = true
source.connect(ctx.destination)
source.start(0, 30)
source.connect(analyser)
;(document.querySelector('x-spectrogram') as SpectrogramElement).analyser = analyser
})
window.onclick = () => ctx.state !== 'running' ? ctx.resume() : ctx.suspend()
if (ctx.state !== 'running') document.body.appendChild(new Text('click to start/stop'))