以下に、ユーザーが画像をクリックすると起動されるbuttonClickResultという関数があります。クリックのオン/オフで、 distortionSwitchという変数を 2 つの値の間で再割り当てしたいと思います。1 つはemptyCableと呼ばれ、もう 1 つは overdrive と呼ばれます。この変数は、bufferFunctionNameという関数内にあります。
この変数の割り当てを他の関数に渡す方法がよくわかりません。
要約すると、ユーザーがボタンをクリックしたときに、bufferFunctionName内の歪みスイッチ と呼ばれる変数は、 emptyCableと呼ばれる変数の割り当てとオーバードライブと呼ばれる変数の割り当てを切り替える必要があります。
ありがとうございました。
var humSnd = new Audio('audio/hum.wav');
var button = document.getElementById('btn1');
buttonClickResult = function () {
function playClickSound(filename) {
var snd = new Audio(filename);
snd.play();
}
button.onmousedown = function buttonClicked() {
if (button.className == "off") {
button.className = "on";
button.src = 'imgs/on.png';
playClickSound('audio/click.wav');
humSnd.play();
humSnd.loop = true;
} else if (button.className == "on") {
button.className = "off";
button.src = 'imgs/off.png';
playClickSound('audio/click.wav');
humSnd.pause();
}
}
};
buttonClickResult();
var context = new webkitAudioContext();
// Start Of Web Audio API Buffer & Playback Abstraction
function audioApiKey(domNode, fileDirectory, bufferFunctionName, incomingBuffer, savedBuffer, xhr) {
this.domNode = domNode;
this.fileDirectory = fileDirectory;
this.bufferFunctionName = bufferFunctionName;
this.incomingBuffer = incomingBuffer;
this.savedBuffer = savedBuffer;
this.xhr = xhr;
bufferFunctionName = function () {
var distortionSwitch = overdrive; // THIS SHOULD TOGGLE
var source = context.createBufferSource();
source.buffer = savedBuffer;
source.connect(distortionSwitch.input);
distortionSwitch.connect(delay.input);
delay.connect(convolver.input);
convolver.connect(context.destination);
source.noteOn(0); // Play sound immediately
};
補遺 の下にコードを書くと、bufferFunctionNameは、本来あるべき方法でスコープされているにもかかわらず、歪曲スイッチにアクセスできません(私は思いますか?)
var distortionSwitch = overdrive;
bufferFunctionName = function () {
var source = context.createBufferSource();
source.buffer = savedBuffer;
source.connect(distortionSwitch.input);
distortionSwitch.connect(delay.input);
delay.connect(convolver.input);
convolver.connect(context.destination);
source.noteOn(0); // Play sound immediately
};
このように書くとうまくいきます
bufferFunctionName = function () {
var distortionSwitch = overdrive;
var source = context.createBufferSource();
source.buffer = savedBuffer;
source.connect(distortionSwitch.input);
distortionSwitch.connect(delay.input);
delay.connect(convolver.input);
convolver.connect(context.destination);
source.noteOn(0); // Play sound immediately
};