Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9319855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:29:16+00:00 2026-06-19T03:29:16+00:00

I’m trying to create a graphic equalizer type visualization for HTML5 audio – Chrome

  • 0

I’m trying to create a graphic equalizer type visualization for HTML5 audio – Chrome only at this point, using webkitAudioContext.

I’m finding unusual and unpredictable behaviour when I try to change the source of the audio i.e. to play a different song. I read somewhere I should wait until the “canplay” event on the audio is triggered before connecting it to the context / analyser:

var context, sourceNode, analyser, javascriptNode, audio;

var ctx = $("#songcanvas").get()[0].getContext("2d");    


function loadSong(url) { 
    if (audio!=undefined) { audio.pause(); }
    audio = new Audio();
    audio.src = url;
    audio.addEventListener("canplay", function(e) {
         setupAudioNodes();
    }, false);
}

function setupAudioNodes() {
        context = new webkitAudioContext();
        javascriptNode = context.createJavaScriptNode(2048, 1, 1);
        javascriptNode.connect(context.destination);

        analyser = context.createAnalyser();
        analyser.smoothingTimeConstant = 0.3;
        analyser.fftSize = 512;

        sourceNode = context.createMediaElementSource(audio);
        sourceNode.connect(analyser);
        analyser.connect(javascriptNode);

        sourceNode.connect(context.destination);

        javascriptNode.onaudioprocess = function() {
            var array =  new Uint8Array(analyser.frequencyBinCount);
            analyser.getByteFrequencyData(array);
            ctx.clearRect(0, 0, 1000, 325);
            ctx.fillStyle="rgba(32, 45, 21,1)";
            drawSpectrum(array);
        }
        audio.play();
}

function drawSpectrum(array) {
    for ( var i = 0; i < (array.length); i++ ){
        var value = array[i];
        ctx.fillRect(i*5,325-value,3,325);
    }
};

The first three or four times I change the source, it works, then eventually fails with “Uncaught SyntaxError: audio resources unavailable for AudioContext construction”

Full demo here http://jsfiddle.net/eAgQN/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:29:17+00:00Added an answer on June 19, 2026 at 3:29 am

    I’d recommend staying away from creating a JavaScriptNode if you’re simply going to be drawing the spectrum. Instead try utilizing the requestAnimationFrame method as it will save CPU cycles and will get you closer to a constant 60fps (is especially useful if your window/tab is in the background as any function wrapped in a requestAnimationFrame will wait until the window is in focus again before firing).

    The reason you’re probably getting that error is because A) you can only create a single AudioContext per window, B) you should be disconnecting the MediaElementSource when you’re finished using it, and C) you should also be removing the actual Audio element too.

    Here’s a your demo with the changes I outlined above: http://jsbin.com/acolet/1/

    window.AudioContext = window.AudioContext || window.webkitAudioContext;
    var context = new AudioContext(),
        audioAnimation, sourceNode, analyser, audio,
        songs  = document.getElementById('songs'),
        canvas = document.getElementById('songcanvas'),
        WIDTH  = canvas.width,
        HEIGHT = canvas.height,
        // get the context from the canvas to draw on
        ctx = canvas.getContext('2d'),
        gradient = ctx.createLinearGradient(0, 0, 0, HEIGHT),
        bar = { width: 2, gap: 2, ratio: HEIGHT / 256 };
    
    gradient.addColorStop(1.00,'#000000');
    gradient.addColorStop(0.75,'#ff0000');
    gradient.addColorStop(0.25,'#ffff00');
    gradient.addColorStop(0.00,'#ffff00');
    ctx.fillStyle = gradient;
    
    songs.addEventListener('click', loadSong, false);
    
    function loadSong(e) {
      e.preventDefault();
      var url = e.target.href;
      if (!url) return false;
      if (audio) audio.remove();
      if (sourceNode) sourceNode.disconnect();
      cancelAnimationFrame(audioAnimation);
      audio = new Audio();
      audio.src = url;
      audio.addEventListener('canplay', setupAudioNodes, false);
    }
    
    function setupAudioNodes() {
      analyser = (analyser || context.createAnalyser());
      analyser.smoothingTimeConstant = 0.8;
      analyser.fftSize = 512;
    
      sourceNode = context.createMediaElementSource(audio);
      sourceNode.connect(analyser);
      sourceNode.connect(context.destination);
      
      audio.play();
      drawSpectrum();
    }
    
    function drawSpectrum() {
      var freq = new Uint8Array(analyser.frequencyBinCount);
      analyser.getByteFrequencyData(freq);
      ctx.clearRect(0, 0, WIDTH, HEIGHT);
      audioAnimation = requestAnimationFrame(drawSpectrum);
      for ( var i = freq.length - 1; i >= 0 ; i-- ){
        var x = i * (bar.width + bar.gap);
        var y = HEIGHT - (freq[i] * bar.ratio);
        ctx.fillRect(x, y, bar.width, HEIGHT);
      }
    }
    body { font-family: sans-serif; background-color: #000; }
    a { color: #fff; text-decoration: none; }
    ul { padding: 20px 0; width: 100px; }
    ul, canvas { float: left; }
    <ul id="songs">
      <li><a class="song" href="http://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg">ACDC</a></li>
      <li><a class="song" href="http://upload.wikimedia.org/wikipedia/en/9/9f/Sample_of_%22Another_Day_in_Paradise%22.ogg">Phil Collins</a></li>
      <li><a class="song" href="http://upload.wikimedia.org/wikipedia/en/1/1f/%22Layla%22%2C_Derek_and_the_Dominos_song_%28sample%29.ogg">Clapton</a></li>
    </ul>
    <canvas id="songcanvas" width="400" height="128"></canvas>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.