scriptProcessorNode.onaudioprocess not able to access global variables

559 views Asked by At

I am building class around a scriptProcessorNode oscillator. I have wrapped my onaudioprocess event handler in a function Gendy.prototype.process. I can access global variables and functions from within this wrapper function, but they are not accessible from within the onaudioprocess function.

I devised a work around for the properties, to redefine them in the wrapper function, but this doesn't work when trying to call another method, a random walk method, with this.walk().

Here is my code:

Gendy.prototype.process = function(){
    var point = 0;
    var index = 0;
    var y = 0;
    var breakpoint = this.breakpoint;
    var freq = this.freq;

    var walk = this.walk();

    this.scriptNode.onaudioprocess = function(audioProcessingEvent){

        var outputBuffer = audioProcessingEvent.outputBuffer;
        var outputData = outputBuffer.getChannelData(0);

        for(var j = 0; j < outputData.length;j++){
            // linearly interpolate between the new breakpoint positions
            // get the interp point by comparing index to the x distance
            var lerp = (index - breakpoint[point].x) / (breakpoint[point+1].x - breakpoint[point].x);

            y = lerp * (breakpoint[point+1].y - breakpoint[point].y) + breakpoint[point].y;
            if(point < breakpoint.length && index >= breakpoint[point+1].x) {
                point++;
            }

            outputData[j] = y;
            index+=freq; 
            if(index >= breakpoint[breakpoint.length-1].x){
                index = 0;
                point = 0;
                walk(); 
            }  
        }
    }

}

This makes sound, but returns the errors:

Uncaught TypeError: walk is not a function

for few lines and then

Uncaught TypeError: undefined is not a function

forever.

Is this a bug with the scriptProcessorNode? Any insight would be appreciated!

2

There are 2 answers

0
Andrew Bernstein On

I was able to access this from within the onaudioprocess function by attaching .bind(this) to it.

Here is the code:

Gendy.prototype.process = function(){

    this.scriptNode.onaudioprocess = function(audioProcessingEvent){

        var outputBuffer = audioProcessingEvent.outputBuffer;
        var outputData = outputBuffer.getChannelData(0);

        for(var j = 0; j < outputData.length;j++){
            // linearly interpolate between the new breakpoint positions
            // get the interp point by comparing index to the x distance
            var lerp = (this.index - this.breakpoint[this.point].x) / (this.breakpoint[this.point+1].x - this.breakpoint[this.point].x);

            this.y = lerp * (this.breakpoint[this.point+1].y - this.breakpoint[this.point].y) + this.breakpoint[this.point].y;
            if(this.point < this.breakpoint.length && this.index >= this.breakpoint[this.point+1].x) {
                this.point++;
            }

            outputData[j] = this.y;
            this.index+=this.freq; 
            if(this.index >= this.breakpoint[this.breakpoint.length-1].x){
                this.index = 0;
                this.point = 0;
                this.walk(); 
            }  
        }
    }.bind(this);
}
0
mido On

no bug in scriptProcessorNode, the issue is the below line:

this.scriptNode.onaudioprocess = function(audioProcessingEvent){

the this varible inside the onaudioprocess would refer to this.scriptNode object by default, you can handle it in one of two ways:

  • Use bind( as you have done in your answer):

    this.scriptNode.onaudioprocess = function(audioProcessingEvent){
        ...
    }.bind(this)
    
  • use a local variable to hold the value of this, and use that local variable in place of this:

    var self = this;
    this.scriptNode.onaudioprocess = function(audioProcessingEvent){
        ...