Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loop options (loop, loopStart, loopEnd) to Sampler #1213

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Tone/instrument/Sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export interface SamplerOptions extends InstrumentOptions {
baseUrl: string;
curve: ToneBufferSourceCurve;
urls: SamplesMap;
loop?: boolean;
loopStart?: Time;
loopEnd?: Time;
}

/**
Expand Down Expand Up @@ -83,6 +86,11 @@ export class Sampler extends Instrument<SamplerOptions> {
*/
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.
Expand Down Expand Up @@ -126,6 +134,9 @@ export class Sampler extends Instrument<SamplerOptions> {
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) {
Expand All @@ -143,6 +154,9 @@ export class Sampler extends Instrument<SamplerOptions> {
onerror: noOp,
release: 0.1,
urls: {},
loop: false,
loopStart: 0,
loopEnd: undefined
});
}

Expand Down Expand Up @@ -193,7 +207,16 @@ export class Sampler extends Instrument<SamplerOptions> {
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, []);
Expand Down