1 | import { AmplitudeEnvelope } from "../component/envelope/AmplitudeEnvelope.js";
|
2 | import { omitFromObject, optionsFromArguments } from "../core/util/Defaults.js";
|
3 | import { Noise } from "../source/Noise.js";
|
4 | import { Instrument } from "./Instrument.js";
|
5 | import { ToneAudioNode, } from "../core/context/ToneAudioNode.js";
|
6 | import { Envelope } from "../component/envelope/Envelope.js";
|
7 | import { Source } from "../source/Source.js";
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export class NoiseSynth extends Instrument {
|
21 | constructor() {
|
22 | const options = optionsFromArguments(NoiseSynth.getDefaults(), arguments);
|
23 | super(options);
|
24 | this.name = "NoiseSynth";
|
25 | this.noise = new Noise(Object.assign({
|
26 | context: this.context,
|
27 | }, options.noise));
|
28 | this.envelope = new AmplitudeEnvelope(Object.assign({
|
29 | context: this.context,
|
30 | }, options.envelope));
|
31 |
|
32 | this.noise.chain(this.envelope, this.output);
|
33 | }
|
34 | static getDefaults() {
|
35 | return Object.assign(Instrument.getDefaults(), {
|
36 | envelope: Object.assign(omitFromObject(Envelope.getDefaults(), Object.keys(ToneAudioNode.getDefaults())), {
|
37 | decay: 0.1,
|
38 | sustain: 0.0,
|
39 | }),
|
40 | noise: Object.assign(omitFromObject(Noise.getDefaults(), Object.keys(Source.getDefaults())), {
|
41 | type: "white",
|
42 | }),
|
43 | });
|
44 | }
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | triggerAttack(time, velocity = 1) {
|
53 | time = this.toSeconds(time);
|
54 |
|
55 | this.envelope.triggerAttack(time, velocity);
|
56 |
|
57 | this.noise.start(time);
|
58 | if (this.envelope.sustain === 0) {
|
59 | this.noise.stop(time +
|
60 | this.toSeconds(this.envelope.attack) +
|
61 | this.toSeconds(this.envelope.decay));
|
62 | }
|
63 | return this;
|
64 | }
|
65 | |
66 |
|
67 |
|
68 | triggerRelease(time) {
|
69 | time = this.toSeconds(time);
|
70 | this.envelope.triggerRelease(time);
|
71 | this.noise.stop(time + this.toSeconds(this.envelope.release));
|
72 | return this;
|
73 | }
|
74 | sync() {
|
75 | if (this._syncState()) {
|
76 | this._syncMethod("triggerAttack", 0);
|
77 | this._syncMethod("triggerRelease", 0);
|
78 | }
|
79 | return this;
|
80 | }
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | triggerAttackRelease(duration, time, velocity = 1) {
|
92 | time = this.toSeconds(time);
|
93 | duration = this.toSeconds(duration);
|
94 | this.triggerAttack(time, velocity);
|
95 | this.triggerRelease(time + duration);
|
96 | return this;
|
97 | }
|
98 | dispose() {
|
99 | super.dispose();
|
100 | this.noise.dispose();
|
101 | this.envelope.dispose();
|
102 | return this;
|
103 | }
|
104 | }
|
105 |
|
\ | No newline at end of file |