forked from Tonejs/Tone.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaveShaperNode.js
58 lines (48 loc) · 1.45 KB
/
WaveShaperNode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
define(["Tone/core/Tone", "Tone/shim/AudioContext"], function(Tone){
if (Tone.supported){
//fixes safari only bug which is still present in 11
var ua = navigator.userAgent.toLowerCase();
var isSafari = ua.includes("safari") && !ua.includes("chrome");
if (isSafari){
var WaveShaperNode = function(context){
this._internalNode = this.input = this.output = context._native_createWaveShaper();
this._curve = null;
for (var prop in this._internalNode){
this._defineProperty(this._internalNode, prop);
}
};
Object.defineProperty(WaveShaperNode.prototype, "curve", {
get : function(){
return this._curve;
},
set : function(curve){
this._curve = curve;
var array = new Float32Array(curve.length+1);
array.set(curve, 1);
array[0] = curve[0];
this._internalNode.curve = array;
}
});
WaveShaperNode.prototype._defineProperty = function(context, prop){
if (Tone.isUndef(this[prop])){
Object.defineProperty(this, prop, {
get : function(){
if (typeof context[prop] === "function"){
return context[prop].bind(context);
} else {
return context[prop];
}
},
set : function(val){
context[prop] = val;
}
});
}
};
AudioContext.prototype._native_createWaveShaper = AudioContext.prototype.createWaveShaper;
AudioContext.prototype.createWaveShaper = function(){
return new WaveShaperNode(this);
};
}
}
});