untitled-pixi-live2d-engine 1.2.2 → 1.3.0

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.0";
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
  });
@@ -5960,14 +6071,14 @@ class Live2DModel extends Container {
5960
6071
  * @param [options.onError] - Callback function when an error occurs.
5961
6072
  * @returns Promise that resolves with true if the sound is playing, false if it's not.
5962
6073
  */
5963
- speak(sound2, {
6074
+ speak(sound, {
5964
6075
  volume = VOLUME,
5965
6076
  expression,
5966
6077
  resetExpression = true,
5967
6078
  onFinish,
5968
6079
  onError
5969
6080
  } = {}) {
5970
- return this.internalModel.motionManager.speak(sound2, {
6081
+ return this.internalModel.motionManager.speak(sound, {
5971
6082
  volume,
5972
6083
  expression,
5973
6084
  resetExpression,
@@ -8741,8 +8852,9 @@ class CubismMotionManager extends MotionManager {
8741
8852
  isFinished() {
8742
8853
  return this.queueManager.isFinished();
8743
8854
  }
8744
- _startMotion(motion, onFinish, ignoreParamIds) {
8855
+ _startMotion(motion, onFinish, ignoreParamIds, loop) {
8745
8856
  motion.setFinishedMotionHandler(onFinish);
8857
+ motion.setLoop(loop != null ? loop : motion._motionData.loop);
8746
8858
  if (ignoreParamIds && ignoreParamIds.length > 0) {
8747
8859
  const curves = motion._motionData.curves;
8748
8860
  const filtered = createCurveContainer$1(curves);
@@ -9004,8 +9116,9 @@ class CubismParallelMotionManager extends ParallelMotionManager {
9004
9116
  isFinished() {
9005
9117
  return this.queueManager.isFinished();
9006
9118
  }
9007
- _startMotion(motion, onFinish, ignoreParamIds) {
9119
+ _startMotion(motion, onFinish, ignoreParamIds, loop) {
9008
9120
  motion.setFinishedMotionHandler(onFinish);
9121
+ motion.setLoop(loop != null ? loop : motion._motionData.loop);
9009
9122
  if (ignoreParamIds && ignoreParamIds.length > 0) {
9010
9123
  const curves = motion._motionData.curves;
9011
9124
  const filtered = createCurveContainer(curves);
@@ -11609,7 +11722,7 @@ class CubismInternalModel extends InternalModel {
11609
11722
  var _a;
11610
11723
  super.init();
11611
11724
  const eyeBlinkParameters = this.settings.getEyeBlinkParameters();
11612
- if (eyeBlinkParameters.length) {
11725
+ if (this.options.eyeBlink !== false && eyeBlinkParameters.length) {
11613
11726
  const parameterIds = new csmVector();
11614
11727
  for (const parameter of eyeBlinkParameters) {
11615
11728
  parameterIds.pushBack(this.idManager.getId(parameter));
@@ -12625,18 +12738,26 @@ class CubismModelSettings extends ModelSettings {
12625
12738
  parameters.push(id.getString().s);
12626
12739
  }
12627
12740
  }
12628
- } else if (Array.isArray(this.json.Groups)) {
12741
+ } else {
12742
+ let hasEyeBlinkGroup = false;
12629
12743
  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);
12744
+ if (Array.isArray(groups)) {
12745
+ for (const group of groups) {
12746
+ if ((group == null ? void 0 : group.Name) !== "EyeBlink") continue;
12747
+ hasEyeBlinkGroup = true;
12748
+ if (!Array.isArray(group.Ids)) continue;
12749
+ for (const entry of group.Ids) {
12750
+ if (typeof entry === "string") {
12751
+ parameters.push(entry);
12752
+ } else if (entry == null ? void 0 : entry.Id) {
12753
+ parameters.push(entry.Id);
12754
+ }
12637
12755
  }
12638
12756
  }
12639
12757
  }
12758
+ if (hasEyeBlinkGroup) {
12759
+ return parameters;
12760
+ }
12640
12761
  }
12641
12762
  return parameters.length ? parameters : [...defaultEyeBlinkParameters];
12642
12763
  }