@waveform-playlist/playout 12.5.2 → 12.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -865,6 +865,19 @@ var MidiToneTrack = class {
865
865
  // src/SoundFontToneTrack.ts
866
866
  var import_tone3 = require("tone");
867
867
  var import_core3 = require("@waveform-playlist/core");
868
+ function envelopeValueAt(tRel, attack, hold, decay, peakGain, sustainGain) {
869
+ if (tRel <= 0) return 0;
870
+ if (tRel < attack) return peakGain * (tRel / attack);
871
+ const rampStart = hold > 1e-3 ? attack + hold : attack;
872
+ const rampEnd = attack + hold + decay;
873
+ if (tRel <= rampStart || rampEnd <= rampStart) {
874
+ return tRel >= rampEnd ? sustainGain : peakGain;
875
+ }
876
+ if (tRel < rampEnd) {
877
+ return peakGain + (sustainGain - peakGain) * ((tRel - rampStart) / (rampEnd - rampStart));
878
+ }
879
+ return sustainGain;
880
+ }
868
881
  var SoundFontToneTrack = class {
869
882
  constructor(options) {
870
883
  /** Rate-limit missing sample warnings — one per track instance (a page-
@@ -959,15 +972,34 @@ var SoundFontToneTrack = class {
959
972
  const gainNode = rawContext.createGain();
960
973
  const { attackVolEnv, holdVolEnv, decayVolEnv, sustainVolEnv, releaseVolEnv } = sfSample;
961
974
  const sustainGain = peakGain * sustainVolEnv;
975
+ const noteOff = time + effectiveDuration;
976
+ const attackEnd = time + attackVolEnv;
977
+ const decayStart = time + attackVolEnv + holdVolEnv;
978
+ const decayEnd = decayStart + decayVolEnv;
979
+ const heldAtNoteOff = envelopeValueAt(
980
+ effectiveDuration,
981
+ attackVolEnv,
982
+ holdVolEnv,
983
+ decayVolEnv,
984
+ peakGain,
985
+ sustainGain
986
+ );
962
987
  gainNode.gain.setValueAtTime(0, time);
963
- gainNode.gain.linearRampToValueAtTime(peakGain, time + attackVolEnv);
964
- if (holdVolEnv > 1e-3) {
965
- gainNode.gain.setValueAtTime(peakGain, time + attackVolEnv + holdVolEnv);
988
+ if (noteOff < attackEnd) {
989
+ gainNode.gain.linearRampToValueAtTime(heldAtNoteOff, noteOff);
990
+ } else {
991
+ gainNode.gain.linearRampToValueAtTime(peakGain, attackEnd);
992
+ if (holdVolEnv > 1e-3 && decayStart < noteOff) {
993
+ gainNode.gain.setValueAtTime(peakGain, decayStart);
994
+ }
995
+ if (noteOff < decayEnd) {
996
+ gainNode.gain.linearRampToValueAtTime(heldAtNoteOff, noteOff);
997
+ } else {
998
+ gainNode.gain.linearRampToValueAtTime(sustainGain, decayEnd);
999
+ gainNode.gain.setValueAtTime(sustainGain, noteOff);
1000
+ }
966
1001
  }
967
- const decayStart = time + attackVolEnv + holdVolEnv;
968
- gainNode.gain.linearRampToValueAtTime(sustainGain, decayStart + decayVolEnv);
969
- gainNode.gain.setValueAtTime(sustainGain, time + effectiveDuration);
970
- gainNode.gain.linearRampToValueAtTime(0, time + effectiveDuration + releaseVolEnv);
1002
+ gainNode.gain.linearRampToValueAtTime(0, noteOff + releaseVolEnv);
971
1003
  source.connect(gainNode);
972
1004
  gainNode.connect(this.volumeNode.input.input);
973
1005
  this.activeSources.add(source);