Skip to content

Commit

Permalink
Shorten late release
Browse files Browse the repository at this point in the history
  • Loading branch information
jonikorpi committed Feb 7, 2025
1 parent b0c7278 commit 52153dc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions example/Keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,23 +438,21 @@ const attackWithController = (
if (audioContext.state !== "running") audioContext.resume();

const isAlreadyPlaying = playingControllers.has(controllerId);
if (isAlreadyPlaying) releaseWithController(controllerId, shiftKey, altKey);
let instrument = isAlreadyPlaying ? releaseWithController(controllerId, shiftKey, altKey) : null;

const preset = allInstrumentPresets[instrumentName];

let instrument: Instrument | null = null;

for (const freeInstrument of freeInstruments) {
if (
!instrument &&
freeInstrument.preset === preset &&
freeInstrument.previousEndAt < audioContext.currentTime
freeInstrument.previousEndAt <= audioContext.currentTime
) {
// Use instrument
console.log("adopting", controllerId);
instrument = freeInstrument;
freeInstruments.delete(freeInstrument);
} else if (freeInstrument.previousEndAt + 5 < audioContext.currentTime) {
} else if (freeInstrument.previousEndAt + 1 < audioContext.currentTime) {
// Destroy long unused instrument
console.log("destroying");
destroyInstrument(freeInstrument);
Expand Down Expand Up @@ -607,4 +605,6 @@ const releaseWithController = (controllerId: ControllerId, shiftKey = false, _al
}

if (coloriseIntervals) updateConsonances(frequencyToMidi(previousPitch), -1.0);

return instrument;
};
2 changes: 1 addition & 1 deletion instruments.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ export const releaseInstrument = (
cancelPendingInstrumentEvents(instrument, firstEndAt);
vibratoMain.frequency.setTargetAtTime(0.0, firstEndAt, 0.008);

instrument.previousEndAt = releaseEarly ? firstEndAt : endAt + firstRelease * 0.618;
instrument.previousEndAt = releaseEarly ? firstEndAt : endAt + firstRelease * 0.236;
};

const cancelPendingInstrumentEvents = (
Expand Down

0 comments on commit 52153dc

Please sign in to comment.