// Destroy all wavesurfer instances on unmount
// This should be called before calling initMultiTrack again to properly clean up
window.onbeforeunload = () => {
multitrack.destroy()
}
// Set sinkId
multitrack.once('canplay', async () => {
await multitrack.setSinkId('default')
console.log('Set sinkId to default')
})
Certainly! Below is the refactored single HTML file that incorporates both the HTML structure and the JavaScript logic for the multi-track mixer. The necessary script imports are correctly included, and the JavaScript is placed at the end of the <body> to ensure that all DOM elements are loaded before the script executes.
Note: Ensure that the audio files referenced in the url fields are accessible from your server. You might need to update the paths (/examples/audio/...) to point to the correct locations where your audio files are hosted.
Meta Tags: Defines the character set and the title of the page.
Script Imports:
WaveSurfer.js: Core library for audio visualization and manipulation.
Wavesurfer Multitrack Plugin: Extends WaveSurfer.js to support multiple tracks.
Styles (<style>): Basic styling for the body, controls, buttons, and the container to ensure a clean and user-friendly interface.
Body (<body>):
Heading: Displays the title "Multi-track Mixer".
Controls:
Zoom Slider: Allows users to adjust the zoom level of the mixer.
Play, Forward, Backward Buttons: Control playback and navigation within the audio tracks.
Container Div: The main area where the multitrack mixer will be rendered.
JavaScript Functionality:
DOMContentLoaded Event: Ensures that the script runs only after the DOM is fully loaded.
Multitrack Initialization:
Tracks Array: Defines multiple tracks with various properties like id, draggable, startPosition, url, envelope, volume, options, intro, and markers. Update the url fields with the correct paths to your audio files.
Configuration Options: Sets up the container, zoom level, cursor appearance, track styling, and envelope options.
Event Listeners:
Track Property Changes: Logs changes to track properties such as start position, cues, volume, fades, intro end, and envelope points.
Drop Event: Allows adding new tracks when a track is dropped onto the mixer.
Control Buttons:
Play/Pause Button: Toggles playback and updates the button text accordingly. Initially disabled until the audio is ready to play.
Additional Notes:
Audio File Paths: Ensure that the url properties in the tracks array point to valid audio files accessible from your server. You might need to host these files or use publicly available URLs.
Customization: Feel free to customize the styling, track configurations, and event handling as per your project requirements.
Browser Compatibility: Some features like setSinkId may require user permissions or may not be supported in all browsers. Ensure to handle such cases gracefully.
This single HTML file should provide a fully functional multi-track mixer with playback controls and zoom capabilities. Ensure that all dependencies are correctly loaded and that the audio files are accessible for the mixer to function properly.