UNPKG

4.62 kBTypeScriptView Raw
1import { ToneAudioBuffer } from "../core/context/ToneAudioBuffer.js";
2import { Frequency, MidiNote, NormalRange, Note, Time } from "../core/type/Units.js";
3import { Instrument, InstrumentOptions } from "../instrument/Instrument.js";
4import { ToneBufferSourceCurve } from "../source/buffer/ToneBufferSource.js";
5interface SamplesMap {
6 [note: string]: ToneAudioBuffer | AudioBuffer | string;
7 [midi: number]: ToneAudioBuffer | AudioBuffer | string;
8}
9export interface SamplerOptions extends InstrumentOptions {
10 attack: Time;
11 release: Time;
12 onload: () => void;
13 onerror: (error: Error) => void;
14 baseUrl: string;
15 curve: ToneBufferSourceCurve;
16 urls: SamplesMap;
17}
18/**
19 * Pass in an object which maps the note's pitch or midi value to the url,
20 * then you can trigger the attack and release of that note like other instruments.
21 * By automatically repitching the samples, it is possible to play pitches which
22 * were not explicitly included which can save loading time.
23 *
24 * For sample or buffer playback where repitching is not necessary,
25 * use {@link Player}.
26 * @example
27 * const sampler = new Tone.Sampler({
28 * urls: {
29 * A1: "A1.mp3",
30 * A2: "A2.mp3",
31 * },
32 * baseUrl: "https://tonejs.github.io/audio/casio/",
33 * onload: () => {
34 * sampler.triggerAttackRelease(["C1", "E1", "G1", "B1"], 0.5);
35 * }
36 * }).toDestination();
37 * @category Instrument
38 */
39export declare class Sampler extends Instrument<SamplerOptions> {
40 readonly name: string;
41 /**
42 * The stored and loaded buffers
43 */
44 private _buffers;
45 /**
46 * The object of all currently playing BufferSources
47 */
48 private _activeSources;
49 /**
50 * The envelope applied to the beginning of the sample.
51 * @min 0
52 * @max 1
53 */
54 attack: Time;
55 /**
56 * The envelope applied to the end of the envelope.
57 * @min 0
58 * @max 1
59 */
60 release: Time;
61 /**
62 * The shape of the attack/release curve.
63 * Either "linear" or "exponential"
64 */
65 curve: ToneBufferSourceCurve;
66 /**
67 * @param samples An object of samples mapping either Midi Note Numbers or
68 * Scientific Pitch Notation to the url of that sample.
69 * @param onload The callback to invoke when all of the samples are loaded.
70 * @param baseUrl The root URL of all of the samples, which is prepended to all the URLs.
71 */
72 constructor(samples?: SamplesMap, onload?: () => void, baseUrl?: string);
73 /**
74 * @param samples An object of samples mapping either Midi Note Numbers or
75 * Scientific Pitch Notation to the url of that sample.
76 * @param options The remaining options associated with the sampler
77 */
78 constructor(samples?: SamplesMap, options?: Partial<Omit<SamplerOptions, "urls">>);
79 constructor(options?: Partial<SamplerOptions>);
80 static getDefaults(): SamplerOptions;
81 /**
82 * Returns the difference in steps between the given midi note at the closets sample.
83 */
84 private _findClosest;
85 /**
86 * @param notes The note to play, or an array of notes.
87 * @param time When to play the note
88 * @param velocity The velocity to play the sample back.
89 */
90 triggerAttack(notes: Frequency | Frequency[], time?: Time, velocity?: NormalRange): this;
91 /**
92 * @param notes The note to release, or an array of notes.
93 * @param time When to release the note.
94 */
95 triggerRelease(notes: Frequency | Frequency[], time?: Time): this;
96 /**
97 * Release all currently active notes.
98 * @param time When to release the notes.
99 */
100 releaseAll(time?: Time): this;
101 sync(): this;
102 /**
103 * Invoke the attack phase, then after the duration, invoke the release.
104 * @param notes The note to play and release, or an array of notes.
105 * @param duration The time the note should be held
106 * @param time When to start the attack
107 * @param velocity The velocity of the attack
108 */
109 triggerAttackRelease(notes: Frequency[] | Frequency, duration: Time | Time[], time?: Time, velocity?: NormalRange): this;
110 /**
111 * Add a note to the sampler.
112 * @param note The buffer's pitch.
113 * @param url Either the url of the buffer, or a buffer which will be added with the given name.
114 * @param callback The callback to invoke when the url is loaded.
115 */
116 add(note: Note | MidiNote, url: string | ToneAudioBuffer | AudioBuffer, callback?: () => void): this;
117 /**
118 * If the buffers are loaded or not
119 */
120 get loaded(): boolean;
121 /**
122 * Clean up
123 */
124 dispose(): this;
125}
126export {};