5

I'm working on a 3D web app where I want to use positional 3D audio.

I started noticing that a crackling noise appears on the output as the sound source changes its position. Initially I thought it could be a programming issue or a library issue (I was using howler.js).

I made a very basic example based on plain JS and Webaudio API which is shown here

let params={        
        "xPosition":0,
        "zPosition":-1
    };

    let gui=new dat.GUI( { autoPlace: true, width: 500 });       

    const AudioContext = window.AudioContext || window.webkitAudioContext;
    
    let audioCtx;
    let panner;
    let listener;
    let source;
    let osc;
  

    function initWebAudio(){
    
        audioCtx = new AudioContext();
        panner = audioCtx.createPanner();
        listener = audioCtx.listener;

        osc = audioCtx.createOscillator();
        osc.frequency.value = 70;
        osc.connect(panner);
        osc.start(0);


        panner.connect(audioCtx.destination);

        panner.panningModel = 'HRTF';
        panner.distanceModel = 'linear';
        
        panner.maxDistance = 60;
        panner.refDistance = 1;
        panner.rolloffFactor = 1;
        panner.coneInnerAngle = 360;
        panner.coneOuterAngle = 360;
        panner.coneOuterGain = 0;
        
                panner.positionX.setValueAtTime(0,audioCtx.currentTime);
        panner.positionY.setValueAtTime(1,audioCtx.currentTime);
                panner.positionZ.setValueAtTime(1,audioCtx.currentTime);
        
        if(panner.orientationX) {
            panner.orientationX.value = 1;
            panner.orientationY.value = 0;
            panner.orientationZ.value = 0;
        } else {
            panner.setOrientation(1,0,0);
        }

        if(listener.forwardX) {
            listener.forwardX.value = 0;
            listener.forwardY.value = 0;
            listener.forwardZ.value = -1;

            listener.upX.value = 0;
            listener.upY.value = 1;
            listener.upZ.value = 0;
        } else {
            listener.setOrientation(0,0,-1,0,1,0);
        }


        if(listener.positionX) {
            listener.positionX.value = 0;
            listener.positionY.value = 0;
            listener.positionZ.value = 0;
        } else {
            listener.setPosition(0,0,0);
        }

    }

    function positionPanner() {
        
        if(panner.positionX) {
        
            panner.positionX.setValueAtTime(params.xPosition, audioCtx.currentTime);          
            
        } else {
        
            panner.setPosition(params.xPosition,0,params.zPosition);
        }       

    }
    

    function tick(){  
    
        positionPanner();  
        
    }
        
    function onClickStart(){

        initWebAudio();    
        
        gui.add(osc.frequency,"value",50,220).name("frequency");
        
        setInterval(tick,50);

    }
    
    function buildMenu(){

        gui.add(params,"xPosition",-3,3).step(0.001);
        gui.add(window,"onClickStart").name("start");
        
    }

    
    buildMenu();  
    

https://jsfiddle.net/fedeM75/t9vpm8so/23/

Press start, then as you change the xPosition slider a crackling sound appears. It is specially noticeable using headphones.

I searched on google and some people say that it has to do with the rate of change of the position. But I tried with different value ranges and it still happens with small changes.

By the way if the condition to use the panner is to have a very slow rate of change in the position it does not seem to be useful in real world cases.

I use a timer to update the position but the same happens using requestAnimationFrame()

Does anyone has a clue on why this happens and how to solve it?

11
  • 1
    Could you ease/automate the audio changes so they’re not abrupt? E.g. if I were building a play/pause function in an audio player I’d actually do a quick fade in/out to avoid pops and clicks from abrupt changes. Think of the waveform getting cutoff somewhere that isn’t 0… you’ll hear a pop. Jul 21, 2021 at 1:01
  • I tried using another variable as targetValue so the position slowly tries to reach the target value, but the pops are still there. And in real world applications you would be limiting the maximum speed a source can have in 3D space. That's a very important restriction in some cases that makes 3d positional audio un-usable. Imagine an action game were objects need to move very slowly to prevent those pops and clicks ... Jul 21, 2021 at 13:43
  • 1
    No, I'm still looking for a solution. It seems to be a bug, but not exclusive of Chrome, since Firefox shows the same issue Aug 20, 2021 at 22:58
  • 1
    I'm currently trying to find a solution for that. I've created a basic example with a 360deg moving sound: jsfiddle.net/9pbn587L/98 I've tried many things now but didnt find something yet
    – user8232179
    Aug 25, 2021 at 0:58
  • 1
    @NVRM I was actually using howler.js in the first place. As I came across the issue with the panner I realized tha Howler just makes the WebAudio panner node accesible but does not add or change any feature. So I build the simplest possible example based on Webaudio. I tried playing with different update intervals but the problem remains the same. On PC the issue happens in Chrome , Firefox and Edge, but someone told me that on Safari/Mac does not, so may it be an implementation issue. Here I made an even more simple example jsfiddle.net/fedeM75/bqtzcokm/1 Aug 26, 2021 at 13:09

1 Answer 1

1

I had this crackling audio with a simple GainNode. It could be a bug indeed since a simple implementation in the ScriptProcessor does work. Or it's the fact that I/we just don't get how the timing with audioCtx.currentTime should be handled?

For me the solution was to ditch the gain node and use a ScriptProcessor (deprecated: you could/should use an AudioWorkletNode) with my own gain applied to the audio signal directly:

let myGain = 1.0; // change this as you like without crackling noises

let whiteNoise = audioCtx.createScriptProcessor(4096, 0, 1);
whiteNoise.onaudioprocess = function(e) {
    let output = e.outputBuffer.getChannelData(0);
    for (let i = 0; i < output.length; i++) {
        output[i] = (Math.random() * 2 - 1) * myGain;
    }
}

This won't fix your problem directly since you have this problem with the 3D panner, but perhaps you can find sourcecode/examples for such a panner implementation and port it to an AudioWorkletNode? Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.