untitled-pixi-live2d-engine 1.2.2 → 1.3.1

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/cubism.es.js CHANGED
@@ -40,7 +40,6 @@ var __async = (__this, __arguments, generator) => {
40
40
  });
41
41
  };
42
42
  import { EventEmitter, Matrix, loadTextures, Assets, Ticker, Transform, Point, Bounds, ExtensionType, Container, ObservablePoint, WebGLRenderer, DOMAdapter, Texture, CanvasSource } from "pixi.js";
43
- import { sound, Sound, webaudio } from "@pixi/sound";
44
43
  const _csmVector = class _csmVector {
45
44
  /**
46
45
  * 引数付きコンストラクタ
@@ -2760,7 +2759,7 @@ const config = {
2760
2759
  preserveExpressionOnMotion: true,
2761
2760
  cubism: CubismFrameworkConfig
2762
2761
  };
2763
- const VERSION = "1.2.2";
2762
+ const VERSION = "1.3.1";
2764
2763
  const logger = {
2765
2764
  log(tag, ...messages) {
2766
2765
  if (config.logLevel <= config.LOG_LEVEL_VERBOSE) {
@@ -3479,7 +3478,64 @@ class MotionState {
3479
3478
  }
3480
3479
  const TAG$2 = "SoundManager";
3481
3480
  const VOLUME = 0.5;
3482
- sound.disableAutoPause = true;
3481
+ const SOUND_ALIAS_PREFIX = "live2d-sound-";
3482
+ let soundId = 0;
3483
+ let pixiSoundPromise;
3484
+ const configuredLibraries = /* @__PURE__ */ new WeakSet();
3485
+ function configureSoundLibrary(library) {
3486
+ if (!configuredLibraries.has(library)) {
3487
+ try {
3488
+ library.disableAutoPause = true;
3489
+ } catch (e) {
3490
+ logger.warn(TAG$2, "Failed to disable @pixi/sound auto pause.", e);
3491
+ }
3492
+ configuredLibraries.add(library);
3493
+ }
3494
+ return library;
3495
+ }
3496
+ function getGlobalSoundLibrary() {
3497
+ return typeof PIXI !== "undefined" ? PIXI == null ? void 0 : PIXI.sound : void 0;
3498
+ }
3499
+ function resolveSoundLibrary() {
3500
+ return __async(this, null, function* () {
3501
+ const globalLibrary = getGlobalSoundLibrary();
3502
+ if (globalLibrary) {
3503
+ return configureSoundLibrary(globalLibrary);
3504
+ }
3505
+ pixiSoundPromise != null ? pixiSoundPromise : pixiSoundPromise = import("@pixi/sound").then(({ sound }) => configureSoundLibrary(sound)).catch((e) => {
3506
+ logger.warn(
3507
+ TAG$2,
3508
+ "@pixi/sound is not available. Load pixi-sound.js before using motion sounds, speak(), or lip sync.",
3509
+ e
3510
+ );
3511
+ return null;
3512
+ });
3513
+ return pixiSoundPromise;
3514
+ });
3515
+ }
3516
+ function removeSound(library, alias) {
3517
+ try {
3518
+ library.remove(alias);
3519
+ } catch (e) {
3520
+ logger.warn(TAG$2, `Failed to remove sound "${alias}".`, e);
3521
+ }
3522
+ }
3523
+ function destroySound(audio) {
3524
+ try {
3525
+ audio.destroy();
3526
+ } catch (e) {
3527
+ logger.warn(TAG$2, "Failed to destroy audio.", e);
3528
+ }
3529
+ }
3530
+ function getAudioBuffer(audio) {
3531
+ const media = audio.media;
3532
+ const buffer = media.buffer;
3533
+ if (!buffer || typeof buffer !== "object") {
3534
+ return void 0;
3535
+ }
3536
+ const candidate = buffer;
3537
+ return typeof candidate.getChannelData === "function" ? buffer : void 0;
3538
+ }
3483
3539
  class SoundManager {
3484
3540
  /**
3485
3541
  * Global volume that applies to all the sounds.
@@ -3500,22 +3556,47 @@ class SoundManager {
3500
3556
  */
3501
3557
  static add(file, onError) {
3502
3558
  return __async(this, null, function* () {
3559
+ let library = null;
3560
+ let alias;
3503
3561
  try {
3562
+ library = yield resolveSoundLibrary();
3563
+ if (!library) {
3564
+ throw new Error("@pixi/sound is not available");
3565
+ }
3566
+ alias = `${SOUND_ALIAS_PREFIX}${soundId++}`;
3567
+ const soundLibrary = library;
3568
+ const soundAlias = alias;
3504
3569
  const task = new Promise((resolve, reject) => {
3505
- const audio = Sound.from({
3570
+ const audio2 = soundLibrary.add(soundAlias, {
3506
3571
  url: file,
3507
3572
  volume: this._volume,
3508
3573
  preload: true,
3509
- loaded: () => {
3510
- if (!(audio.media instanceof webaudio.WebAudioMedia)) {
3574
+ loaded: (error, loadedAudio) => {
3575
+ if (error) {
3576
+ reject(error);
3577
+ return;
3578
+ }
3579
+ const sound = loadedAudio != null ? loadedAudio : audio2;
3580
+ if (!sound) {
3581
+ reject(new Error(`Error: ${file} failed to load`));
3582
+ return;
3583
+ }
3584
+ if (!getAudioBuffer(sound)) {
3511
3585
  reject(new Error(`Error: ${file} is not WebAudioMedia`));
3586
+ return;
3512
3587
  }
3513
- resolve(audio);
3588
+ resolve(sound);
3514
3589
  }
3515
3590
  });
3516
3591
  });
3517
- return yield task;
3592
+ const audio = yield task;
3593
+ this.aliases.set(audio, { alias: soundAlias, library: soundLibrary });
3594
+ this.audios.push(audio);
3595
+ return audio;
3518
3596
  } catch (e) {
3597
+ if (library && alias) {
3598
+ removeSound(library, alias);
3599
+ }
3519
3600
  logger.warn(TAG$2, `Error occurred on "${file}"`, e);
3520
3601
  onError == null ? void 0 : onError(e);
3521
3602
  return null;
@@ -3532,14 +3613,18 @@ class SoundManager {
3532
3613
  singleInstance: true,
3533
3614
  complete: () => {
3534
3615
  onFinish == null ? void 0 : onFinish();
3535
- audio.destroy();
3616
+ this.dispose(audio);
3536
3617
  }
3537
3618
  });
3538
3619
  }
3539
3620
  static addAnalyzer(audio, context) {
3540
- const media = audio.media;
3621
+ const buffer = getAudioBuffer(audio);
3622
+ if (!buffer) {
3623
+ logger.warn(TAG$2, "Cannot create audio analyzer because WebAudio media is unavailable.");
3624
+ return void 0;
3625
+ }
3541
3626
  const source = context.createBufferSource();
3542
- source.buffer = media.buffer;
3627
+ source.buffer = buffer;
3543
3628
  const analyser = context.createAnalyser();
3544
3629
  analyser.fftSize = config.fftSize;
3545
3630
  analyser.minDecibels = -90;
@@ -3577,7 +3662,18 @@ class SoundManager {
3577
3662
  * @param audio - An audio element.
3578
3663
  */
3579
3664
  static dispose(audio) {
3580
- audio.pause();
3665
+ try {
3666
+ audio.pause();
3667
+ } catch (e) {
3668
+ logger.warn(TAG$2, "Failed to pause audio.", e);
3669
+ }
3670
+ const managedSound = this.aliases.get(audio);
3671
+ if (managedSound) {
3672
+ removeSound(managedSound.library, managedSound.alias);
3673
+ this.aliases.delete(audio);
3674
+ } else {
3675
+ destroySound(audio);
3676
+ }
3581
3677
  remove(this.audios, audio);
3582
3678
  }
3583
3679
  /**
@@ -3601,6 +3697,7 @@ class SoundManager {
3601
3697
  __publicField(SoundManager, "audios", []);
3602
3698
  __publicField(SoundManager, "analysers", []);
3603
3699
  __publicField(SoundManager, "contexts", []);
3700
+ __publicField(SoundManager, "aliases", /* @__PURE__ */ new WeakMap());
3604
3701
  __publicField(SoundManager, "_volume", VOLUME);
3605
3702
  var MotionPreloadStrategy = /* @__PURE__ */ ((MotionPreloadStrategy2) => {
3606
3703
  MotionPreloadStrategy2["ALL"] = "ALL";
@@ -3785,7 +3882,7 @@ class MotionManager extends EventEmitter {
3785
3882
  * @returns Promise that resolves with true if the sound is playing, false otherwise.
3786
3883
  */
3787
3884
  speak(_0) {
3788
- return __async(this, arguments, function* (sound2, {
3885
+ return __async(this, arguments, function* (sound, {
3789
3886
  volume = VOLUME,
3790
3887
  expression,
3791
3888
  resetExpression = true,
@@ -3802,16 +3899,16 @@ class MotionManager extends EventEmitter {
3802
3899
  }
3803
3900
  }
3804
3901
  let soundURL;
3805
- const isBase64Content = sound2 && sound2.startsWith("data:");
3806
- if (sound2 && !isBase64Content) {
3902
+ const isBase64Content = sound && sound.startsWith("data:");
3903
+ if (sound && !isBase64Content) {
3807
3904
  const A = document.createElement("a");
3808
- A.href = sound2;
3809
- sound2 = A.href;
3810
- soundURL = sound2;
3905
+ A.href = sound;
3906
+ sound = A.href;
3907
+ soundURL = sound;
3811
3908
  } else {
3812
3909
  soundURL = "data:audio/";
3813
3910
  }
3814
- const file = sound2;
3911
+ const file = sound;
3815
3912
  if (file) {
3816
3913
  try {
3817
3914
  audio = yield SoundManager.add(file, (e, that = this) => {
@@ -3822,6 +3919,9 @@ class MotionManager extends EventEmitter {
3822
3919
  }
3823
3920
  that.currentAudio = void 0;
3824
3921
  });
3922
+ if (!audio) {
3923
+ return false;
3924
+ }
3825
3925
  this.initializeAudio(audio, volume);
3826
3926
  } catch (e) {
3827
3927
  logger.warn(this.tag, "Failed to create audio", soundURL, e);
@@ -3871,17 +3971,19 @@ class MotionManager extends EventEmitter {
3871
3971
  * @param onFinish - Callback when playback finishes.
3872
3972
  * @param onError - Callback when playback errors.
3873
3973
  * @param ignoreParamIds - The ids to be ignored.
3974
+ * @param loop - Whether the motion should loop. Overrides Cubism 3/4/5 motion JSON loop metadata when specified.
3874
3975
  * @return Promise that resolves with true if the motion is successfully started, false otherwise.
3875
3976
  */
3876
3977
  startMotion(_0, _1) {
3877
3978
  return __async(this, arguments, function* (group, index, priority = MotionPriority.NORMAL, {
3878
- sound: sound2 = void 0,
3979
+ sound = void 0,
3879
3980
  volume = VOLUME,
3880
3981
  expression = void 0,
3881
3982
  resetExpression = true,
3882
3983
  onFinish,
3883
3984
  onError,
3884
- ignoreParamIds = []
3985
+ ignoreParamIds = [],
3986
+ loop = void 0
3885
3987
  } = {}) {
3886
3988
  if (this.destroyed) {
3887
3989
  return false;
@@ -3903,12 +4005,12 @@ class MotionManager extends EventEmitter {
3903
4005
  }
3904
4006
  let audio;
3905
4007
  let soundURL;
3906
- const isBase64Content = sound2 && sound2.startsWith("data:");
3907
- if (sound2 && !isBase64Content) {
4008
+ const isBase64Content = sound && sound.startsWith("data:");
4009
+ if (sound && !isBase64Content) {
3908
4010
  const A = document.createElement("a");
3909
- A.href = sound2;
3910
- sound2 = A.href;
3911
- soundURL = sound2;
4011
+ A.href = sound;
4012
+ sound = A.href;
4013
+ soundURL = sound;
3912
4014
  } else {
3913
4015
  soundURL = this.getSoundFile(definition);
3914
4016
  if (soundURL) {
@@ -3926,7 +4028,9 @@ class MotionManager extends EventEmitter {
3926
4028
  }
3927
4029
  that.currentAudio = void 0;
3928
4030
  });
3929
- this.initializeAudio(audio, volume);
4031
+ if (audio) {
4032
+ this.initializeAudio(audio, volume);
4033
+ }
3930
4034
  } catch (e) {
3931
4035
  logger.warn(this.tag, "Failed to create audio", soundURL, e);
3932
4036
  }
@@ -3964,7 +4068,7 @@ class MotionManager extends EventEmitter {
3964
4068
  }
3965
4069
  this.playing = true;
3966
4070
  if (motion) {
3967
- this._startMotion(motion, void 0, ignoreParamIds);
4071
+ this._startMotion(motion, void 0, ignoreParamIds, loop);
3968
4072
  }
3969
4073
  return true;
3970
4074
  });
@@ -3980,16 +4084,18 @@ class MotionManager extends EventEmitter {
3980
4084
  * @param crossOrigin - Cross origin setting.
3981
4085
  * @param onFinish - Callback when playback finishes.
3982
4086
  * @param onError - Callback when playback errors.
4087
+ * @param loop - Whether the motion should loop. Overrides Cubism 3/4/5 motion JSON loop metadata when specified.
3983
4088
  * @return Promise that resolves with true if the motion is successfully started, false otherwise.
3984
4089
  */
3985
4090
  startRandomMotion(_0, _1) {
3986
4091
  return __async(this, arguments, function* (group, priority, {
3987
- sound: sound2,
4092
+ sound,
3988
4093
  volume = VOLUME,
3989
4094
  expression,
3990
4095
  resetExpression = true,
3991
4096
  onFinish,
3992
- onError
4097
+ onError,
4098
+ loop = void 0
3993
4099
  } = {}) {
3994
4100
  var _a, _b;
3995
4101
  if (this.destroyed) {
@@ -4007,12 +4113,13 @@ class MotionManager extends EventEmitter {
4007
4113
  if (availableIndices.length) {
4008
4114
  const index = availableIndices[Math.floor(Math.random() * availableIndices.length)];
4009
4115
  return this.startMotion(group, index, priority, {
4010
- sound: sound2,
4116
+ sound,
4011
4117
  volume,
4012
4118
  expression,
4013
4119
  resetExpression,
4014
4120
  onFinish,
4015
- onError
4121
+ onError,
4122
+ loop
4016
4123
  });
4017
4124
  }
4018
4125
  }
@@ -5855,34 +5962,38 @@ class Live2DModel extends Container {
5855
5962
  * @param [options.resetExpression=true] - Reset the expression to default after the motion is finished.
5856
5963
  * @param [options.onFinish] - Callback function when speaking completes.
5857
5964
  * @param [options.onError] - Callback function when an error occurs.
5965
+ * @param [options.loop] - Whether the motion should loop. Overrides Cubism 3/4/5 motion JSON loop metadata when specified.
5858
5966
  * @return Promise that resolves with true if the motion is successfully started, with false otherwise.
5859
5967
  */
5860
5968
  motion(_0, _1, _2) {
5861
5969
  return __async(this, arguments, function* (group, index, priority, {
5862
- sound: sound2 = void 0,
5970
+ sound = void 0,
5863
5971
  volume = VOLUME,
5864
5972
  expression = void 0,
5865
5973
  resetExpression = true,
5866
5974
  onFinish,
5867
- onError
5975
+ onError,
5976
+ loop = void 0
5868
5977
  } = {}) {
5869
5978
  if (index === void 0) {
5870
5979
  return this.internalModel.motionManager.startRandomMotion(group, priority, {
5871
- sound: sound2,
5980
+ sound,
5872
5981
  volume,
5873
5982
  expression,
5874
5983
  resetExpression,
5875
5984
  onFinish,
5876
- onError
5985
+ onError,
5986
+ loop
5877
5987
  });
5878
5988
  } else {
5879
5989
  return this.internalModel.motionManager.startMotion(group, index, priority, {
5880
- sound: sound2,
5990
+ sound,
5881
5991
  volume,
5882
5992
  expression,
5883
5993
  resetExpression,
5884
5994
  onFinish,
5885
- onError
5995
+ onError,
5996
+ loop
5886
5997
  });
5887
5998
  }
5888
5999
  });
@@ -5900,13 +6011,18 @@ class Live2DModel extends Container {
5900
6011
  * group: The motion group,
5901
6012
  * index: Index in the motion group,
5902
6013
  * priority: The priority to be applied. (0: No priority, 1: IDLE, 2:NORMAL, 3:FORCE) (default: 2)
6014
+ * ignoreParamIds: The ids to be ignored,
6015
+ * loop: Whether the motion should loop.
5903
6016
  * @return Promise that resolves with a list, indicates the motion is successfully started, with false otherwise.
5904
6017
  */
5905
6018
  parallelMotion(motionList) {
5906
6019
  return __async(this, null, function* () {
5907
6020
  this.internalModel.extendParallelMotionManager(motionList.length);
5908
6021
  const result = motionList.map(
5909
- (m, idx) => this.internalModel.parallelMotionManager[idx].startMotion(m.group, m.index, m.priority)
6022
+ (m, idx) => this.internalModel.parallelMotionManager[idx].startMotion(m.group, m.index, m.priority, {
6023
+ ignoreParamIds: m.ignoreParamIds,
6024
+ loop: m.loop
6025
+ })
5910
6026
  );
5911
6027
  const flags = [];
5912
6028
  for (const r of result) {
@@ -5960,14 +6076,14 @@ class Live2DModel extends Container {
5960
6076
  * @param [options.onError] - Callback function when an error occurs.
5961
6077
  * @returns Promise that resolves with true if the sound is playing, false if it's not.
5962
6078
  */
5963
- speak(sound2, {
6079
+ speak(sound, {
5964
6080
  volume = VOLUME,
5965
6081
  expression,
5966
6082
  resetExpression = true,
5967
6083
  onFinish,
5968
6084
  onError
5969
6085
  } = {}) {
5970
- return this.internalModel.motionManager.speak(sound2, {
6086
+ return this.internalModel.motionManager.speak(sound, {
5971
6087
  volume,
5972
6088
  expression,
5973
6089
  resetExpression,
@@ -8741,8 +8857,9 @@ class CubismMotionManager extends MotionManager {
8741
8857
  isFinished() {
8742
8858
  return this.queueManager.isFinished();
8743
8859
  }
8744
- _startMotion(motion, onFinish, ignoreParamIds) {
8860
+ _startMotion(motion, onFinish, ignoreParamIds, loop) {
8745
8861
  motion.setFinishedMotionHandler(onFinish);
8862
+ motion.setLoop(loop != null ? loop : motion._motionData.loop);
8746
8863
  if (ignoreParamIds && ignoreParamIds.length > 0) {
8747
8864
  const curves = motion._motionData.curves;
8748
8865
  const filtered = createCurveContainer$1(curves);
@@ -8907,11 +9024,13 @@ class ParallelMotionManager extends EventEmitter {
8907
9024
  * @param index - Index in the motion group.
8908
9025
  * @param priority - The priority to be applied. default: 2 (NORMAL)
8909
9026
  * @param ignoreParamIds - The ids to be ignored.
9027
+ * @param loop - Whether the motion should loop. Overrides Cubism 3/4/5 motion JSON loop metadata when specified.
8910
9028
  * @return Promise that resolves with true if the motion is successfully started, with false otherwise.
8911
9029
  */
8912
9030
  startMotion(_0, _1) {
8913
- return __async(this, arguments, function* (group, index, priority = MotionPriority.NORMAL, ignoreParamIds = []) {
9031
+ return __async(this, arguments, function* (group, index, priority = MotionPriority.NORMAL, options = {}) {
8914
9032
  var _a;
9033
+ const { ignoreParamIds = [], loop } = Array.isArray(options) ? { ignoreParamIds: options, loop: void 0 } : options;
8915
9034
  if (!this.state.reserve(group, index, priority)) {
8916
9035
  return false;
8917
9036
  }
@@ -8926,7 +9045,7 @@ class ParallelMotionManager extends EventEmitter {
8926
9045
  logger.log(this.tag, "Start motion:", this.getMotionName(definition));
8927
9046
  this.emit("motionStart", group, index, void 0);
8928
9047
  this.playing = true;
8929
- this._startMotion(motion, void 0, ignoreParamIds);
9048
+ this._startMotion(motion, void 0, ignoreParamIds, loop);
8930
9049
  return true;
8931
9050
  });
8932
9051
  }
@@ -8934,10 +9053,11 @@ class ParallelMotionManager extends EventEmitter {
8934
9053
  * Starts a random Motion as given priority.
8935
9054
  * @param group - The motion group.
8936
9055
  * @param priority - The priority to be applied. (default: 1 `IDLE`)
9056
+ * @param loop - Whether the motion should loop. Overrides Cubism 3/4/5 motion JSON loop metadata when specified.
8937
9057
  * @return Promise that resolves with true if the motion is successfully started, with false otherwise.
8938
9058
  */
8939
- startRandomMotion(group, priority) {
8940
- return __async(this, null, function* () {
9059
+ startRandomMotion(_0, _1) {
9060
+ return __async(this, arguments, function* (group, priority, { loop = void 0 } = {}) {
8941
9061
  var _a;
8942
9062
  const groupDefs = this.manager.definitions[group];
8943
9063
  if (groupDefs == null ? void 0 : groupDefs.length) {
@@ -8950,7 +9070,7 @@ class ParallelMotionManager extends EventEmitter {
8950
9070
  }
8951
9071
  if (availableIndices.length) {
8952
9072
  const index = availableIndices[Math.floor(Math.random() * availableIndices.length)];
8953
- return this.startMotion(group, index, priority);
9073
+ return this.startMotion(group, index, priority, { loop });
8954
9074
  }
8955
9075
  }
8956
9076
  return false;
@@ -9004,8 +9124,9 @@ class CubismParallelMotionManager extends ParallelMotionManager {
9004
9124
  isFinished() {
9005
9125
  return this.queueManager.isFinished();
9006
9126
  }
9007
- _startMotion(motion, onFinish, ignoreParamIds) {
9127
+ _startMotion(motion, onFinish, ignoreParamIds, loop) {
9008
9128
  motion.setFinishedMotionHandler(onFinish);
9129
+ motion.setLoop(loop != null ? loop : motion._motionData.loop);
9009
9130
  if (ignoreParamIds && ignoreParamIds.length > 0) {
9010
9131
  const curves = motion._motionData.curves;
9011
9132
  const filtered = createCurveContainer(curves);
@@ -11609,7 +11730,7 @@ class CubismInternalModel extends InternalModel {
11609
11730
  var _a;
11610
11731
  super.init();
11611
11732
  const eyeBlinkParameters = this.settings.getEyeBlinkParameters();
11612
- if (eyeBlinkParameters.length) {
11733
+ if (this.options.eyeBlink !== false && eyeBlinkParameters.length) {
11613
11734
  const parameterIds = new csmVector();
11614
11735
  for (const parameter of eyeBlinkParameters) {
11615
11736
  parameterIds.pushBack(this.idManager.getId(parameter));
@@ -12625,18 +12746,26 @@ class CubismModelSettings extends ModelSettings {
12625
12746
  parameters.push(id.getString().s);
12626
12747
  }
12627
12748
  }
12628
- } else if (Array.isArray(this.json.Groups)) {
12749
+ } else {
12750
+ let hasEyeBlinkGroup = false;
12629
12751
  const groups = this.json.Groups;
12630
- for (const group of groups) {
12631
- if ((group == null ? void 0 : group.Name) !== "EyeBlink" || !Array.isArray(group.Ids)) continue;
12632
- for (const entry of group.Ids) {
12633
- if (typeof entry === "string") {
12634
- parameters.push(entry);
12635
- } else if (entry == null ? void 0 : entry.Id) {
12636
- parameters.push(entry.Id);
12752
+ if (Array.isArray(groups)) {
12753
+ for (const group of groups) {
12754
+ if ((group == null ? void 0 : group.Name) !== "EyeBlink") continue;
12755
+ hasEyeBlinkGroup = true;
12756
+ if (!Array.isArray(group.Ids)) continue;
12757
+ for (const entry of group.Ids) {
12758
+ if (typeof entry === "string") {
12759
+ parameters.push(entry);
12760
+ } else if (entry == null ? void 0 : entry.Id) {
12761
+ parameters.push(entry.Id);
12762
+ }
12637
12763
  }
12638
12764
  }
12639
12765
  }
12766
+ if (hasEyeBlinkGroup) {
12767
+ return parameters;
12768
+ }
12640
12769
  }
12641
12770
  return parameters.length ? parameters : [...defaultEyeBlinkParameters];
12642
12771
  }