Audio waveform player - temporary fork for experiments
npm install @rewbs/wavesurfer.js
wavesurfer.js 
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
> š v8 Beta Available! Try the new reactive streams and state management features. Learn more ā
1. Getting started
2. What's new in v8
3. API reference
4. Plugins
5. CSS styling
6. Frequent questions
7. Contributing
8. Tests
9. Feedback
Install and import the package:
``bash`
npm install wavesurfer.js
`js`
import WaveSurfer from 'wavesurfer.js'
Alternatively, insert a UMD script tag which exports the library as a global WaveSurfer variable:`html`
Create a wavesurfer instance and pass various options:
`js
const wavesurfer = WaveSurfer.create({
container: '#waveform',
waveColor: '#4F4A85',
progressColor: '#383351',
url: '/audio.mp3',
})
// Subscribe to events
wavesurfer.on('ready', () => {
wavesurfer.play()
})
`
TypeScript types are included in the package, so there's no need to install @types/wavesurfer.js.
See more examples.
v8 is 100% backward compatible ā your existing code works without changes!
Subscribe to events with powerful stream operators:
`js
// Debounce time updates for better performance
wavesurfer.getEventStream('timeupdate')
.debounce(100)
.subscribe(time => {
updateUI(time)
})
// Chain multiple operators
wavesurfer.getEventStream('timeupdate')
.filter(() => wavesurfer.isPlaying())
.throttle(1000)
.map(time => Math.floor(time))
.distinct()
.subscribe(time => {
console.log(Second ${time})`
})
React to application state changes:
`js
// Subscribe to playing state
wavesurfer.state
.select(s => s.playback.isPlaying)
.subscribe(isPlaying => {
button.textContent = isPlaying ? 'Pause' : 'Play'
})
// Combine multiple state values
wavesurfer.state
.selectMany(
s => s.playback.currentTime,
s => s.audio.duration
)
.subscribe(([time, duration]) => {
const progress = (time / duration) * 100
updateProgressBar(progress)
})
`
- map(fn) ā Transform values
- filter(fn) ā Filter values
- debounce(ms) ā Debounce updates
- throttle(ms) ā Throttle updates
- distinct() ā Only emit unique values
- take(n) ā Take first n emissions
- takeUntil(notifier) ā Take until another stream emits
Try the beta:
`bash`
npm install wavesurfer.js@beta
Learn more:
- Beta Announcement ā What's new and how to try it
- User Guide ā Complete v8 documentation
- Migration Guide ā Upgrading from v7 (if you want to)
See the wavesurfer.js documentation on our website:
For v8 features, see the v8 API Reference.
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 timestamp on waveform hover
To import a plugin (v7):
`js`
import Regions from 'wavesurfer.js/dist/plugins/regions.esm.js'
Or as a script tag:
`html`
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:
`css`
#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 either 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.
Alternatively, you can use the Web Audio shim which is more accurate.
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).
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 nothing more than that. Please don't expect wavesurfer to be able to cut, add effects, or process your audio in any way.
We welcome contributions! Here's how to get started:
1. Install dependencies:
`bash`
npm install
2. Start the development server:
`bash`
npm run dev
This command will start the Vite dev server with live reload at http://localhost:5173.
3. Run tests:
`bash`
npm test
Interested in building plugins for wavesurfer.js? Check out our comprehensive guides:
- Plugin Development Guide ā Complete guide to building plugins
- Contributing Guide ā Development workflow and guidelines
- API Reference ā Full API documentation
``
wavesurfer.js/
āāā src/
ā āāā streams/ # Reactive streams
ā āāā state/ # State management
ā āāā core/ # Pure functions
ā āāā plugins/ # Plugin system
ā āāā ... # Core components
āāā examples/ # Example files
āāā docs/ # Documentation
The project uses Vitest for unit tests and Cypress for e2e/visual regression tests.
`bash`
npm test
First build the project:
`bash`
npm run build
Then launch the tests:
`bash``
npm run 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!
---
License: BSD-3-Clause
Made with ā„ by the wavesurfer.js community