Skip to content

Commit 156ba1f

Browse files
Merge pull request #8 from Live2D/develop
Update to Cubism 4 SDK for Java R1
2 parents e75235c + 0c50687 commit 156ba1f

37 files changed

+15176
-16209
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

7+
## [4-r.1] - 2023-05-25
8+
9+
### Added
10+
11+
* Add some functions for checking consistency of MOC3 files.
12+
* Add the function of checking consistency in `setupModel()` in `LAppModel` and `LAppMinimumModel`.
13+
* Add the function of checking consistency before loading a model. (`hasMocConsistencyFromFile()` in `LAppModel` and `LAppMinimumModel`)
14+
* This feature is enabled by default. Please see the following manual for more information.
15+
* https://docs.live2d.com/cubism-sdk-manual/moc3-consistency/
16+
17+
### Changed
18+
19+
* Change so that when `USE_MODEL_RENDER_TARGET` is defined, one model will apply the opacity obtained from the motion.
20+
21+
### Removed
22+
23+
* Remove unnecessary files in the assets folder.
24+
25+
### Fixed
26+
27+
* Fix a problem in which `haru` motion and voice were incorrect combination.
28+
* Fix a problem in `LAppWavFileHandler`class that some audio could not be played correctly because the sampling rate was the fixed value.
29+
730
## [4-r.1-beta.4] - 2023-03-16
831

932
### Fixed
@@ -52,6 +75,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
5275

5376
* New released!
5477

78+
[4-r.1]: https://github.com/Live2D/CubismJavaSamples/compare/4-r.1-beta.4...4-r.1
5579
[4-r.1-beta.4]: https://github.com/Live2D/CubismJavaSamples/compare/4-r.1-beta.3...4-r.1-beta.4
5680
[4-r.1-beta.3]: https://github.com/Live2D/CubismJavaSamples/compare/4-r.1-beta.2...4-r.1-beta.3
5781
[4-r.1-beta.2]: https://github.com/Live2D/CubismJavaSamples/compare/4-r.1-beta.1...4-r.1-beta.2

README.ja.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ Android Studioでプロジェクトを開きビルドすることを推奨しま
5959

6060
| 開発ツール | バージョン |
6161
|----------------|------------------|
62-
| Android Studio | Electric Eel 2022.1.1 |
63-
| IntelliJ IDEA | 2022.3.1 |
62+
| Android Studio | Flamingo 2022.2.1 Patch 1 |
63+
| IntelliJ IDEA | 2023.1.1 |
6464
| CMake | 3.1.0 |
6565
| Gradle | 6.9 |
6666

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ Please refer to [CHANGELOG.md](CHANGELOG.md) for the changelog of this repositor
5959

6060
| Development Tools | Version |
6161
|-------------------|--|
62-
| Android Studio | Electric Eel 2022.1.1 |
63-
| IntelliJ IDEA | 2022.3.1 |
62+
| Android Studio | Flamingo 2022.2.1 Patch 1 |
63+
| IntelliJ IDEA | 2023.1.1 |
6464
| CMake | 3.1.0 |
6565
| Gradle | 6.9 |
6666

Sample/src/full/java/com/live2d/demo/full/LAppModel.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.live2d.sdk.cubism.framework.id.CubismId;
1818
import com.live2d.sdk.cubism.framework.id.CubismIdManager;
1919
import com.live2d.sdk.cubism.framework.math.CubismMatrix44;
20+
import com.live2d.sdk.cubism.framework.model.CubismMoc;
2021
import com.live2d.sdk.cubism.framework.model.CubismUserModel;
2122
import com.live2d.sdk.cubism.framework.motion.ACubismMotion;
2223
import com.live2d.sdk.cubism.framework.motion.CubismExpressionMotion;
@@ -25,11 +26,20 @@
2526
import com.live2d.sdk.cubism.framework.rendering.CubismRenderer;
2627
import com.live2d.sdk.cubism.framework.rendering.android.CubismOffscreenSurfaceAndroid;
2728
import com.live2d.sdk.cubism.framework.rendering.android.CubismRendererAndroid;
29+
import com.live2d.sdk.cubism.framework.utils.CubismDebug;
2830

29-
import java.util.*;
31+
import java.util.ArrayList;
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
35+
import java.util.Random;
3036

