Skip to content
generated from stagas/ts

Audio waveform visualizer Web Component.

License

Notifications You must be signed in to change notification settings

stagas/x-waveform

Repository files navigation

x-waveform

Audio waveform visualizer Web Component

npm i x-waveform pnpm add x-waveform yarn add x-waveform

Examples

# web

    Try it live

    # view source example/web.ts

    import { fetchAudioBuffer } from 'webaudio-tools'
    import { WaveformElement } from 'x-waveform/dist/esm'
    
    customElements.define('x-waveform', WaveformElement)
    document.body.innerHTML = `
    <div id="demo" style="display:inline-flex;height:80px;">
      <x-waveform autoresize></x-waveform>
    </div>
    `
    
    const ctx = new AudioContext({ sampleRate: 44100, latencyHint: 'playback' })
    
    const analyser = ctx.createAnalyser()
    analyser.fftSize = 512
    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-waveform') as WaveformElement).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

# WaveformElement src/x-waveform.ts#L29

Credits

Contributing

Fork or edit and submit a PR.

All contributions are welcome!

License

MIT © 2022 stagas