diff --git a/Tone/instrument/Sampler.ts b/Tone/instrument/Sampler.ts index 9efb1f480..7e636b4ed 100644 --- a/Tone/instrument/Sampler.ts +++ b/Tone/instrument/Sampler.ts @@ -24,6 +24,9 @@ export interface SamplerOptions extends InstrumentOptions { baseUrl: string; curve: ToneBufferSourceCurve; urls: SamplesMap; + loop?: boolean; + loopStart?: Time; + loopEnd?: Time; } /** @@ -83,6 +86,11 @@ export class Sampler extends Instrument { */ curve: ToneBufferSourceCurve; + loop: boolean; + + loopStart: Time; + loopEnd: Time | undefined; + /** * @param samples An object of samples mapping either Midi Note Numbers or * Scientific Pitch Notation to the url of that sample. @@ -126,6 +134,9 @@ export class Sampler extends Instrument { this.attack = options.attack; this.release = options.release; this.curve = options.curve; + this.loop = options.loop || false; + this.loopStart = options.loopStart || 0; + this.loopEnd = options.loopEnd; // invoke the callback if it's already loaded if (this._buffers.loaded) { @@ -143,6 +154,9 @@ export class Sampler extends Instrument { onerror: noOp, release: 0.1, urls: {}, + loop: false, + loopStart: 0, + loopEnd: undefined }); } @@ -193,7 +207,16 @@ export class Sampler extends Instrument { fadeOut: this.release, playbackRate, }).connect(this.output); - source.start(time, 0, buffer.duration / playbackRate, velocity); + + if (this.loop) { + source.loop = true; + source.loopStart = this.loopStart; + if (this.loopEnd !== undefined) { + source.loopEnd = this.loopEnd; + } + } + + source.start(time, 0, this.loop ? undefined : buffer.duration / playbackRate, velocity); // add it to the active sources if (!isArray(this._activeSources.get(midi))) { this._activeSources.set(midi, []);