UNPKG

6.92 kBTypeScriptView Raw
1import { Frequency, NormalRange, Time } from "../core/type/Units.js";
2import { RecursivePartial } from "../core/util/Interface.js";
3import { Instrument, InstrumentOptions } from "./Instrument.js";
4import { MembraneSynth, MembraneSynthOptions } from "./MembraneSynth.js";
5import { FMSynth, FMSynthOptions } from "./FMSynth.js";
6import { AMSynth, AMSynthOptions } from "./AMSynth.js";
7import { MonoSynth, MonoSynthOptions } from "./MonoSynth.js";
8import { MetalSynth, MetalSynthOptions } from "./MetalSynth.js";
9import { Monophonic } from "./Monophonic.js";
10import { Synth, SynthOptions } from "./Synth.js";
11type VoiceConstructor<V> = {
12 getDefaults: () => VoiceOptions<V>;
13} & (new (...args: any[]) => V);
14type OmitMonophonicOptions<T> = Omit<T, "context" | "onsilence">;
15type VoiceOptions<T> = T extends MembraneSynth ? MembraneSynthOptions : T extends MetalSynth ? MetalSynthOptions : T extends FMSynth ? FMSynthOptions : T extends MonoSynth ? MonoSynthOptions : T extends AMSynth ? AMSynthOptions : T extends Synth ? SynthOptions : T extends Monophonic<infer U> ? U : never;
16/**
17 * The settable synth options. excludes monophonic options.
18 */
19type PartialVoiceOptions<T> = RecursivePartial<OmitMonophonicOptions<VoiceOptions<T>>>;
20export interface PolySynthOptions<Voice> extends InstrumentOptions {
21 maxPolyphony: number;
22 voice: VoiceConstructor<Voice>;
23 options: PartialVoiceOptions<Voice>;
24}
25/**
26 * PolySynth handles voice creation and allocation for any
27 * instruments passed in as the second parameter. PolySynth is
28 * not a synthesizer by itself, it merely manages voices of
29 * one of the other types of synths, allowing any of the
30 * monophonic synthesizers to be polyphonic.
31 *
32 * @example
33 * const synth = new Tone.PolySynth().toDestination();
34 * // set the attributes across all the voices using 'set'
35 * synth.set({ detune: -1200 });
36 * // play a chord
37 * synth.triggerAttackRelease(["C4", "E4", "A4"], 1);
38 * @category Instrument
39 */
40export declare class PolySynth<Voice extends Monophonic<any> = Synth> extends Instrument<VoiceOptions<Voice>> {
41 readonly name: string;
42 /**
43 * The voices which are not currently in use
44 */
45 private _availableVoices;
46 /**
47 * The currently active voices
48 */
49 private _activeVoices;
50 /**
51 * All of the allocated voices for this synth.
52 */
53 private _voices;
54 /**
55 * The options that are set on the synth.
56 */
57 private options;
58 /**
59 * The polyphony limit.
60 */
61 maxPolyphony: number;
62 /**
63 * The voice constructor
64 */
65 private readonly voice;
66 /**
67 * A voice used for holding the get/set values
68 */
69 private _dummyVoice;
70 /**
71 * The GC timeout. Held so that it could be cancelled when the node is disposed.
72 */
73 private _gcTimeout;
74 /**
75 * A moving average of the number of active voices
76 */
77 private _averageActiveVoices;
78 /**
79 * @param voice The constructor of the voices
80 * @param options The options object to set the synth voice
81 */
82 constructor(voice?: VoiceConstructor<Voice>, options?: PartialVoiceOptions<Voice>);
83 constructor(options?: Partial<PolySynthOptions<Voice>>);
84 static getDefaults(): PolySynthOptions<Synth>;
85 /**
86 * The number of active voices.
87 */
88 get activeVoices(): number;
89 /**
90 * Invoked when the source is done making sound, so that it can be
91 * readded to the pool of available voices
92 */
93 private _makeVoiceAvailable;
94 /**
95 * Get an available voice from the pool of available voices.
96 * If one is not available and the maxPolyphony limit is reached,
97 * steal a voice, otherwise return null.
98 */
99 private _getNextAvailableVoice;
100 /**
101 * Occasionally check if there are any allocated voices which can be cleaned up.
102 */
103 private _collectGarbage;
104 /**
105 * Internal method which triggers the attack
106 */
107 private _triggerAttack;
108 /**
109 * Internal method which triggers the release
110 */
111 private _triggerRelease;
112 /**
113 * Schedule the attack/release events. If the time is in the future, then it should set a timeout
114 * to wait for just-in-time scheduling
115 */
116 private _scheduleEvent;
117 /**
118 * Trigger the attack portion of the note
119 * @param notes The notes to play. Accepts a single Frequency or an array of frequencies.
120 * @param time The start time of the note.
121 * @param velocity The velocity of the note.
122 * @example
123 * const synth = new Tone.PolySynth(Tone.FMSynth).toDestination();
124 * // trigger a chord immediately with a velocity of 0.2
125 * synth.triggerAttack(["Ab3", "C4", "F5"], Tone.now(), 0.2);
126 */
127 triggerAttack(notes: Frequency | Frequency[], time?: Time, velocity?: NormalRange): this;
128 /**
129 * Trigger the release of the note. Unlike monophonic instruments,
130 * a note (or array of notes) needs to be passed in as the first argument.
131 * @param notes The notes to play. Accepts a single Frequency or an array of frequencies.
132 * @param time When the release will be triggered.
133 * @example
134 * const poly = new Tone.PolySynth(Tone.AMSynth).toDestination();
135 * poly.triggerAttack(["Ab3", "C4", "F5"]);
136 * // trigger the release of the given notes.
137 * poly.triggerRelease(["Ab3", "C4"], "+1");
138 * poly.triggerRelease("F5", "+3");
139 */
140 triggerRelease(notes: Frequency | Frequency[], time?: Time): this;
141 /**
142 * Trigger the attack and release after the specified duration
143 * @param notes The notes to play. Accepts a single Frequency or an array of frequencies.
144 * @param duration the duration of the note
145 * @param time if no time is given, defaults to now
146 * @param velocity the velocity of the attack (0-1)
147 * @example
148 * const poly = new Tone.PolySynth(Tone.AMSynth).toDestination();
149 * // can pass in an array of durations as well
150 * poly.triggerAttackRelease(["Eb3", "G4", "Bb4", "D5"], [4, 3, 2, 1]);
151 */
152 triggerAttackRelease(notes: Frequency | Frequency[], duration: Time | Time[], time?: Time, velocity?: NormalRange): this;
153 sync(): this;
154 /**
155 * The release which is scheduled to the timeline.
156 */
157 protected _syncedRelease: (time: number) => this;
158 /**
159 * Set a member/attribute of the voices
160 * @example
161 * const poly = new Tone.PolySynth().toDestination();
162 * // set all of the voices using an options object for the synth type
163 * poly.set({
164 * envelope: {
165 * attack: 0.25
166 * }
167 * });
168 * poly.triggerAttackRelease("Bb3", 0.2);
169 */
170 set(options: RecursivePartial<VoiceOptions<Voice>>): this;
171 get(): VoiceOptions<Voice>;
172 /**
173 * Trigger the release portion of all the currently active voices immediately.
174 * Useful for silencing the synth.
175 */
176 releaseAll(time?: Time): this;
177 dispose(): this;
178}
179export {};