Web Audio API (3)
code:sample1.js
osc = {}
gain = {}
function keyhandler(e){
var c = e.target.id
if(e.type == 'mousedown'){
gain[${c}0].gain.value = 1.0
/*
osc[${c}0].connect(audioctx.destination);
osc[${c}1].connect(audioctx.destination);
osc[${c}2].connect(audioctx.destination);
osc[${c}3].connect(audioctx.destination);
*/
}
if(e.type == 'mouseup'){
gain[${c}0].gain.value = 0.0
/*
osc[${c}0].disconnect(audioctx.destination);
osc[${c}1].disconnect(audioctx.destination);
osc[${c}2].disconnect(audioctx.destination);
osc[${c}3].disconnect(audioctx.destination);
*/
}
}
function setkey(c,freq){
$('<span>').text(c)
.appendTo($('body'))
.on('mousedown',keyhandler)
.on('mouseup',keyhandler)
.attr('id',c)
$('body').append($('<span> </span>'))
gain[${c}0] = new GainNode(audioctx);
gain[${c}0].gain.value = 0.0
osc[${c}0] = new OscillatorNode(audioctx);
osc[${c}0].frequency.value = freq / 2.0
alert(gain[${c}0])
alert(osc[${c}0].connect)
alert(audioctx.destination)
alert(gain[${c}0].connect(audioctx.destination))
osc[${c}0].connect(gain[${c}0]).connect(audioctx.destination);
alert(100)
osc[${c}0].start();
/*
osc[${c}1] = new OscillatorNode(audioctx);
osc[${c}1].frequency.value = freq
osc[${c}1].start();
osc[${c}2] = new OscillatorNode(audioctx);
osc[${c}2].frequency.value = freq*2
osc[${c}2].start();
osc[${c}3] = new OscillatorNode(audioctx);
osc[${c}3].frequency.value = freq*4
osc[${c}3].start();
*/
}
function setup(){
audioctx = new AudioContext(); // AudioContext を作成
$('<button>').on('click',Start).text('Start').appendTo($('body'))
$('body').append($('<br>'))
}
function Start() {
setkey('A',440)
setkey('As',466)
setkey('B',493.9)
}