0

我正在围绕 scriptProcessorNode 振荡器构建课程。我已经将我的onaudioprocess事件处理程序包装在一个函数中Gendy.prototype.process。我可以从这个包装函数中访问全局变量和函数,但不能从onaudioprocess函数中访问它们。

我为这些属性设计了一个解决方法,在包装函数中重新定义它们,但是当尝试使用this.walk().

这是我的代码:

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(); 
            }  
        }
    }

}

这听起来不错,但会返回错误:

Uncaught TypeError: walk is not a function

几行,然后

Uncaught TypeError: undefined is not a function

永远。

这是 scriptProcessorNode 的错误吗?任何见解将不胜感激!

4

2 回答 2

1

scriptProcessorNode 中没有错误,问题在于以下行:

this.scriptNode.onaudioprocess = function(audioProcessingEvent){

默认情况下,内部的this变量onaudioprocess将引用this.scriptNode对象,您可以通过以下两种方式之一处理它:

  • 使用bind(正如您在回答中所做的那样):

    this.scriptNode.onaudioprocess = function(audioProcessingEvent){
        ...
    }.bind(this)
    
  • 使用局部变量来保存 的值this,并使用该局部变量代替this

    var self = this;
    this.scriptNode.onaudioprocess = function(audioProcessingEvent){
        ...
    
于 2015-05-27T02:39:14.643 回答
0

我可以通过附加到函数 this内部来访问它。onaudioprocess.bind(this)

这是代码:

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);
}
于 2015-05-27T02:19:18.107 回答