Skip to content

Commit

Permalink
Fixed webkitAudioContext patch. Refs #38
Browse files Browse the repository at this point in the history
The patch actually broke newer Safari implementation.
  • Loading branch information
sunyatasattva committed Apr 11, 2016
1 parent 80ef90f commit d1fc7c3
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions assets/js/lib/webkit-audiocontext-patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,54 @@
* Patch to port old implementation of `webkitAudioContext`
* to standards based `audioContext`.
*
* Props to Lajos György Mészáros <[email protected]> for this patch.
* Props to Lajos György Mészáros <[email protected]> for this patch,
* although some edits were needed to make it work.
* @see https://github.com/meszaros-lajos-gyorgy/meszaros-lajos-gyorgy.github.io/blob/master/microtonal/monochord/js/webkit-audio-context-patch.js
*/

/* globals OscillatorNode */

"use strict";

module.exports = function(){
var AudioContext,
OscillatorNode,
oldStart,
oldStop;

if( !window.hasOwnProperty("AudioContext") &&
window.hasOwnProperty("webkitAudioContext") ){
var a = window.AudioContext = window.webkitAudioContext;
AudioContext = window.AudioContext = window.webkitAudioContext;
OscillatorNode = window.OscillatorNode;

if(!a.prototype.hasOwnProperty("createGain")){
a.prototype.createGain = a.prototype.createGainNode;
if(!AudioContext.prototype.hasOwnProperty("createGain")){
AudioContext.prototype.createGain = AudioContext.prototype.createGainNode;
}

if(!OscillatorNode.prototype.hasOwnProperty("start")){
OscillatorNode.prototype.start = OscillatorNode.prototype.noteOn;
}

// make the first parameter optional for firefox <30
var oldStart = OscillatorNode.prototype.start;
OscillatorNode.prototype.start = function(t){
oldStart(t || 0);
oldStart = OscillatorNode.prototype.start;
OscillatorNode.prototype.start = function(t = 0){
oldStart.call(this, t);
};

if(!OscillatorNode.prototype.hasOwnProperty("stop")){
OscillatorNode.prototype.stop = OscillatorNode.prototype.noteOff;
}

// make the first parameter optional for firefox <30
var oldStop = OscillatorNode.prototype.stop;
OscillatorNode.prototype.stop = function(t){
oldStop(t || 0);
oldStop = OscillatorNode.prototype.stop;
OscillatorNode.prototype.stop = function(t = 0){
oldStop.call(this, t);
};

Object.defineProperty(OscillatorNode.prototype, "type", {
get : function(){
get: function() {
return ["sine", "square", "sawtooth", "triangle", "custom"][this.type];
},
set : function(type){
this.type = OscillatorNode.prototype[type.toUpperCase()];
set: function(type) {
this.type = OscillatorNode.prototype[type.toUpperCase()] || type;
}
});
}
Expand Down

0 comments on commit d1fc7c3

Please sign in to comment.