x-oscilloscope
Audio oscilloscope visualizer Web Component
npm i x-oscilloscope
|
pnpm add x-oscilloscope
|
yarn add x-oscilloscope
|
Examples
<summary>#
web
</summary> 
Try it live
<summary>#
view source
</summary> example/web.ts
import { fetchAudioBuffer } from 'webaudio-tools'
import { OscilloscopeElement } from 'x-oscilloscope'
customElements.define('x-oscilloscope', OscilloscopeElement)
document.body.innerHTML = `
<div id="demo" style="display:inline-flex;height:80px;">
<x-oscilloscope autoresize divider=4></x-oscilloscope>
</div>
`
const ctx = new AudioContext({ sampleRate: 44100, latencyHint: 0.08 })
const analyser = ctx.createAnalyser()
analyser.fftSize = 8192
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, 83)
source.connect(analyser)
;(document.querySelector('x-oscilloscope') as OscilloscopeElement).analyser =
analyser
})
window.onclick = () => ctx.state !== 'running' ? ctx.resume() : ctx.suspend()
if (ctx.state !== 'running')
document.body.appendChild(new Text('click to start/stop'))
// requestAnimationFrame <- for shoty
API
<summary>#
OscilloscopeElement
</summary> src/x-oscilloscope.tsx#L10
<summary>#
$
</summary> src/work/stagas/sigl/dist/types/sigl.d.ts#L25 Context<OscilloscopeElement & JsxContext<OscilloscopeElement> & Omit<{
<T>(ctor) =>
- CleanClass<T>
<T>(ctx) =>
- Wrapper<T>
},
"transition"
>> <summary>#
context
</summary> src/work/stagas/sigl/dist/types/sigl.d.ts#L26 ContextClass<OscilloscopeElement & JsxContext<OscilloscopeElement> & Omit<{
<T>(ctor) =>
- CleanClass<T>
<T>(ctx) =>
- Wrapper<T>
},
"transition"
>> <summary>#
created
(ctx) </summary>
<summary>#
ctx
</summary> Context<OscilloscopeElement & JsxContext<OscilloscopeElement> & Omit<{
<T>(ctor) =>
- CleanClass<T>
<T>(ctx) =>
- Wrapper<T>
},
"transition"
>> created(ctx) =>
- void
<summary>#
mounted
($) </summary> src/x-oscilloscope.tsx#L41
<summary>#
$
</summary> Context<OscilloscopeElement & JsxContext<OscilloscopeElement> & Omit<{
<T>(ctor) =>
- CleanClass<T>
<T>(ctx) =>
- Wrapper<T>
},
"transition"
>> mounted($) =>
- void
<summary>#
on
(name) </summary>
on<K>(name) =>
- On<Fn<[ EventHandler<OscilloscopeElement, LifecycleEvents & object [K]> ], Off>>
<summary>#
start
() – Start displaying the spectrum.</summary> src/x-oscilloscope.tsx#L31
start() =>
- void
<summary>#
stop
() – Stop displaying the spectrum.</summary> src/x-oscilloscope.tsx#L37
stop() =>
- void
Credits
- sigl by stagas – Web framework
- x-plot by stagas – A Web Component that plots zoomable and pannable waveforms.
Contributing
All contributions are welcome!