Skip to content

Commit

Permalink
feat: piano loads subset of audio notes in layers (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgecartridge committed Dec 15, 2023
1 parent 2f72847 commit 410c86b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 25 additions & 4 deletions src/splendid-grand-piano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export type SplendidGrandPianoConfig = {
detune: number;
velocity: number;
decayTime: number;
notesToLoad?: {
notes: number[],
velocityRange: [number, number]
}
} & Partial<DefaultPlayerConfig>;

const BASE_URL = "https://danigb.github.io/samples/splendid-grand-piano";
Expand Down Expand Up @@ -45,7 +49,8 @@ export class SplendidGrandPiano {
this.player = new DefaultPlayer(context, this.options);
const loader = splendidGrandPianoLoader(
this.options.baseUrl,
this.options.storage
this.options.storage,
this.options.notesToLoad
);
this.load = loader(context, this.player.buffers).then(() => this);
}
Expand Down Expand Up @@ -114,13 +119,29 @@ function findNearestMidiInLayer(

function splendidGrandPianoLoader(
baseUrl: string,
storage: Storage
storage: Storage,
notesToLoad?: {
notes: number[],
velocityRange: [number, number]
}
): AudioBuffersLoader {
const format = findFirstSupportedFormat(["ogg", "m4a"]) ?? "ogg";
let layers = notesToLoad
? LAYERS.filter(
(layer) =>
layer.vel_range[0] <= notesToLoad.velocityRange[1] &&
layer.vel_range[1] >= notesToLoad.velocityRange[0]
)
: LAYERS;

return async (context, buffers) => {
for (const layer of LAYERS) {
for (const layer of layers) {
const samples = notesToLoad
? layer.samples.filter(sample => notesToLoad.notes.includes(sample[0] as number))
: layer.samples;

await Promise.all(
layer.samples.map(async ([midi, name]) => {
samples.map(async ([midi, name]) => {
const url = `${baseUrl}/${name}.${format}`;
const buffer = await loadAudioBuffer(context, url, storage);
if (buffer) buffers[layer.name + midi] = buffer;
Expand Down
76 changes: 76 additions & 0 deletions src/splendid-grand-piano/splendid-grand-piano.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { InternalPlayerMock, createAudioContextMock, createFetchMock } from "../test-helpers";
import { SplendidGrandPiano } from "../splendid-grand-piano";

function setup() {
createFetchMock({
"https://danigb.github.io/samples/splendid-grand-piano/PP-C3.ogg":
"PP-C3",
"https://danigb.github.io/samples/splendid-grand-piano/Mp-C3.ogg":
"Mp-C3",
"https://danigb.github.io/samples/splendid-grand-piano/Mf-C3.ogg":
"Mf-C3",
"https://danigb.github.io/samples/splendid-grand-piano/FF-C3.ogg":
"FF-C3",
"https://danigb.github.io/samples/splendid-grand-piano/FF-G3.ogg":
"FF-G3",
});
const mock = createAudioContextMock();
const context = mock.context;

return { context, mock };
}

describe("Splendid grand piano", () => {
it("only specified notes are loaded", async () => {
const { context } = setup();
const piano = await new SplendidGrandPiano(context, {
notesToLoad: {
notes: [60, 67],
velocityRange: [105, 106]
}
}).load;

expect(Object.keys(piano.buffers)).toHaveLength(2);
expect(piano.buffers).toEqual({ 'FF60': { arrayBuffer: 'FF-C3' }, 'FF67': { arrayBuffer: 'FF-G3' } });

const start = jest.fn();

(piano as any).player.start = start;
piano.start({ note: 'C4', velocity: 105 });
expect(start).toHaveBeenCalledWith({ note: 'FF60', stopId: 60, detune: 0, velocity: 105 });
});

it("specified notes are loaded across multiple velocity ranges when given", async () => {
const { context } = setup();
const piano = await new SplendidGrandPiano(context, {
notesToLoad: {
notes: [60],
velocityRange: [1, 109]
}
}).load;

expect(Object.keys(piano.buffers)).toHaveLength(5);
expect(piano.buffers).toEqual({
'FF60': { arrayBuffer: 'FF-C3' },
'MF60': { arrayBuffer: 'Mf-C3' },
'MP60': { arrayBuffer: 'Mp-C3' },
'PP60': { arrayBuffer: 'PP-C3' },
'PPP60': { arrayBuffer: 'PP-C3' }
});
});

it("detuning is based on loaded notes", async () => {
const { context } = setup();
const piano = await new SplendidGrandPiano(context, {
notesToLoad: {
notes: [60],
velocityRange: [105, 106]
}
}).load;
const start = jest.fn();

(piano as any).player.start = start;
piano.start({ note: 'C6', velocity: 105 });
expect(start).toHaveBeenCalledWith({ note: 'FF60', stopId: 84, detune: 2400, velocity: 105 });
});
});

0 comments on commit 410c86b

Please sign in to comment.