Skip to content

Instantly share code, notes, and snippets.

@dmgig
Created April 20, 2016 18:46
Show Gist options
  • Save dmgig/e4f9e832dc6317791bd920ba5153ec5b to your computer and use it in GitHub Desktop.
Save dmgig/e4f9e832dc6317791bd920ba5153ec5b to your computer and use it in GitHub Desktop.
Messy messing with the browser's AudioContext capabilities...
<script type="application/javascript">
// create web audio api context
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// create Oscillator and gain node
// connect oscillator to gain node to speakers
var initialFreq = 400;
var initialVol = 0.001;
// set options for the oscillator
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
oscillator.type = 'sine';
oscillator.frequency.value = initialFreq; // value in hertz
oscillator.detune.value = 50; // value in cents
gainNode.gain.value = initialVol;
oscillator.frequency.value = 2500;
gainNode.gain.value = .01;
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
oscillator.start(0);
function rand(a, z){
return Math.floor((Math.random() * z) + a);
}
function oneIn(x){
return Math.floor(Math.random() * 100) % x == 0;
}
var last = 1;
var types = ['sine','sine','square','sawtooth'];
var prev = 800;
function setFreq()
{
oscillator.type = types[rand(0, (types.length-1))];
gainNode.gain.value = .02;
var anew = rand(prev, 800);
if(anew < 200) anew = 200;
if(anew > 800) anew = 800;
oscillator.frequency.value = anew / last;
prev = anew;
if(oneIn(6)){
oscillator2.frequency.value = oscillator.frequency.value / rand(3, 8);// your code here
oscillator.type = 'triangle';
gainNode.gain.value = .008;
oscillator.detune.value = 150;
}else{
oscillator.frequency.value = oscillator2.frequency.value / rand(1, 4);// your code here
}
myLoop();
setTimeout(setFreq, rand(1500, 3500));
}
function myLoop (type) { // create a loop function
oscillator.detune.value = last * 25;
if(type == 'pos'){
gainNode.gain.value = gainNode.gain.value + .0002;
last = last + .25;
var newval = oscillator.frequency.value / last;
}else{
gainNode.gain.value = gainNode.gain.value - .0005;
last = last - .5;
var newval = oscillator.frequency.value * .5;
}
if(newval < 200) newval = 200;
if(newval > 1000) newval = 1000;
oscillator.frequency.value = newval;
if(last >= 12) last = 1;
if(last <= 1 ){
last = 12;
oscillator.frequency.value * prev;
}
setTimeout(function () {
if (gainNode.gain.value < -.035) {
myLoop('pos');
}else if (gainNode.gain.value > .035){
myLoop('neg');
}else{
myLoop('neg');
}// .. setTimeout()
}, rand(100,400))
}
var oscillator2 = audioCtx.createOscillator();
var gainNode2 = audioCtx.createGain();
oscillator2.type = 'square';
oscillator2.frequency.value = initialFreq; // value in hertz
oscillator2.detune.value = 100; // value in cents
oscillator2.frequency.value = 2500;
gainNode2.gain.value = .01;
oscillator2.connect(gainNode2);
gainNode2.connect(audioCtx.destination);
oscillator2.start(0);
var prev2 = 400;
function setFreq2(){
oscillator2.detune.value = last * 50;
oscillator2.type = types[rand(0, (types.length-1))];
gainNode2.gain.value = rand(.001,.02);
var anew2 = rand(200, 400);
oscillator2.frequency.value = anew2;
if(oneIn(4)) oscillator2.frequency.value = oscillator.frequency.value / 2;// your code here
else myLoop2();
prev2 = anew2;
setTimeout(setFreq2, rand(5000, 10000));
}
function myLoop2 (type) { // create a loop function
if(type == 'neg'){
gainNode2.gain.value = gainNode2.gain.value - .0001;
last = last - 2;
var newval = oscillator.frequency.value / last;
oscillator2.frequency.value = oscillator2.frequency.value / last;
}else{
gainNode2.gain.value = gainNode2.gain.value + .0001;
last = last + 2;
var newval = oscillator.frequency.value / last;
}
if(newval > 400) newval = 400;
if(newval < 100) newval = 100;
oscillator2.frequency.value = newval;
setTimeout(function () {
if (gainNode2.gain.value > .03) {
myLoop2('neg');
}else if (gainNode2.gain.value < -.03){
myLoop2('pos');
}else{
myLoop2('neg');
}
}, rand(10,100))
}
var oscillator3 = audioCtx.createOscillator();
var gainNode3 = audioCtx.createGain();
oscillator3.type = 'sine';
oscillator3.frequency.value = initialFreq; // value in hertz
oscillator3.detune.value = 100; // value in cents
oscillator3.frequency.value = 2500;
gainNode3.gain.value = .01;
oscillator3.connect(gainNode3);
gainNode3.connect(audioCtx.destination);
oscillator3.start(0);
function setFreq3(){
//if(oneIn(2)){
if(oscillator2.frequency.value / 2 < 100){
oscillator3.frequency.value = oscillator2.frequency.value * rand(2,3);// your code here
}else{
oscillator3.frequency.value = oscillator.frequency.value / rand(2,5);// your code here
}
//}
setTimeout(setFreq3, rand(500, 1270));
}
setFreq3();
setFreq2();
setFreq();
</script>
<h1>Hello World!</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment