Skip to content

Commit

Permalink
chore: rename and deprecate
Browse files Browse the repository at this point in the history
  • Loading branch information
danigb committed Dec 17, 2024
1 parent bb01f51 commit 305ca88
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 41 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ DrumMachines group different samples with same prefix under the same group. For

```js
const drums = new DrumMachine(context, { instrument: "TR-808" });
drum.sampleNames; // => ['kick-1', 'kick-2', 'snare-1', 'snare-2', ...]
drum.sampleGroups; // => ['kick', 'snare']
drum.getGroupVariations('kick') => // => ['kick-1', 'kick-2']
drum.getSampleNames(); // => ['kick-1', 'kick-2', 'snare-1', 'snare-2', ...]
drum.getGroupNames(); // => ['kick', 'snare']
drum.getSampleNamesForGroup('kick') => // => ['kick-1', 'kick-2']
```

⚠️ This is a breaking change: before `sampleNames` returns `sampleGroups`. Now `sampleNames` return all sample names.
**Deprecations:**

Also, before 0.16.0 `getGroupVariations` was `getVariations`. Now `getVariations` is deprecated.
- `drum.sampleNames` is deprecated in favour of `drum.getSampleNames()` or `drum.getGroupNames()`
- `drum.getVariations` is now called `drum.getSampleNamesForGroup`

## 0.15.x

Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,20 @@ const drums = new DrumMachine(context, { instrument: "TR-808" });
drums.start({ note: "kick" });

// Drum samples are grouped and can have variations:
drum.sampleNames; // => ['kick-1', 'kick-2', 'snare-1', 'snare-2', ...]
drum.sampleGroups; // => ['kick', 'snare']
drum.getGroupVariations('kick') => // => ['kick-1', 'kick-2']
drums.sampleNames; // => ['kick-1', 'kick-2', 'snare-1', 'snare-2', ...]
drums.groupNames; // => ['kick', 'snare']
drums.getSampleNamesForGroup("kick") => // => ['kick-1', 'kick-2']

