cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <p class="text">Hover your mouse over</p>
<div class="canvasHook"></div>
            
          
!
            
              (function() {
    function createAudioContext(desiredSampleRate) {
        var AudioCtor = window.AudioContext || window.webkitAudioContext;
        desiredSampleRate = typeof desiredSampleRate === 'number' ? desiredSampleRate : 44100;
        var context = new AudioCtor();
        // Check if hack is necessary. Only occurs in iOS6+ devices
        // and only when you first boot the iPhone, or play a audio/video
        // with a different sample rate
        if (/(iPhone|iPad)/i.test(navigator.userAgent) && context.sampleRate !== desiredSampleRate) {
            var buffer = context.createBuffer(1, 1, desiredSampleRate);
            var dummy = context.createBufferSource();
            dummy.buffer = buffer;
            dummy.connect(context.destination);
            dummy.start(0);
            dummy.disconnect();
            context.close();
            // dispose old context
            context = new AudioCtor();
        }
        return context;
    }

    var audioContext = new createAudioContext();
    var mixGain = audioContext.createGain();
    var filterGain = audioContext.createGain();
    var kickButton = document.querySelector('#kickButton');
    var snareButton = document.querySelector('#snareButton');
    var hihatButton = document.querySelector('#hihatButton');
    var mixButton = document.querySelector('#mixButton');

    //SOUNDS
    mixGain.connect(audioContext.destination);
    mixGain.gain.value = 0;
    filterGain.gain.value = 0;

    //EXAMPLE SOUNDS
    var kickMixGain = audioContext.createGain();
    var kickOsc = audioContext.createOscillator();
    var kickOsc2 = audioContext.createOscillator();
    var kickGainOsc = audioContext.createGain();
    var kickGainOsc2 = audioContext.createGain();

    kickOsc.type = 'triangle';
    kickOsc.frequency.value = 40;
    kickGainOsc.gain.value = 1;
    kickOsc2.type = 'sine';
    kickOsc2.frequency.value = 80;
    kickGainOsc2.gain.value = 1;

    //Connections
    kickOsc.connect(kickGainOsc);
    kickOsc2.connect(kickGainOsc2);
    kickGainOsc2.connect(kickMixGain);
    kickGainOsc.connect(kickMixGain);
    kickMixGain.gain.value = 0;
    kickOsc.start(audioContext.currentTime);
    kickOsc2.start(audioContext.currentTime);

    //VISUALISING THE KICK WAVEFORM
    var canvasHook = document.querySelector('.canvasHook');
    //var canvas = document.getElementById('myCanvas');
    var canvas = document.createElement("canvas");
    canvas.style.width = '600px';
    canvas.style.height = '200px';
    canvas.style.borderRadius = '25px';
    if(canvas != null){
    canvasHook.appendChild(canvas);
    }
    var analyser = audioContext.createAnalyser();
    var WIDTH = 600;
    var HEIGHT = 200;
    kickMixGain.connect(analyser);
    //var canvas = document.querySelector('.visualizer');
    var myCanvas = canvas.getContext('2d');
    analyser.fftSize = 2048;
    var bufferLength = analyser.frequencyBinCount;
    /*an unsigned long value half that of the FFT size. This generally equates to 
the number of data values you will have to play with for the visualization*/
    var dataArray = new Uint8Array(bufferLength);
    myCanvas.clearRect(0, 0, WIDTH, HEIGHT);

    function draw() {

        drawVisual = requestAnimationFrame(draw);
        analyser.getByteTimeDomainData(dataArray);
        myCanvas.fillStyle = 'white';
        myCanvas.fillRect(0, 0, WIDTH, HEIGHT);
        myCanvas.lineWidth = 2;
        myCanvas.strokeStyle = 'black';
        myCanvas.beginPath();

        var sliceWidth = WIDTH * 1 / bufferLength;
        var x = 0;

        for (var i = 0; i < bufferLength; i++) {
            var v = dataArray[i] / 128;
            var y = v * HEIGHT / 2;
            if (i === 0) {
                myCanvas.moveTo(x, y);
            } else {
                myCanvas.lineTo(x, y);
            }
            x += sliceWidth;
        }
        myCanvas.lineTo(WIDTH, HEIGHT / 2);
        myCanvas.stroke();
    }
 
    canvas.addEventListener('mouseover', function() {
        kickMixGain.gain.value = 0.8;
        kickMixGain.connect(audioContext.destination);
       
    });

    canvas.addEventListener('mouseout', function() {
        kickMixGain.gain.value = 0;
    });
  
    draw();

}());
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console