Skip to content

Commit

Permalink
Fix: piano stop with note name (#100)
Browse files Browse the repository at this point in the history
* fix: stop sample logic in SplendidGrandPiano

* bump version and changelog
  • Loading branch information
danigb authored Dec 18, 2024
1 parent d17711d commit 76d8945
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
29 changes: 20 additions & 9 deletions src/splendid-grand-piano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export type SplendidGrandPianoConfig = {
velocity: number;
decayTime: number;
notesToLoad?: {
notes: number[],
velocityRange: [number, number]
}
notes: number[];
velocityRange: [number, number];
};
} & Partial<DefaultPlayerConfig>;

const BASE_URL = "https://danigb.github.io/samples/splendid-grand-piano";
Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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] &&
Expand All @@ -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(
Expand Down

0 comments on commit 76d8945

Please sign in to comment.