UNPKG

3.79 kBJavaScriptView Raw
1import { AmplitudeEnvelope } from "../component/envelope/AmplitudeEnvelope.js";
2import { omitFromObject, optionsFromArguments } from "../core/util/Defaults.js";
3import { Noise } from "../source/Noise.js";
4import { Instrument } from "./Instrument.js";
5import { ToneAudioNode, } from "../core/context/ToneAudioNode.js";
6import { Envelope } from "../component/envelope/Envelope.js";
7import { Source } from "../source/Source.js";
8/**
9 * Tone.NoiseSynth is composed of {@link Noise} through an {@link AmplitudeEnvelope}.
10 * ```
11 * +-------+ +-------------------+
12 * | Noise +>--> AmplitudeEnvelope +>--> Output
13 * +-------+ +-------------------+
14 * ```
15 * @example
16 * const noiseSynth = new Tone.NoiseSynth().toDestination();
17 * noiseSynth.triggerAttackRelease("8n", 0.05);
18 * @category Instrument
19 */
20export 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 // connect the noise to the output
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 * Start the attack portion of the envelopes. Unlike other
47 * instruments, Tone.NoiseSynth doesn't have a note.
48 * @example
49 * const noiseSynth = new Tone.NoiseSynth().toDestination();
50 * noiseSynth.triggerAttack();
51 */
52 triggerAttack(time, velocity = 1) {
53 time = this.toSeconds(time);
54 // the envelopes
55 this.envelope.triggerAttack(time, velocity);
56 // start the noise
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 * Start the release portion of the envelopes.
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 * Trigger the attack and then the release after the duration.
83 * @param duration The amount of time to hold the note for
84 * @param time The time the note should start
85 * @param velocity The volume of the note (0-1)
86 * @example
87 * const noiseSynth = new Tone.NoiseSynth().toDestination();
88 * // hold the note for 0.5 seconds
89 * noiseSynth.triggerAttackRelease(0.5);
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//# sourceMappingURL=NoiseSynth.js.map
\No newline at end of file