@waveform-playlist/playout 12.5.2 → 12.5.4

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);
@@ -1821,9 +1853,11 @@ function createToneAdapter(options) {
1821
1853
  getGlobalContext();
1822
1854
  let _playoutGeneration = 1;
1823
1855
  let playout = new TonePlayout({ effects: options?.effects });
1856
+ let _onPlaybackEnded = null;
1824
1857
  playout.setOnPlaybackComplete(() => {
1825
1858
  if (_playoutGeneration === 1) {
1826
1859
  _isPlaying = false;
1860
+ _onPlaybackEnded?.();
1827
1861
  }
1828
1862
  });
1829
1863
  let _isPlaying = false;
@@ -1956,6 +1990,7 @@ function createToneAdapter(options) {
1956
1990
  playout.setOnPlaybackComplete(() => {
1957
1991
  if (generation === _playoutGeneration) {
1958
1992
  _isPlaying = false;
1993
+ _onPlaybackEnded?.();
1959
1994
  }
1960
1995
  });
1961
1996
  }
@@ -2115,6 +2150,9 @@ function createToneAdapter(options) {
2115
2150
  isPlaying() {
2116
2151
  return _isPlaying;
2117
2152
  },
2153
+ onPlaybackEnded(callback) {
2154
+ _onPlaybackEnded = callback;
2155
+ },
2118
2156
  setMasterVolume(volume) {
2119
2157
  playout?.setMasterGain(volume);
2120
2158
  },
@@ -2246,6 +2284,7 @@ function createToneAdapter(options) {
2246
2284
  }
2247
2285
  playout = null;
2248
2286
  _isPlaying = false;
2287
+ _onPlaybackEnded = null;
2249
2288
  _currentTracks.clear();
2250
2289
  _midiTrackBuild.clear();
2251
2290
  }