Skip to content

Instantly share code, notes, and snippets.

@katspaugh
Last active March 11, 2024 15:56
Show Gist options
  • Save katspaugh/998f3697a6ce83671926c2f3868d9b7d to your computer and use it in GitHub Desktop.
Save katspaugh/998f3697a6ce83671926c2f3868d9b7d to your computer and use it in GitHub Desktop.
import WaveSurfer from 'wavesurfer.js'
import WebAudioPlayer from 'wavesurfer.js/dist/webaudio.js'
const media = new WebAudioPlayer()
media.src = '/examples/audio/audio.wav'
function playSegment(fromTime, toTime) {
media.currentTime = fromTime
media.play()
media.stopAt(toTime)
}
media.addEventListener('loadedmetadata', () => {
const wavesurfer = WaveSurfer.create({
container: document.body,
waveColor: 'rgb(200, 0, 200)',
progressColor: 'rgb(100, 0, 100)',
media,
peaks: media.getChannelData(),
})
})
document.querySelector('#play').onclick = () => {
playSegment(5, 8)
}
/*
<html>
<button id="play">Play 3 seconds</button>
</html>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment