Last active
January 3, 2024 07:06
-
-
Save katspaugh/85830fe8827040932066b40bdd03841f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Custom rendering function | |
import WaveSurfer from 'wavesurfer.js' | |
const wavesurfer = WaveSurfer.create({ | |
container: document.body, | |
waveColor: 'rgb(200, 0, 200)', | |
progressColor: 'rgb(100, 0, 100)', | |
url: '/examples/audio/demo.wav', | |
}) | |
wavesurfer.on('ready', () => { | |
const customRenderWavesurfer = WaveSurfer.create({ | |
container: wavesurfer.getWrapper(), | |
media: wavesurfer.getMediaElement(), | |
peaks: wavesurfer.exportPeaks(), | |
duration: wavesurfer.getDuration(), | |
/** | |
* Render a waveform as a squiggly line | |
* @see https://css-tricks.com/making-an-audio-waveform-visualizer-with-vanilla-javascript/ | |
*/ | |
renderFunction: (channels, ctx) => { | |
const { width, height } = ctx.canvas | |
const scale = channels[0].length / width | |
const step = 10 | |
ctx.translate(0, height / 2) | |
ctx.strokeStyle = ctx.fillStyle | |
ctx.beginPath() | |
for (let i = 0; i < width; i += step * 2) { | |
const index = Math.floor(i * scale) | |
const value = Math.abs(channels[0][index]) | |
let x = i | |
let y = value * height | |
ctx.moveTo(x, 0) | |
ctx.lineTo(x, y) | |
ctx.arc(x + step / 2, y, step / 2, Math.PI, 0, true) | |
ctx.lineTo(x + step, 0) | |
x = x + step | |
y = -y | |
ctx.moveTo(x, 0) | |
ctx.lineTo(x, y) | |
ctx.arc(x + step / 2, y, step / 2, Math.PI, 0, false) | |
ctx.lineTo(x + step, 0) | |
} | |
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