0

I've been looking at the information here:

Is there a way get something like decibel levels from an audio file and transform that information into a json array?

But when I try to run the JSBin here:

<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8 />
    <title>Get Audio Data</title>
    <link rel="stylesheet" type="text/css" href="index.css">
    <script type="text/javascript" src="music.js"></script>
  </head>
  <body>
    <div id=meter></div>
  </body>
</html>

#meter {
    width: 500px;
    height: 15px;
    margin: 2px 0;
    background: green;
    -webkit-transition;
    width .05s;
}

function() {
    var ctx = new webkitAudioContext(),
    url = 'test.mp3',
    audio = new Audio(url),
    processor = ctx.createJavaScriptNode(2048, 1, 1),
    meter = document.getElementById('meter'),
    source;

    audio.addEventListener('canplaythrough', function() {
        source = ctx.createMediaElementSource(audio);
        source.connect(processor);
        source.connect(ctx.destination);
        processor.connect(ctx.destination);
        audio.play();
    }, false);

    processor.onaudioprocess = function(evt) {
        var input = evt.inputBuffer.getChannelData(0),
        len = input.length,
        total = i = 0,
        rms;
        while(i<len)
            total += Math.abs(input[i++]);
        rms = Math.sqrt(total/len);
        meter.style.width = (rms*100) + '%';
    };
};

I'm not really sure what it's doing. Can anyone give me more info?

Community
  • 1
  • 1
Sonofblip
  • 1,167
  • 3
  • 12
  • 20
  • I'm not chasing round the Internet looking for your code. Please take a look at [ask] –  Nov 21 '14 at 21:41
  • Clicking a single link doesn't really constitute chasing around the internet... But I'll edit the question. – Sonofblip Nov 21 '14 at 21:52
  • Are you just asking for someone to explain what the code is doing or are you expecting it should be doing something that it is not? Your question isn't very clear. – jaket Nov 21 '14 at 22:07

1 Answers1

3

Your code does generate RMS output once you replace defunct API calls

old    webkitAudioContext
new    


try {

    window.AudioContext = window.AudioContext ||
        window.webkitAudioContext ||
        window.mozAudioContext ||
        window.oAudioContext ||
        window.msAudioContext;

    ctx = new AudioContext();

    console.log("cool audio context established ... ctx ");

} catch (e) {

    alert("Web Audio API is not supported by this browser\n ... http://caniuse.com/#feat=audio-api");
}

and

old    createJavaScriptNode
new    createScriptProcessor
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104