@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.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);
@@ -1838,9 +1870,11 @@ function createToneAdapter(options) {
1838
1870
  getGlobalContext();
1839
1871
  let _playoutGeneration = 1;
1840
1872
  let playout = new TonePlayout({ effects: options?.effects });
1873
+ let _onPlaybackEnded = null;
1841
1874
  playout.setOnPlaybackComplete(() => {
1842
1875
  if (_playoutGeneration === 1) {
1843
1876
  _isPlaying = false;
1877
+ _onPlaybackEnded?.();
1844
1878
  }
1845
1879
  });
1846
1880
  let _isPlaying = false;
@@ -1973,6 +2007,7 @@ function createToneAdapter(options) {
1973
2007
  playout.setOnPlaybackComplete(() => {
1974
2008
  if (generation === _playoutGeneration) {
1975
2009
  _isPlaying = false;
2010
+ _onPlaybackEnded?.();
1976
2011
  }
1977
2012
  });
1978
2013
  }
@@ -2132,6 +2167,9 @@ function createToneAdapter(options) {
2132
2167
  isPlaying() {
2133
2168
  return _isPlaying;
2134
2169
  },
2170
+ onPlaybackEnded(callback) {
2171
+ _onPlaybackEnded = callback;
2172
+ },
2135
2173
  setMasterVolume(volume) {
2136
2174
  playout?.setMasterGain(volume);
2137
2175
  },
@@ -2263,6 +2301,7 @@ function createToneAdapter(options) {
2263
2301
  }
2264
2302
  playout = null;
2265
2303
  _isPlaying = false;
2304
+ _onPlaybackEnded = null;
2266
2305
  _currentTracks.clear();
2267
2306
  _midiTrackBuild.clear();
2268
2307
  }