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();
};
以下は動作するコードですが、これはaudioApiKeyとbufferFunctionNameを変更することによって行われます
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();
};