JSFiddle - React, Tailwind, and code Playground

by josekerekes

JavaScript

function audioContext() {
	
  if(typeof AudioContext !== "undefined"){
  	return new AudioContext();
  }
  else if(typeof webkitAudioContext !=="undefined"){
  	return new webkitAudioContext();
  }
  else if(typeof mozAudioContext !=="undefined"){
  	return new mozAudioContext();
  }

}

var audioContext = audioContext();

var osc= audioContext.createOscillator();
var oscGain = audioContext.createGain();
osc.type = "sawtooth";
osc.frequency.value = 1000;
osc.detune.value = 50;
osc.connect(oscGain); //routing to gain node
oscGain.gain.value = .1;
oscGain.connect(audioContext.destination); //speakers
osc.start(audioContext.currentTime)
//osc.disconnect()   //will not stop only mute, disconnect from speakers
setTimeout(function(){
	osc.stop()
},500);

//mixed gainnnodes, creating to gain nodes both connected to a third gain node that will be connected to destination

// 4 oscillators : sine, sawtooth, triangle, square, custom