Tone.js/Tone/shim/WaveShaperNode.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

define(["../core/Tone", "../shim/AudioContext"], function(Tone){
2017-12-29 17:02:40 +00:00
if (Tone.supported && !Tone.global.AudioContext.prototype._native_createWaveShaper){
2017-12-29 17:02:40 +00:00
//fixes safari only bug which is still present in 11
var ua = navigator.userAgent.toLowerCase();
2018-01-22 21:21:50 +00:00
var isSafari = ua.includes("safari") && !ua.includes("chrome");
2017-12-29 17:02:40 +00:00
if (isSafari){
var WaveShaperNode = function(context){
this._internalNode = this.input = this.output = context._native_createWaveShaper();
this._curve = null;
for (var prop in this._internalNode){
2017-12-29 17:20:51 +00:00
this._defineProperty(this._internalNode, prop);
2017-12-29 17:02:40 +00:00
}
};
Object.defineProperty(WaveShaperNode.prototype, "curve", {
"get" : function(){
2017-12-29 17:02:40 +00:00
return this._curve;
},
"set" : function(curve){
2017-12-29 17:02:40 +00:00
this._curve = curve;
var array = new Float32Array(curve.length+1);
array.set(curve, 1);
array[0] = curve[0];
this._internalNode.curve = array;
}
});
2017-12-29 17:20:51 +00:00
WaveShaperNode.prototype._defineProperty = function(context, prop){
if (Tone.isUndef(this[prop])){
Object.defineProperty(this, prop, {
"get" : function(){
2017-12-29 17:02:40 +00:00
if (typeof context[prop] === "function"){
return context[prop].bind(context);
} else {
return context[prop];
}
},
"set" : function(val){
2017-12-29 17:02:40 +00:00
context[prop] = val;
}
});
}
2017-12-29 17:26:51 +00:00
};
2017-12-29 17:02:40 +00:00
Tone.global.AudioContext.prototype._native_createWaveShaper = Tone.global.AudioContext.prototype.createWaveShaper;
Tone.global.AudioContext.prototype.createWaveShaper = function(){
2017-12-29 17:02:40 +00:00
return new WaveShaperNode(this);
};
}
}
});