Analyser


    import sono from 'sono';
    import 'sono/effects';

    const sound = sono.create({
        url: ['beats.ogg', 'beats.mp3'],
        loop: true
    })
    .play();

    const analyser = sound.effects.add(sono.analyser({
        fftSize: 128
    }));

    function averageAmplitude(wave) {
        let sum = 0;
        for (let i = 0; i < wave.length; i++) {
            sum += wave[i];
        }
        return sum / wave.length / 256;
    }

    function update() {
        window.requestAnimationFrame(update);

        const value = averageAmplitude(analyser.getWaveform());

        if (value < min) {
            min = value;
        }

        if (value > max) {
            max = value;
        }

        const range = (max - min) || 1;
        const norm = (value - min) / range;

        if (norm > threshold) {
            // got a peak so animate, speed up, glow etc
        }
    }
    update();