/blog

Javascript for nice waveforms

A simple Javascript/CSS solution for a drop-in replacement of the standard audio tag for more stylized audio playback.

In my previous post about the SuperCollider musical programming language I wanted to include some audio demos. I didn’t like the traditional way of showing an audio file and tried to make something more akin to how soundcloud renders (i.e. an abstract waveform). It turns out this is really easy to do thanks to wavesurfer.js.

This is the traditional way of making showing an audio file in HTML:

<audio src="drone5.wav" type="audio/wav" controls></audio> 
<audio src="drone4.wav" type="audio/wav" controls></audio> 

which looks like this:


It turns out its really easy to now get a waveform visualized playback with a drop-in replacement for the audio tag. With about 50 lines of code (below) you can convert audio elements into something much nicer and still practical.

Now the same two audio tags will instead look like this:

How does it work

I simpy wrote a little javascript as a wrapper around wavesurfer.js. The wrapper looks for all the audio selectors and converts them to waveforms. Then it adds SVG buttons for play/pause from feathericons.com.

Here is the entire HTML you need for a drop-in replacement to convert audio tags into nice playable waveforms:

<script src="https://unpkg.com/wavesurfer.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    var els = document.querySelectorAll("audio");
    for (var i = 0; i < els.length; i++) {
        console.log(els[i].src);
        let i_ = i;
        let src_ = els[i].src;
        let newNode = document.createElement("div")
        newNode.innerHTML = `<table style="padding-top:16px;padding-bottom:16px;"><tr><td style="position: relative;top: 4px;width:40px;"><div class="controls" style=""> <button class="audiobtn" data-action="play" style="background: #fff; border: none; padding-left:0;padding-right:0;"> <svg class="playicon" xmlns="http://www.w3.org/2000/svg" width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="feather feather-play-circle"><circle cx="12" cy="12" r="10"></circle><polygon points="10 8 16 12 10 16 10 8"></polygon></svg> <svg class="pauseicon" style="display:none;" xmlns="http://www.w3.org/2000/svg" width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="feather feather-pause-circle"><circle cx="12" cy="12" r="10"></circle><line x1="10" y1="15" x2="10" y2="9"></line><line x1="14" y1="15" x2="14" y2="9"></line></svg> </button></div></td><td width="100%"><div class="wave"></div></td></tr></table>`;
        els[i_].parentNode.insertBefore(newNode, els[i_])
        els[i].remove();
        let wavesurfer = WaveSurfer.create({
            container: document.getElementsByClassName("wave")[i_],
            waveColor: '#909090',
            progressColor: '#443e3c',
            cursorColor: '#ffffff',
            backend: 'MediaElement',
            mediaControls: false,
            hideScrollbar: true,
            minPxPerSec: 120,
            normalize: true,
            height: 64,
        });
        wavesurfer.once('ready', function() {
            console.log('Using wavesurfer.js ' + WaveSurfer.VERSION);
        });
        wavesurfer.on('error', function(e) {
            console.warn(e);
        });
        wavesurfer.on('play', function(e) {
            document.getElementsByClassName("playicon")[i_].style.display = "none";
            document.getElementsByClassName("pauseicon")[i_].style.display = "block";
        });
        wavesurfer.on('pause', function(e) {
            document.getElementsByClassName("playicon")[i_].style.display = "block";
            document.getElementsByClassName("pauseicon")[i_].style.display = "none";
        });
        newNode.querySelector('[data-action="play"]')
            .addEventListener('click', wavesurfer.playPause.bind(wavesurfer));
        wavesurfer.load(src_);
    }
});
</script>

 / #software #tutorial #music #javascript 

Made by Zack, filed in Blog. 2019.