x-spectrum
Audio spectrum analyser Web Component
npm i x-spectrum
|
pnpm add x-spectrum
|
yarn add x-spectrum
|
Examples
# web
Try it live
# view source
example/web.ts
import { fetchAudioBuffer } from 'webaudio-tools'
import { SpectrumElement } from 'x-spectrum'
customElements.define('x-spectrum', SpectrumElement)
document.body.innerHTML = `
<div id="demo" style="display:inline-flex;height:80px;">
<x-spectrum autoresize></x-spectrum>
</div>
`
const ctx = new AudioContext({ sampleRate: 44100, latencyHint: 'playback' })
const analyser = ctx.createAnalyser()
analyser.fftSize = 16384
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, 75)
source.connect(analyser)
;(document.querySelector('x-spectrum') as SpectrumElement).analyser = analyser
})
window.onclick = () => ctx.state !== 'running' ? ctx.resume() : ctx.suspend()
if (ctx.state !== 'running')
document.body.appendChild(new Text('click to start/stop'))
API
Context<SpectrumElement & JsxContext<SpectrumElement> & Omit<{ <T>(ctor) => <T>(ctx) => #
src/x-spectrum.ts#L31 SpectrumElement
#
src/work/stagas/sigl/dist/types/sigl.d.ts#L25 $
CleanClass<T>
Wrapper<T>
},
"transition"
>> # context
src/work/stagas/sigl/dist/types/sigl.d.ts#L26 ContextClass<SpectrumElement & JsxContext<SpectrumElement> & Omit<{
<T>(ctor) =>
-
CleanClass<T>
<T>(ctx) =>
-
Wrapper<T>
},
"transition"
>> # stop
()
stop() =>
- void
# onmounted
EventHandler<SpectrumElement, CustomEvent<any>>
# onunmounted
EventHandler<SpectrumElement, CustomEvent<any>>
# created
(ctx)
Context<SpectrumElement & JsxContext<SpectrumElement> & Omit<{ <T>(ctor) => #
ctx
CleanClass<T>
<T>(ctx) =>
-
Wrapper<T>
},
"transition"
>> created(ctx) =>
- void
# mounted
($)
src/x-spectrum.ts#L81 Context<SpectrumElement & JsxContext<SpectrumElement> & Omit<{ <T>(ctor) => #
$
CleanClass<T>
<T>(ctx) =>
-
Wrapper<T>
},
"transition"
>> mounted($) =>
- void
# on
(name)
on<K>(name) =>
-
On<Fn<[ EventHandler<SpectrumElement, LifecycleEvents & object [K]> ], Off>>
# toJSON
()
toJSON() =>
-
Pick<SpectrumElement, keyof SpectrumElement>
Credits
- sigl by stagas – Web framework
- webaudio-tools by stagas – Useful tools for WebAudio.
Contributing
All contributions are welcome!