Skip to content

Instantly share code, notes, and snippets.

@aturley
Created November 11, 2012 21:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aturley/4056326 to your computer and use it in GitHub Desktop.
Save aturley/4056326 to your computer and use it in GitHub Desktop.
Ring modulation using the web audio api
var wac = new webkitAudioContext();
var ringGain = wac.createGainNode();
ringGain.gain.setValueAtTime(0, 0);
var ringCarrier = wac.createOscillator();
ringCarrier.type = ringCarrier.SINE;
ringCarrier.frequency.setValueAtTime(2500, 0);
var url = "Ring_Modulation-Original_sample.ogg";
var mediaElement = document.createElement("audio");
mediaElement.setAttribute("id", "audioElement");
// mediaElement.setAttribute("src", url);
mediaElement.setAttribute("src", "Ring_Modulation-Original_sample.ogg");
var sourceNode = wac.createMediaElementSource(mediaElement);
ringCarrier.connect(ringGain.gain);
sourceNode.connect(ringGain);
ringGain.connect(wac.destination);
ringCarrier.noteOn(0);
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function() {
wac.decodeAudioData(request.response, function(pianoBuffer) {
var source = wac.createBufferSource();
source.buffer = pianoBuffer;
source.connect(ringGain);
source.noteOn(0);
}, function (e) {});
}
request.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment