-
Notifications
You must be signed in to change notification settings - Fork 954
/
GreaterThanZero.ts
65 lines (55 loc) · 1.58 KB
/
GreaterThanZero.ts
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
59
60
61
62
63
64
65
import { SignalOperator, SignalOperatorOptions } from "./SignalOperator";
import { Multiply } from "./Multiply";
import { ToneAudioNode } from "../core/context/ToneAudioNode";
import { WaveShaper } from "./WaveShaper";
import { optionsFromArguments } from "../core/util/Defaults";
export type GreaterThanZeroOptions = SignalOperatorOptions
/**
* GreaterThanZero outputs 1 when the input is strictly greater than zero
* @offline 0.1 1
* @example
* const gt0 = new Tone.GreaterThanZero().toDestination();
* const sig = new Tone.Signal(0.5).connect(gt0);
* sig.setValueAtTime(-1, 0.05);
*/
export class GreaterThanZero extends SignalOperator<GreaterThanZeroOptions> {
readonly name: string = "GreaterThanZero";
/**
* The waveshaper
*/
private _thresh: WaveShaper;
/**
* Scale the first thresholded signal by a large value.
* this will help with values which are very close to 0
*/
private _scale: Multiply;
readonly output: ToneAudioNode;
readonly input: ToneAudioNode;
constructor(options?: Partial<GreaterThanZeroOptions>);
constructor() {
super(Object.assign(optionsFromArguments(GreaterThanZero.getDefaults(), arguments)));
this._thresh = this.output = new WaveShaper({
context: this.context,
length: 127,
mapping: (val) => {
if (val <= 0) {
return 0;
} else {
return 1;
}
},
});
this._scale = this.input = new Multiply({
context: this.context,
value: 10000
});
// connections
this._scale.connect(this._thresh);
}
dispose(): this {
super.dispose();
this._scale.dispose();
this._thresh.dispose();
return this;
}
}