Skip to content

Instantly share code, notes, and snippets.

@katspaugh
Last active September 29, 2023 17:38
Show Gist options
  • Save katspaugh/a6fc58e1a1516f7b89b338c1cfe6e6f3 to your computer and use it in GitHub Desktop.
Save katspaugh/a6fc58e1a1516f7b89b338c1cfe6e6f3 to your computer and use it in GitHub Desktop.
// Custom rendering function
import WaveSurfer from 'https://unpkg.com/wavesurfer.js@7/dist/wavesurfer.esm.js'
const wavesurfer = WaveSurfer.create({
container: document.body,
waveColor: 'rgb(200, 0, 200)',
progressColor: 'rgb(100, 0, 100)',
url: '/examples/audio/demo.wav',
renderFunction: (channels, ctx) => {
const { width, height } = ctx.canvas
const scale = channels[0].length / width
ctx.translate(0, height / 2)
ctx.strokeStyle = ctx.fillStyle
ctx.beginPath()
for (let i = 0; i < width; i++) {
const peak = channels[0][Math.floor(i * scale)]
ctx.lineTo(i, peak * height)
}
ctx.stroke()
ctx.closePath()
},
})
wavesurfer.on('interaction', () => {
wavesurfer.play()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment