Synth 1 with keys

by Ehsan Ziya

HTML

<script src="http://cdn.jsdelivr.net/keyboardjs/0.4.0/keyboard.js"></script>
<input type="button" value="On" id="button">

JavaScript

$(document).ready(function(){

    var context = new webkitAudioContext();
    var osc = context.createOscillator();
    var gain = context.createGain();
    
    osc.connect(gain);
    gain.connect(context.destination);
    osc.start(0);
    
    gain.gain.value = 0;
    
    var oscStart = function(freq){
    var now = context.currentTime;
    osc.frequency.value = freq;
    gain.gain.cancelScheduledValues( now );
    gain.gain.setValueAtTime(gain.gain.value, now);
    gain.gain.linearRampToValueAtTime(1 , now + 0.1);
    };
    
    var oscOff = function(){
    var now = context.currentTime;
    gain.gain.cancelScheduledValues( now );
    gain.gain.setValueAtTime(gain.gain.value, now);
    gain.gain.linearRampToValueAtTime(0.000 , now + 0.5);
    

    };

    
    KeyboardJS.on( 'a' , function(){
    oscStart(261.626);
    } , oscOff );
    
    KeyboardJS.on( 'w' , function(){
    oscStart(277.183);
    } , oscOff );
    
    KeyboardJS.on( 's' , function(){
    oscStart(293.665);
    } , oscOff );
    
    KeyboardJS.on( 'e' , function(){
    oscStart(311.127);
    } , oscOff );
    
    KeyboardJS.on( 'd' , function(){
    oscStart(329.628);
    } , oscOff );
    
    KeyboardJS.on( 'f' , function(){
    oscStart(349.228);
    } , oscOff );
    
    KeyboardJS.on( 't' , function(){
    oscStart(369.994);
    } , oscOff );
    
    KeyboardJS.on( 'g' , function(){
    oscStart(391.995);
    } , oscOff );
    
    KeyboardJS.on( 'y' , function(){
    oscStart(415.305);
    } , oscOff );
    
    KeyboardJS.on( 'h' , function(){
    oscStart(440.000);
    } , oscOff );
    
    KeyboardJS.on( 'u' , function(){
    oscStart(466.164);
    } , oscOff );
    
    KeyboardJS.on( 'j' , function(){
    oscStart(493.883);
    } , oscOff );
    
    KeyboardJS.on( 'k' , function(){
    oscStart(523.251);
    } , oscOff );
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    








});