使用按钮功能切换WebAudio API节点分配时遇到问题

时间:2012-11-28 16:02:51

标签: javascript web-audio

下面我有一个名为 buttonClickResult 的功能,在用户点击图片时启动。我想点击/关闭以在两个值之间重新分配名为 distortionSwitch 的变量。一个叫做 emptyCable ,另一个称为 overdrive 。此变量位于名为 bufferFunctionName

的函数内

我不太清楚如何将此变量赋值传递给另一个函数。

总结当用户点击按钮时, bufferFunctionName 中名为 distortionSwitch 的变量应该在名为 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 无法访问 distortionSwitch ,即使它的作用范围应该是(我认为?)

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

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,我认为你想要做的就是移动这一行:

var distortionSwitch = overdrive;

并将其放在首位

var humSnd = new Audio('audio/hum.wav');
var button = document.getElementById('btn1');
var distortionSwitch = overdrive;

这使得按钮单击侦听器中的distortionSwitch可用。然后你做

if (button.className == "off") {
   button.className = "on";
   button.src = 'imgs/on.png';
   distortionSwitch = overdrive;
   playClickSound('audio/click.wav');
   humSnd.play();
   humSnd.loop = true;
 } else if (button.className == "on") {
   button.className = "off";       
   button.src = 'imgs/off.png';
   distortionSwitch = emptyCable;
   playClickSound('audio/click.wav');
   humSnd.pause();
 }