From 9002d0f31588980a32416894d9cda4c2bef20d9d Mon Sep 17 00:00:00 2001 From: AdelaFoofur82 <78669385+AdelaFoofur82@users.noreply.github.com> Date: Wed, 23 Oct 2024 08:18:55 +0200 Subject: [PATCH 1/2] Emitting "source" event when a source is scheduled --- src/beatbox.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/beatbox.ts b/src/beatbox.ts index 5f87ba8..cd86fb9 100644 --- a/src/beatbox.ts +++ b/src/beatbox.ts @@ -119,6 +119,7 @@ export class Beatbox extends EventEmitter { source.connect(this._audioContext!.destination); } + this.emit("source", { source, time }); source.start(time); const clear = () => { From c02318f4b915d6cd2ec1c09a71b234e745b7e946 Mon Sep 17 00:00:00 2001 From: AdelaFoofur82 <78669385+AdelaFoofur82@users.noreply.github.com> Date: Wed, 23 Oct 2024 08:49:55 +0200 Subject: [PATCH 2/2] We don't stop the source before it ends We avoid to stop the source when another instance of the same source needed to be scheduled to play, in order to let the sound source end by itself. --- src/beatbox.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/beatbox.ts b/src/beatbox.ts index cd86fb9..e2b23c4 100644 --- a/src/beatbox.ts +++ b/src/beatbox.ts @@ -374,8 +374,9 @@ export class Beatbox extends EventEmitter { if (instr && (instr.volume == null || instr.volume > 0)) { let time = this._referenceTime! + (this._position - this._upbeat) * this._strokeLength / 1000; - if(this._lastInstrumentStrokes[instr.key]) - this._lastInstrumentStrokes[instr.key].stop(time); + // We avoid the sound to stop before it is required to sound again. We let it end by itself + // if(this._lastInstrumentStrokes[instr.key]) + // this._lastInstrumentStrokes[instr.key].stop(time); const sound = this._scheduleSound(instr, time); this._lastInstrumentStrokes[instr.key] = sound;