Skip to content

Commit

Permalink
fix: stop sample logic in SplendidGrandPiano
Browse files Browse the repository at this point in the history
  • Loading branch information
danigb committed Dec 18, 2024
1 parent d17711d commit dc7f7e4
Showing 1 changed file with 20 additions and 9 deletions.
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 dc7f7e4

Please sign in to comment.