Skip to content

Commit

Permalink
fix: detune start param (#74)
Browse files Browse the repository at this point in the history
* feat: add detuneto piano keyboard

* fix: findSampleInRegion includes user's detune param

* chore: bump version

* fix: changelog
  • Loading branch information
danigb authored Apr 11, 2024
1 parent 551690e commit 7452050
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
22 changes: 8 additions & 14 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,22 @@ const context = new AudioContext();
const drums = new DrumMachine(context, {
instrument: {
baseUrl: "https://smpldsnds.github.io/drum-machines/roland-cr-8000/",
name: "roland-cr-8000",
name: "Roland-CR-8000",
samples: [
"Cr8kbass",
"Cr8kchat",
"Cr8kclap",
"Cr8kclav",
"Cr8kcowb",
"Cr8kcymb",
"Cr8khitm",
"Cr8klcng",
"Cr8klotm",
"Cr8kmcng",
"Cr8kohat",
"Cr8krim",
"Cr8ksnar",
"hihat-closed",
"hihat-open",
"kick",
"snare",
"tom-high",
"tom-low",
],
formats: ["ogg", "m4a"],
},
});
```

- DrumMachine uses https://github.com/smpldsnds/drum-machines as source of samples
- Fix: `detune` param on `start` method

## 0.12.x

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.13.2",
"version": "0.13.3",
"homepage": "https://github.com/danigb/smplr#readme",
"description": "A Sampled collection of instruments",
"main": "dist/index.js",
Expand Down
15 changes: 13 additions & 2 deletions site/src/PianoKeyboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const isBlack = (midi: number) => [1, 3, 6, 8, 10].includes(midi % 12);
type PianoKeyboardNote = {
note: number;
velocity: number;
detune: number;
time?: number;
duration?: number;
};
Expand All @@ -26,6 +27,7 @@ export function PianoKeyboard({
onRelease?: (midi: number) => void;
}) {
const [velocity, setVelocity] = useState(100);
const [detune, setDetune] = useState(0);
const [oct, setOct] = useState(60);
const [sustain, setSustain] = useState(false);
const isPlaying = (midi: number) => false;
Expand All @@ -44,7 +46,7 @@ export function PianoKeyboard({
className={`accidental-key ${
isPlaying(midi) ? "accidental-key--playing" : ""
}`}
onMouseDown={() => onPress({ note: midi, velocity })}
onMouseDown={() => onPress({ note: midi, velocity, detune })}
onMouseUp={() => release(midi)}
>
<div className={"text"}></div>
Expand All @@ -56,7 +58,7 @@ export function PianoKeyboard({
className={`natural-key ${
isPlaying(midi) ? "natural-key--playing" : ""
}`}
onMouseDown={() => onPress({ note: midi, velocity })}
onMouseDown={() => onPress({ note: midi, velocity, detune })}
onMouseUp={() => release(midi)}
>
<div className={"text"}></div>
Expand Down Expand Up @@ -93,6 +95,14 @@ export function PianoKeyboard({
value={velocity}
onChange={(e) => setVelocity(e.target.valueAsNumber)}
/>
<div className="ml-3">Detune: {detune}</div>
<input
type="range"
min={-100}
max={100}
value={detune}
onChange={(e) => setDetune(e.target.valueAsNumber)}
/>

<input
className="ml-3"
Expand All @@ -112,6 +122,7 @@ export function PianoKeyboard({
onPress({
note: midi ?? 0,
velocity: Math.floor(60 + 40 * Math.random()),
detune: 0,
time: time * 0.25,
duration: 0.05,
})
Expand Down
17 changes: 17 additions & 0 deletions src/player/layers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ describe("findSamplesInRegions", () => {
]);
});

it("applies given detune", () => {
const group: RegionGroup = {
regions: [
{ sampleName: "a", midiPitch: 60, midiLow: 60, midiHigh: 75 },
{ sampleName: "b", midiPitch: 75, midiLow: 70, midiHigh: 80 },
],
sample: {},
};

expect(findSamplesInRegions(group, { note: 60, detune: 50 })).toEqual([
{ name: "a", detune: 50, note: 60 },
]);
expect(findSamplesInRegions(group, { note: 62, detune: -50 })).toEqual([
{ name: "a", detune: 150, note: 62 },
]);
});

it("finds finds with non integer midi", () => {
const group: RegionGroup = {
regions: [
Expand Down
3 changes: 2 additions & 1 deletion src/player/layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ function findSampleInRegion(
const velocity = sample.velocity ?? defaults.velocity;
const regionGainOffset = region.volume ? dbToGain(region.volume) : 0;
const sampleGainOffset = sample.gainOffset ?? defaults.gainOffset ?? 0;
const sampleDetune = sample.detune ?? 0;
return {
decayTime:
sample?.decayTime ?? region.sample?.decayTime ?? defaults.decayTime,
detune: 100 * (semitones + (region.tune ?? 0)),
detune: 100 * (semitones + (region.tune ?? 0)) + sampleDetune,
duration: sample?.duration ?? region.sample?.duration ?? defaults.duration,
gainOffset: sampleGainOffset + regionGainOffset || undefined,
loop: sample?.loop ?? region.sample?.loop ?? defaults.loop,
Expand Down

0 comments on commit 7452050

Please sign in to comment.