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ます。次の 2 つの方法のいずれかで処理できます。

  • 使用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 に答える