Skip to content

Commit

Permalink
Merge branch 'update'
Browse files Browse the repository at this point in the history
  • Loading branch information
RaSan147 committed Apr 10, 2024
2 parents 75d50c9 + 0409b60 commit 8ba8fa5
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 73 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
ref: ${{github.ref_name}}
submodules: true
fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.

- uses: actions/setup-node@v3
with:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Live2D models on a high level without the need to learn how the internal system

#### Requirements

- PixiJS: 6.x
- PixiJS: 7.x
- Cubism core: 2.1 or 4
- Browser: WebGL, ES6

Expand Down Expand Up @@ -108,7 +108,7 @@ import { Live2DModel } from 'pixi-live2d-display/cubism4';
<!-- Load Cubism and PixiJS -->
<script src="https://cubism.live2d.com/sdk-web/cubismcore/live2dcubismcore.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/dylanNew/live2d/webgl/Live2D/lib/live2d.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/browser/pixi.min.js"></script>
<script src="https://pixijs.download/v7.4.0/pixi.min.js"></script>


<!-- if support for both Cubism 2.1 and 4 -->
Expand Down Expand Up @@ -172,7 +172,7 @@ window.PIXI = PIXI;
* Time to code
```js
var category_name = "Idle" // name of the morion category
var animation_index = 0 // index of animation under that motion category
var animation_index = 0 // index of animation under that motion category [null => random]
var priority_number = 3 // if you want to keep the current animation going or move to new animation by force [0: no priority, 1: idle, 2: normal, 3: forced]
var audio_link = "https://cdn.jsdelivr.net/gh/RaSan147/[email protected]/playground/test.mp3" //[Optional arg, can be null or empty] [relative or full url path] [mp3 or wav file]
var volume = 1; //[Optional arg, can be null or empty] [0.0 - 1.0]
Expand Down
7 changes: 3 additions & 4 deletions src/Live2DModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { InternalModel, ModelSettings } from "@/cubism-common";
import { MotionPriority } from "@/cubism-common";
import type { InternalModel, ModelSettings, MotionPriority } from "@/cubism-common";
import type { MotionManagerOptions } from "@/cubism-common/MotionManager";
import { VOLUME } from "@/cubism-common/SoundManager";
import type { Live2DFactoryOptions } from "@/factory/Live2DFactory";
Expand Down Expand Up @@ -168,8 +167,8 @@ export class Live2DModel<IM extends InternalModel = InternalModel> extends Conta
*/
motion(
group: string,
index: number,
priority: MotionPriority = MotionPriority.NORMAL,
index?: number,
priority?: MotionPriority,
{
sound = undefined,
volume = VOLUME,
Expand Down
112 changes: 55 additions & 57 deletions src/cubism-common/MotionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export abstract class MotionManager<Motion = any, MotionSpec = any> extends util
}

if (audio) {
let playSuccess = false;
let playSuccess = true;
const readyToPlay = SoundManager.play(audio).catch((e) => {
logger.warn(this.tag, "Failed to play audio", audio!.src, e);
playSuccess = false;
Expand Down Expand Up @@ -361,17 +361,16 @@ export abstract class MotionManager<Motion = any, MotionSpec = any> extends util
crossOrigin?: string;
} = {},
): Promise<boolean> {
if (!this.state.reserve(group, index, priority)) {
return false;
}
// Does not start a new motion if audio is still playing
if (this.currentAudio) {
if (!this.currentAudio.ended) {
if (!this.currentAudio.ended && priority != MotionPriority.FORCE) {
return false;
}
}

if (!this.state.reserve(group, index, priority)) {
return false;
}

const definition = this.definitions[group]?.[index];

if (!definition) {
Expand All @@ -387,59 +386,58 @@ export abstract class MotionManager<Motion = any, MotionSpec = any> extends util
let analyzer: AnalyserNode | undefined;
let context: AudioContext | undefined;

if (config.sound) {
const isBase64Content = sound && sound.startsWith("data:audio/wav;base64");
if (sound && !isBase64Content) {
const A = document.createElement("a");
A.href = sound;
sound = A.href; // This should be the absolute url
// since resolveURL is not working for some reason
}
const isUrlPath = sound && (sound.startsWith("http") || sound.startsWith("blob"));
const soundURL = this.getSoundFile(definition);
let file = soundURL;
let soundURL: string | undefined;
const isBase64Content = sound && sound.startsWith("data:");

if (sound && !isBase64Content) {
const A = document.createElement("a");
A.href = sound;
sound = A.href; // This should be the absolute url
// since resolveURL is not working for some reason
soundURL = sound;
} else {
soundURL = this.getSoundFile(definition);
if (soundURL) {
file = this.settings.resolveURL(soundURL) + "?cache-buster=" + new Date().getTime();
soundURL = this.settings.resolveURL(soundURL);
}
if (isUrlPath || isBase64Content) {
file = sound;
}
if (file) {
try {
// start to load the audio
audio = SoundManager.add(
file,
(that = this) => {
resetExpression &&
expression &&
that.expressionManager &&
that.expressionManager.resetExpression();
that.currentAudio = undefined;
}, // reset expression when audio is done
(e, that = this) => {
resetExpression &&
expression &&
that.expressionManager &&
that.expressionManager.resetExpression();
that.currentAudio = undefined;
}, // on error
crossOrigin,
);

this.currentAudio = audio!;

SoundManager.volume = volume;

// Add context
context = SoundManager.addContext(this.currentAudio);
this.currentContext = context;

// Add analyzer
analyzer = SoundManager.addAnalyzer(this.currentAudio, this.currentContext);
this.currentAnalyzer = analyzer;
} catch (e) {
logger.warn(this.tag, "Failed to create audio", soundURL, e);
}
}
const file: string | undefined = soundURL;

if (file) {
try {
// start to load the audio
audio = SoundManager.add(
file,
(that = this) => {
resetExpression &&
expression &&
that.expressionManager &&
that.expressionManager.resetExpression();
that.currentAudio = undefined;
}, // reset expression when audio is done
(e, that = this) => {
resetExpression &&
expression &&
that.expressionManager &&
that.expressionManager.resetExpression();
that.currentAudio = undefined;
}, // on error
crossOrigin,
);

this.currentAudio = audio!;

SoundManager.volume = volume;

// Add context
context = SoundManager.addContext(this.currentAudio);
this.currentContext = context;

// Add analyzer
analyzer = SoundManager.addAnalyzer(this.currentAudio, this.currentContext);
this.currentAnalyzer = analyzer;
} catch (e) {
logger.warn(this.tag, "Failed to create audio", soundURL, e);
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/cubism2/Cubism2InternalModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Cubism2InternalModel extends InternalModel {
angleZParamIndex: number;
bodyAngleXParamIndex: number;
breathParamIndex: number;
mouthFormIndex: number;
// mouthFormIndex: number;

textureFlipY = true;

Expand Down Expand Up @@ -74,7 +74,7 @@ export class Cubism2InternalModel extends InternalModel {
this.angleZParamIndex = coreModel.getParamIndex("PARAM_ANGLE_Z");
this.bodyAngleXParamIndex = coreModel.getParamIndex("PARAM_BODY_ANGLE_X");
this.breathParamIndex = coreModel.getParamIndex("PARAM_BREATH");
this.mouthFormIndex = coreModel.getParamIndex("PARAM_MOUTH_FORM");
// this.mouthFormIndex = coreModel.getParamIndex("PARAM_MOUTH_FORM");

this.init();
}
Expand Down Expand Up @@ -250,11 +250,13 @@ export class Cubism2InternalModel extends InternalModel {
let value = this.motionManager.mouthSync();
let min_ = 0;
const max_ = 1;
const weight = 1.2;
if (value > 0) {
const bias_weight = 1.2;
const bias_power = 0.7;
if (value > 0.0) {
min_ = 0.4;
}
value = clamp(value * weight, min_, max_);
value = Math.pow(value, bias_power);
value = clamp(value * bias_weight, min_, max_);

for (let i = 0; i < this.motionManager.lipSyncIds.length; ++i) {
this.coreModel.setParamFloat(
Expand Down
8 changes: 6 additions & 2 deletions src/cubism4/Cubism4MotionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ export class Cubism4MotionManager extends MotionManager<CubismMotion, CubismSpec
expressionManager?: Cubism4ExpressionManager;

eyeBlinkIds: string[];
lipSyncIds: string[];
lipSyncIds = ["ParamMouthOpenY"];

constructor(settings: Cubism4ModelSettings, options?: MotionManagerOptions) {
super(settings, options);

this.definitions = settings.motions ?? {};
this.eyeBlinkIds = settings.getEyeBlinkParameters() || [];
this.lipSyncIds = settings.getLipSyncParameters() || [];

const lipSyncIds = settings.getLipSyncParameters();

if (lipSyncIds?.length) {
this.lipSyncIds = lipSyncIds;
}
this.init(options);
}

Expand Down

0 comments on commit 8ba8fa5

Please sign in to comment.