From 76d8945a2ffe1333205c01a19ad86e5f01829be2 Mon Sep 17 00:00:00 2001 From: danigb Date: Wed, 18 Dec 2024 09:43:56 +0100 Subject: [PATCH] Fix: piano stop with note name (#100) * fix: stop sample logic in SplendidGrandPiano * bump version and changelog --- CHANGELOG.md | 11 +++++++++++ package.json | 2 +- src/splendid-grand-piano.ts | 29 ++++++++++++++++++++--------- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 547bdfc..4dc2441 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,17 @@ drum.getSampleNamesForGroup('kick') => // => ['kick-1', 'kick-2'] - `drum.sampleNames` is deprecated in favour of `drum.getSampleNames()` or `drum.getGroupNames()` - `drum.getVariations` is now called `drum.getSampleNamesForGroup` +#### Bug fix: SampleGrandPiano stop note + +Now you can pass a note name to `stop` method of grand piano: + +```js +piano.start("C4"); + +piano.stop(60); // This worked previously +piano.stop("C4"); // This now works +``` + ## 0.15.x #### Disable scheduler with `disableScheduler` option diff --git a/package.json b/package.json index d4cc849..d998c9e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "smplr", - "version": "0.16.0", + "version": "0.16.1", "homepage": "https://github.com/danigb/smplr#readme", "description": "A Sampled collection of instruments", "main": "dist/index.js", diff --git a/src/splendid-grand-piano.ts b/src/splendid-grand-piano.ts index da27cbe..c98ffb1 100644 --- a/src/splendid-grand-piano.ts +++ b/src/splendid-grand-piano.ts @@ -19,9 +19,9 @@ export type SplendidGrandPianoConfig = { velocity: number; decayTime: number; notesToLoad?: { - notes: number[], - velocityRange: [number, number] - } + notes: number[]; + velocityRange: [number, number]; + }; } & Partial; const BASE_URL = "https://danigb.github.io/samples/splendid-grand-piano"; @@ -97,7 +97,16 @@ export class SplendidGrandPiano { } stop(sample?: SampleStop | number | string) { - return this.player.stop(sample); + if (typeof sample === "string") { + return this.player.stop(toMidi(sample) ?? sample); + } else if (typeof sample === "object") { + const midi = toMidi(sample.stopId); + return this.player.stop( + midi !== undefined ? { ...sample, stopId: midi } : sample + ); + } else { + return this.player.stop(sample); + } } } @@ -121,12 +130,12 @@ function splendidGrandPianoLoader( baseUrl: string, storage: Storage, notesToLoad?: { - notes: number[], - velocityRange: [number, number] + notes: number[]; + velocityRange: [number, number]; } ): AudioBuffersLoader { const format = findFirstSupportedFormat(["ogg", "m4a"]) ?? "ogg"; - let layers = notesToLoad + let layers = notesToLoad ? LAYERS.filter( (layer) => layer.vel_range[0] <= notesToLoad.velocityRange[1] && @@ -136,8 +145,10 @@ function splendidGrandPianoLoader( return async (context, buffers) => { for (const layer of layers) { - const samples = notesToLoad - ? layer.samples.filter(sample => notesToLoad.notes.includes(sample[0] as number)) + const samples = notesToLoad + ? layer.samples.filter((sample) => + notesToLoad.notes.includes(sample[0] as number) + ) : layer.samples; await Promise.all(