3137
public class LAppModel extends CubismUserModel {
3238
public LAppModel() {
39+
if (LAppDefine.MOC_CONSISTENCY_VALIDATION_ENABLE) {
40+
mocConsistency = true;
41+
}
42+
3343
if (LAppDefine.DEBUG_LOG_ENABLE) {
3444
debugMode = true;
3545
}
@@ -107,6 +117,9 @@ public void update() {
107117
// モデルの状態を保存
108118
model.saveParameters();
109119

120+
// 不透明度
121+
opacity = model.getModelOpacity();
122+
110123
// eye blink
111124
// メインモーションの更新がないときだけまばたきする
112125
if (!isMotionUpdated) {
@@ -363,6 +376,30 @@ public CubismOffscreenSurfaceAndroid getRenderingBuffer() {
363376
return renderingBuffer;
364377
}
365378

379+
/**
380+
* .moc3ファイルの整合性をチェックする。
381+
*
382+
* @param mocFileName MOC3ファイル名
383+
* @return MOC3に整合性があるかどうか。整合性があればtrue。
384+
*/
385+
public boolean hasMocConsistencyFromFile(String mocFileName) {
386+
assert mocFileName != null && !mocFileName.isEmpty();
387+
388+
String path = mocFileName;
389+
path = modelHomeDirectory + path;
390+
391+
byte[] buffer = createBuffer(path);
392+
boolean consistency = CubismMoc.hasMocConsistency(buffer);
393+
394+
if (!consistency) {
395+
CubismDebug.cubismLogInfo("Inconsistent MOC3.");
396+
} else {
397+
CubismDebug.cubismLogInfo("Consistent MOC3.");
398+
}
399+
400+
return consistency;
401+
}
402+
366403
private static byte[] createBuffer(final String path) {
367404
if (LAppDefine.DEBUG_LOG_ENABLE) {
368405
LAppPal.printLog("create buffer: " + path);
@@ -388,7 +425,7 @@ private void setupModel(ICubismModelSetting setting) {
388425
}
389426

390427
byte[] buffer = createBuffer(path);
391-
loadModel(buffer);
428+
loadModel(buffer, mocConsistency);
392429
}
393430
}
394431

@@ -576,7 +613,6 @@ private void setupTextures() {
576613
}
577614
}
578615

579-
580616
private ICubismModelSetting modelSetting;
581617
/**
582618
* モデルのホームディレクトリ

Sample/src/full/java/com/live2d/demo/full/LAppView.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ public void render() {
166166
};
167167

168168
for (int i = 0; i < live2dManager.getModelNum(); i++) {
169-
// サンプルとしてαに適当な差を付ける。
170-
float alpha = getSpriteAlpha(i);
169+
LAppModel model = live2dManager.getModel(i);
170+
float alpha = i < 1 ? 1.0f : model.getOpacity(); // 片方のみ不透明度を取得できるようにする。
171+
171172
renderingSprite.setColor(1.0f, 1.0f, 1.0f, alpha);
172173

173-
LAppModel model = live2dManager.getModel(i);
174174
if (model != null) {
175175
renderingSprite.renderImmediate(model.getRenderingBuffer().getColorBuffer()[0], uvVertex);
176176
}

Sample/src/full/java/com/live2d/demo/full/LAppWavFileHandler.java

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99

1010
package com.live2d.demo.full;
1111

12+
import android.content.res.AssetFileDescriptor;
1213
import android.media.AudioAttributes;
1314
import android.media.AudioFormat;
1415
import android.media.AudioTrack;
16+
import android.media.MediaExtractor;
17+
import android.media.MediaFormat;
18+
import android.os.Build;
19+
20+
import java.io.IOException;
1521

1622
public class LAppWavFileHandler extends Thread {
1723
public LAppWavFileHandler(String filePath) {
@@ -24,33 +30,49 @@ public void run() {
2430
}
2531

2632
public void loadWavFile() {
27-
byte[] voiceBuffer = LAppPal.loadFileAsBytes(filePath);
33+
// 対応していないAPI(API24未満)の場合は音声再生を行わない。
34+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
35+
return;
36+
}
37+
38+
MediaExtractor mediaExtractor = new MediaExtractor();
39+
try {
40+
AssetFileDescriptor afd = LAppDelegate.getInstance().getActivity().getAssets().openFd(filePath);
41+
mediaExtractor.setDataSource(afd);
42+
} catch (IOException e) {
43+
// 例外が発生したらエラーだけだして再生せずreturnする。
44+
e.printStackTrace();
45+
return;
46+
}
47+
48+
MediaFormat mf = mediaExtractor.getTrackFormat(0);
49+
int samplingRate = mf.getInteger(MediaFormat.KEY_SAMPLE_RATE);
2850

2951
int bufferSize = AudioTrack.getMinBufferSize(
30-
88187,
52+
samplingRate,
3153
AudioFormat.CHANNEL_OUT_MONO,
3254
AudioFormat.ENCODING_PCM_16BIT
3355
);
3456

35-
AudioTrack audioTrack = null;
36-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
37-
audioTrack = new AudioTrack.Builder()
38-
.setAudioAttributes(new AudioAttributes.Builder()
39-
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
40-
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
41-
.build())
42-
.setAudioFormat(new AudioFormat.Builder()
43-
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
44-
.setSampleRate(88187)
45-
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
46-
.build())
47-
.setBufferSizeInBytes(bufferSize)
48-
.build();
49-
audioTrack.play();
50-
51-
int offset = 100;
52-
audioTrack.write(voiceBuffer, offset, voiceBuffer.length - offset);
53-
}
57+
AudioTrack audioTrack;
58+
audioTrack = new AudioTrack.Builder()
59+
.setAudioAttributes(new AudioAttributes.Builder()
60+
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
61+
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
62+
.build())
63+
.setAudioFormat(new AudioFormat.Builder()
64+
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
65+
.setSampleRate(samplingRate)
66+
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
67+
.build())
68+
.setBufferSizeInBytes(bufferSize)
69+
.build();
70+
audioTrack.play();
71+
72+
// ぶつぶつ音を回避
73+
int offset = 100;
74+
byte[] voiceBuffer = LAppPal.loadFileAsBytes(filePath);
75+
audioTrack.write(voiceBuffer, offset, voiceBuffer.length - offset);
5476
}
5577

5678
private final String filePath;

0 commit comments

Comments
 (0)