Skip to content

Instantly share code, notes, and snippets.

@whichlight
Created May 6, 2013 01:46
Show Gist options
  • Select an option

  • Save whichlight/5522924 to your computer and use it in GitHub Desktop.

Select an option

Save whichlight/5522924 to your computer and use it in GitHub Desktop.
blue generator , web audio synth 1.
$(document).ready(function(){
$(".start").css("width","100%");
$(".start").css("height","100px");
$(".start").css("background-color","#A6D1FF");
$(".start").css("cursor","pointer");
var playing = false;
try{
var myAudioContext = new webkitAudioContext();
}
catch(err){
alert("this uses the web audio API, try opening it in google chrome \n\n <3 whichlight" );
}
'use strict';
function get_synth(pitch, amp){
var nodes={};
nodes.source = myAudioContext.createOscillator();
nodes.source.type=1;
nodes.filter = myAudioContext.createBiquadFilter();
nodes.volume = myAudioContext.createGainNode();
nodes.filter.type=0; //0 is a low pass filter
nodes.volume.gain.value = amp;
nodes.source.connect(nodes.filter);
//pitch val
nodes.source.frequency.value=pitch;
//frequency val
nodes.filter.frequency.value=1000;
nodes.reverb = myAudioContext.createConvolver();
nodes.filter.connect(nodes.volume);
nodes.volume.connect(nodes.reverb);
nodes.reverb.connect(myAudioContext.destination)
setReverbImpulseResponse('http://thelab.thingsinjars.com/web-audio-tutorial/Church-Schellingwoude.mp3', nodes.reverb, function() {
nodes.source.noteOn(0);
});
return nodes
}
function setReverbImpulseResponse(url, convolver, callback) {
// As with the main sound source,
// the Impulse Response loads asynchronously
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
request.onload = function () {
convolver.buffer = myAudioContext.createBuffer(request.response, false);
callback();
}
request.send();
}
$(".start").click(function(){
if(playing){
$(this).css("background-color","#A6D1FF");
var oscs= [s0,s1,s2,s3,s4,s5, s6];
for(var i in oscs){
oscs[i].source.noteOff(0);
}
}
if(!playing){
$(this).css("background-color","#2900A3");
s0 = get_synth(50, 0.3);
s1 = get_synth(100, 0.3);
s2 = get_synth(100.2, 0.3);
s3 = get_synth(250, 0.2);
s4 = get_synth(250.1, 0.2);
s5 = get_synth(390, 0.1);
s6 = get_synth(392.1, 0.1);
}
playing=!playing;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment