Update Sampler.ts

This commit is contained in:
Maxime Le Breton 2023-10-27 18:06:52 +02:00 committed by GitHub
parent 08df7ad68c
commit 28eb26c930
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<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.
@ -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) {
@ -143,6 +154,9 @@ export class Sampler extends Instrument<SamplerOptions> {
onerror: noOp,
release: 0.1,
urls: {},
loop: false,
loopStart: 0,
loopEnd: undefined
});
}
@ -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, []);