0

snareDrum変数のsliderVal引数スロットにHTML入力範囲スライダー値を動的に取得したい。

document.getElementById('slider').addEventListener('change', function() {
var sliderVal = document.getElementById("slider").value;

// Event handler code....I tried a bunch of different things


}); 


// Don't know how to get it to the sliderVal  argument.

var snareDrum = new audioApiKey("snareDrum","snare.mp3",false, sliderVal); 









// Abstracted Web Audio playback Block


var context = new webkitAudioContext();
 function audioApiKey(domNode,fileDirectory,loopOnOff,playBackSpeed) {
    this.domNode = domNode;
    this.fileDirectory = fileDirectory;
    this.playBackSpeed = playBackSpeed;
    var bufferFunctionName = bufferFunctionName;
    var incomingBuffer;
    var savedBuffer;
    var xhr;

       bufferFunctionName = function () {          
       var source = context.createBufferSource();
       source.buffer = savedBuffer;
       source.loop = loopOnOff;
       source.playbackRate.value = playBackSpeed;
       source.connect(context.destination);
       source.noteOn(0); // Play sound immediately
       };
    var xhr = new XMLHttpRequest();
    xhr.open('get',fileDirectory, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function () {
    context.decodeAudioData(xhr.response,
       function(incomingBuffer) {
       savedBuffer = incomingBuffer;
       var note = document.getElementById(domNode);
       note.addEventListener("click", bufferFunctionName , false);
         }
      );
   };
 xhr.send();
 };

以下は動作するコードですが、これはaudioApiKeybufferFunctionNameを変更することによって行われます

var snareDrumPitchSlider = document.getElementById("snareDrumPitchSlider");
var snareDrum = new audioApiKey("snareDrum","snare.mp3",false,"snareDrumPitchSlider");


// Abstracted Web Audio playback Block

var context = new webkitAudioContext();

 function audioApiKey(domNode,fileDirectory,loopOnOff,pitchSlider) {
    this.domNode = domNode;
    this.fileDirectory = fileDirectory;
    this.pitchSlider = pitchSlider;
    var bufferFunctionName = bufferFunctionName;
    var incomingBuffer;
    var savedBuffer;
    var xhr;

       bufferFunctionName = function () {          
       var source = context.createBufferSource();
       source.buffer = savedBuffer;
       source.loop = loopOnOff;
       var speed = document.getElementById(pitchSlider).value; // Added
       source.playbackRate.value = speed;
       source.connect(context.destination);
       source.noteOn(0); // Play sound immediately

       };

    var xhr = new XMLHttpRequest();
    xhr.open('get',fileDirectory, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function () {
    context.decodeAudioData(xhr.response,
       function(incomingBuffer) {
       savedBuffer = incomingBuffer;
       var note = document.getElementById(domNode);
       note.addEventListener("click", bufferFunctionName , false);



         }

      );
   };
 xhr.send();
 };
4

1 に答える 1

2

見た目からして、これは範囲の問題です。次のように、sliderVal の宣言をリスナーの外に移動してみてください。

var sliderVal; //feel free to set it to a default
document.getElementById('slider').addEventListener('change', function() {
    sliderVal = document.getElementById("slider").value;
    // Event handler code....
}); 


// sliderVal should now be accessible
var snareDrum = new audioApiKey("snareDrum","snare.mp3",false, sliderVal);
于 2013-03-13T17:23:41.643 に答える