@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.mjs CHANGED
@@ -842,6 +842,19 @@ var MidiToneTrack = class {
842
842
  // src/SoundFontToneTrack.ts
843
843
  import { Volume as Volume3, Gain as Gain3, Panner as Panner3, Part as Part2, getDestination as getDestination3, getContext as getContext3 } from "tone";
844
844
  import { gainToDb as gainToDb3 } from "@waveform-playlist/core";
845
+ function envelopeValueAt(tRel, attack, hold, decay, peakGain, sustainGain) {
846
+ if (tRel <= 0) return 0;
847
+ if (tRel < attack) return peakGain * (tRel / attack);
848
+ const rampStart = hold > 1e-3 ? attack + hold : attack;
849
+ const rampEnd = attack + hold + decay;
850
+ if (tRel <= rampStart || rampEnd <= rampStart) {
851
+ return tRel >= rampEnd ? sustainGain : peakGain;
852
+ }
853
+ if (tRel < rampEnd) {
854
+ return peakGain + (sustainGain - peakGain) * ((tRel - rampStart) / (rampEnd - rampStart));
855
+ }
856
+ return sustainGain;
857
+ }
845
858
  var SoundFontToneTrack = class {
846
859
  constructor(options) {
847
860
  /** Rate-limit missing sample warnings — one per track instance (a page-
@@ -936,15 +949,34 @@ var SoundFontToneTrack = class {
936
949
  const gainNode = rawContext.createGain();
937
950
  const { attackVolEnv, holdVolEnv, decayVolEnv, sustainVolEnv, releaseVolEnv } = sfSample;
938
951
  const sustainGain = peakGain * sustainVolEnv;
952
+ const noteOff = time + effectiveDuration;
953
+ const attackEnd = time + attackVolEnv;
954
+ const decayStart = time + attackVolEnv + holdVolEnv;
955
+ const decayEnd = decayStart + decayVolEnv;
956
+ const heldAtNoteOff = envelopeValueAt(
957
+ effectiveDuration,
958
+ attackVolEnv,
959
+ holdVolEnv,
960
+ decayVolEnv,
961
+ peakGain,
962
+ sustainGain
963
+ );
939
964
  gainNode.gain.setValueAtTime(0, time);
940
- gainNode.gain.linearRampToValueAtTime(peakGain, time + attackVolEnv);
941
- if (holdVolEnv > 1e-3) {
942
- gainNode.gain.setValueAtTime(peakGain, time + attackVolEnv + holdVolEnv);
965
+ if (noteOff < attackEnd) {
966
+ gainNode.gain.linearRampToValueAtTime(heldAtNoteOff, noteOff);
967
+ } else {
968
+ gainNode.gain.linearRampToValueAtTime(peakGain, attackEnd);
969
+ if (holdVolEnv > 1e-3 && decayStart < noteOff) {
970
+ gainNode.gain.setValueAtTime(peakGain, decayStart);
971
+ }
972
+ if (noteOff < decayEnd) {
973
+ gainNode.gain.linearRampToValueAtTime(heldAtNoteOff, noteOff);
974
+ } else {
975
+ gainNode.gain.linearRampToValueAtTime(sustainGain, decayEnd);
976
+ gainNode.gain.setValueAtTime(sustainGain, noteOff);
977
+ }
943
978
  }
944
- const decayStart = time + attackVolEnv + holdVolEnv;
945
- gainNode.gain.linearRampToValueAtTime(sustainGain, decayStart + decayVolEnv);
946
- gainNode.gain.setValueAtTime(sustainGain, time + effectiveDuration);
947
- gainNode.gain.linearRampToValueAtTime(0, time + effectiveDuration + releaseVolEnv);
979
+ gainNode.gain.linearRampToValueAtTime(0, noteOff + releaseVolEnv);
948
980
  source.connect(gainNode);
949
981
  gainNode.connect(this.volumeNode.input.input);
950
982
  this.activeSources.add(source);