Wavesurfer.js is an interactive waveform rendering and audio playback library, perfect for web applications. It leverages modern web technologies to provide a robust and visually engaging audio experience.
Gold sponsor π Closed Caption Creator
- Getting started
- API reference
- Plugins
- CSS styling
- Frequent questions
- Upgrading from v6 to v7
- Development
- Tests
- Feedback
Install and import the package:
npm install --save wavesurfer.js
import WaveSurfer from 'wavesurfer.js'
Alternatively, insert a UMD script tag which exports the library as a global WaveSurfer
variable:
<script src="https://unpkg.com/wavesurfer.js@7"></script>
Create a wavesurfer instance and pass various options:
const wavesurfer = WaveSurfer.create({
container: '#waveform',
waveColor: '#4F4A85',
progressColor: '#383351',
url: '/audio.mp3',
})
To import one of the plugins, e.g. the Regions plugin:
import Regions from 'wavesurfer.js/dist/plugins/regions.esm.js'
Or as a script tag that will export WaveSurfer.Regions
:
<script src="https://unpkg.com/wavesurfer.js@7/dist/plugins/regions.min.js"></script>
TypeScript types are included in the package, so there's no need to install @types/wavesurfer.js
.
See more examples.
See the wavesurfer.js documentation on our website:
We maintain a number of official plugins that add various extra features:
- Regions β visual overlays and markers for regions of audio
- Timeline β displays notches and time labels below the waveform
- Minimap β a small waveform that serves as a scrollbar for the main waveform
- Envelope β a graphical interface to add fade-in and -out effects and control volume
- Record β records audio from the microphone and renders a waveform
- Spectrogram β visualization of an audio frequency spectrum (written by @akreal)
- Hover β shows a vertical line and timestmap on waveform hover
wavesurfer.js v7 is rendered into a Shadow DOM tree. This isolates its CSS from the rest of the web page.
However, it's still possible to style various wavesurfer.js elements with CSS via the ::part()
pseudo-selector.
For example:
#waveform ::part(cursor):before {
content: 'π';
}
#waveform ::part(region) {
font-family: fantasy;
}
You can see which elements you can style in the DOM inspector β they will have a part
attribute.
See this example to play around with styling.
Have a question about integrating wavesurfer.js on your website? Feel free to ask in our Discussions forum.
However, please keep in mind that this forum is dedicated to wavesurfer-specific questions. If you're new to JavaScript and need help with the general basics like importing NPM modules, please consider asking ChatGPT or StackOverflow first.
I'm having CORS issues
Wavesurfer fetches audio from the URL you specify in order to decode it. Make sure this URL allows fetching data from your domain. In browser JavaScript, you can only fetch data eithetr from the same domain or another domain if and only if that domain enables CORS. So if your audio file is on an external domain, make sure that domain sends the right Access-Control-Allow-Origin headers. There's nothing you can do about it from the requesting side (i.e. your JS code).Does wavesurfer support large files?
Since wavesurfer decodes audio entirely in the browser using Web Audio, large clips may fail to decode due to memory constraints. We recommend using pre-decoded peaks for large files (see this example). You can use a tool like bbc/audiowaveform to generate peaks.What about streaming audio?
Streaming audio is supported only with pre-decoded peaks and duration.There is a mismatch between my audio and the waveform. How do I fix it?
If you're using a VBR (variable bit rate) audio file, there might be a mismatch between the audio and the waveform. This can be fixed by converting your file to CBR (constant bit rate).Alternatively, you can use the Web Audio shim which is more accurate.
How do I connect wavesurfer.js to Web Audio effects?
Generally, wavesurfer.js doesn't aim to be a wrapper for all things Web Audio. It's just a player with a waveform visualization. It does allow connecting itself to a Web Audio graph by exporting its audio element (see this example) but nothign more than that. Please don't expect wavesurfer to be able to cut, add effects, or process your audio in any way.Wavesurfer.js v7 is a TypeScript rewrite of wavesurfer.js that brings several improvements:
- Typed API for better development experience
- Enhanced decoding and rendering performance
- New and improved plugins
Most options, events, and methods are similar to those in previous versions.
- HTML audio playback by default (used to be an opt-in via
backend: "MediaElement"
) - The Markers plugin is removed β you should use the Regions plugin with just a
startTime
. - No Microphone plugin β superseded by the new Record plugin with more features.
- The Cursor plugin is replaced by the Hover plugin.
audioContext
,closeAudioContext
,audioScriptProcessor
autoCenterImmediately
βautoCenter
is now always immediate unless the audio is playingbackgroundColor
,hideCursor
β this can be easily set via CSSmediaType
β you should instead pass an entire media element in themedia
option. Example.partialRender
β done by defaultpixelRatio
βwindow.devicePixelRatio
is used by defaultrenderer
β there's just one renderer for now, so no need for this optionresponsive
β responsiveness is enabled by defaultscrollParent
β the container will scroll ifminPxPerSec
is set to a higher valueskipLength
β there's noskipForward
andskipBackward
methods anymoresplitChannelsOptions
β you should now usesplitChannels
to pass the channel options. Passheight: 0
to hide a channel. See this example.drawingContextAttributes
,maxCanvasWidth
,forceDecode
β removed to reduce code complexityxhr
- please usefetchParams
insteadbarMinHeight
- the minimum bar height is now 1 pixel by default
getFilters
,setFilter
β see the Web Audio exampledrawBuffer
β to redraw the waveform, usesetOptions
instead and pass new rendering optionscancelAjax
β you can pass an AbortSignal infetchParams
skipForward
,skipBackward
,setPlayEnd
β can be implemented usingsetTime(time)
exportPCM
is replaced withexportPeaks
which returns arrays of floatstoggleMute
is now calledsetMuted(true | false)
setHeight
,setWaveColor
,setCursorColor
, etc. β usesetOptions
with the corresponding params instead. E.g.,wavesurfer.setOptions({ height: 300, waveColor: '#abc' })
See the complete documentation of the new API.
To get started with development, follow these steps:
- Install dev dependencies:
yarn
- Start the TypeScript compiler in watch mode and launch an HTTP server:
yarn start
This command will open http://localhost:9090 in your browser with live reload, allowing you to see the changes as you develop.
The tests are written in the Cypress framework. They are a mix of e2e and visual regression tests.
To run the test suite locally, first build the project:
yarn build
Then launch the tests:
yarn cypress
We appreciate your feedback and contributions!
If you encounter any issues or have suggestions for improvements, please don't hesitate to post in our forum.
We hope you enjoy using wavesurfer.js and look forward to hearing about your experiences with the library!