// You can trigger samples by group name or specific sample
drums.start("kick");
drums.start("kick-1");
```

Properties and functions:

- `sampleNames: string[]`: An array with all sample names of this instrument
- `sampleGroups: string[]`: Samples with same name are grouped (for example `tom-1.ogg` and `tom-2.ogg` are from the `tom` group). `sampleGroups` is an array with all groups of this drum machine
- `getGroupVariations(groupName: string) => string[]`: Return all sample names of the given group
- `groupNames: string[]`: Samples with same name are grouped (for example `tom-1.ogg` and `tom-2.ogg` are from the `tom` group). `groupNames` is an array with all groups of this drum machine
- `getSampleNamesForGroup(groupName: string) => string[]`: Return all sample names of the given group

### Smolken double bass

Expand Down
14 changes: 7 additions & 7 deletions site/src/DrumMachineExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,29 @@ export function DrumMachineExample({ className }: { className?: string }) {
/>
</div>
<div className="grid grid-cols-6 gap-1">
{drums?.sampleGroups.map((sample) => (
<div key={sample} className="bg-zinc-900 rounded px-2 pb-2">
{drums?.getGroupNames().map((group) => (
<div key={group} className="bg-zinc-900 rounded px-2 pb-2">
<div className="flex">
<button
className="text-left flex-grow"
onClick={() => {
drums?.start({
note: sample,
note: group,
detune: 50 * (Math.random() - 0.5),
});
}}
>
{sample}
{group}
</button>
</div>
<div className="flex flex-wrap gap-1 mt-1">
{drums?.getVariations(sample).map((variation) => (
{drums?.getSampleNamesForGroup(group).map((sample) => (
<button
key={variation}
key={sample}
className="bg-zinc-600 w-4 h-4 rounded"
onMouseDown={() => {
drums?.start({
note: variation,
note: sample,
});
}}
></button>
Expand Down
12 changes: 6 additions & 6 deletions src/drum-machine/dm-instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function isDrumMachineInstrument(
typeof instrument.baseUrl === "string" &&
typeof instrument.name === "string" &&
Array.isArray(instrument.samples) &&
Array.isArray(instrument.sampleGroups) &&
Array.isArray(instrument.groupNames) &&
typeof instrument.nameToSampleName === "object" &&
typeof instrument.sampleGroupVariations === "object"
);
Expand All @@ -18,15 +18,15 @@ export type DrumMachineInstrument = {
baseUrl: string;
name: string;
samples: string[];
sampleGroups: string[];
groupNames: string[];
nameToSampleName: Record<string, string | undefined>;
sampleGroupVariations: Record<string, string[]>;
};
export const EMPTY_INSTRUMENT: DrumMachineInstrument = {
baseUrl: "",
name: "",
samples: [],
sampleGroups: [],
groupNames: [],
nameToSampleName: {},
sampleGroupVariations: {},
};
Expand All @@ -39,15 +39,15 @@ export async function fetchDrumMachineInstrument(
const json = await res.json();
// need to fix json
json.baseUrl = url.replace("/dm.json", "");
json.sampleGroups = [];
json.groupNames = [];
json.nameToSampleName = {};
json.sampleGroupVariations = {};
for (const sample of json.samples) {
json.nameToSampleName[sample] = sample;
const separator = sample.indexOf("/") !== -1 ? "/" : "-";
const [base, variation] = sample.split(separator);
if (!json.sampleGroups.includes(base)) {
json.sampleGroups.push(base);
if (!json.groupNames.includes(base)) {
json.groupNames.push(base);
}
json.nameToSampleName[base] ??= sample;
json.sampleGroupVariations[base] ??= [];
Expand Down
9 changes: 7 additions & 2 deletions src/drum-machine/drum-machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ describe("Drum machine", () => {
const dm = await new DrumMachine(context, {
instrument: "TR-808",
}).load;
expect(dm.sampleNames).toEqual(["kick/low", "kick/mid", "kick/high"]);
expect(dm.sampleGroups).toEqual(["kick"]);
expect(dm.getSampleNames()).toEqual(["kick/low", "kick/mid", "kick/high"]);
expect(dm.getGroupNames()).toEqual(["kick"]);
expect(dm.getSampleNamesForGroup("kick")).toEqual([
"kick/low",
"kick/mid",
"kick/high",
]);
});

it("calls underlying player on stop", () => {
Expand Down
37 changes: 21 additions & 16 deletions src/drum-machine/drum-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,15 @@ export class DrumMachine {
});
}

async loaded() {
console.warn("deprecated: use load instead");
return this.load;
}

get sampleNames(): string[] {
return this.#instrument.samples;
}

get sampleGroups(): string[] {
return this.#instrument.sampleGroups;
getSampleNames(): string[] {
return this.#instrument.samples.slice();
}

getVariations(groupName: string): string[] {
console.warn("deprecated: use getGroupVariations");
return this.#instrument.sampleGroupVariations[groupName] ?? [];
getGroupNames(): string[] {
return this.#instrument.groupNames.slice();
}

getGroupVariations(groupName: string): string[] {
console.warn("deprecated: use getGroupVariations");
getSampleNamesForGroup(groupName: string): string[] {
return this.#instrument.sampleGroupVariations[groupName] ?? [];
}

Expand All @@ -115,6 +104,22 @@ export class DrumMachine {
stop(sample: SampleStop) {
return this.player.stop(sample);
}

/** @deprecated */
async loaded() {
console.warn("deprecated: use load instead");
return this.load;
}
/** @deprecated */
get sampleNames(): string[] {
console.log("deprecated: Use getGroupNames instead");
return this.#instrument.groupNames.slice();
}
/** @deprecated */
getVariations(groupName: string): string[] {
console.warn("deprecated: use getSampleNamesForGroup");
return this.#instrument.sampleGroupVariations[groupName] ?? [];
}
}

function drumMachineLoader(
Expand Down

0 comments on commit 305ca88

Please sign in to comment.