Skip to content

Commit

Permalink
Merge pull request #8 from Live2D/develop
Browse files Browse the repository at this point in the history
Update to Cubism 4 SDK for Java R1
  • Loading branch information
itoh-at-live2d-com authored May 25, 2023
2 parents e75235c + 0c50687 commit 156ba1f
Show file tree
Hide file tree
Showing 37 changed files with 15,176 additions and 16,209 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ All notable changes to this project will be documented in this file.

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

## [4-r.1] - 2023-05-25

### Added

* Add some functions for checking consistency of MOC3 files.
* Add the function of checking consistency in `setupModel()` in `LAppModel` and `LAppMinimumModel`.
* Add the function of checking consistency before loading a model. (`hasMocConsistencyFromFile()` in `LAppModel` and `LAppMinimumModel`)
* This feature is enabled by default. Please see the following manual for more information.
* https://docs.live2d.com/cubism-sdk-manual/moc3-consistency/

### Changed

* Change so that when `USE_MODEL_RENDER_TARGET` is defined, one model will apply the opacity obtained from the motion.

### Removed

* Remove unnecessary files in the assets folder.

### Fixed

* Fix a problem in which `haru` motion and voice were incorrect combination.
* Fix a problem in `LAppWavFileHandler`class that some audio could not be played correctly because the sampling rate was the fixed value.

## [4-r.1-beta.4] - 2023-03-16

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

* New released!

[4-r.1]: https://github.com/Live2D/CubismJavaSamples/compare/4-r.1-beta.4...4-r.1
[4-r.1-beta.4]: https://github.com/Live2D/CubismJavaSamples/compare/4-r.1-beta.3...4-r.1-beta.4
[4-r.1-beta.3]: https://github.com/Live2D/CubismJavaSamples/compare/4-r.1-beta.2...4-r.1-beta.3
[4-r.1-beta.2]: https://github.com/Live2D/CubismJavaSamples/compare/4-r.1-beta.1...4-r.1-beta.2
Expand Down
4 changes: 2 additions & 2 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ Android Studioでプロジェクトを開きビルドすることを推奨しま

| 開発ツール | バージョン |
|----------------|------------------|
| Android Studio | Electric Eel 2022.1.1 |
| IntelliJ IDEA | 2022.3.1 |
| Android Studio | Flamingo 2022.2.1 Patch 1 |
| IntelliJ IDEA | 2023.1.1 |
| CMake | 3.1.0 |
| Gradle | 6.9 |

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ Please refer to [CHANGELOG.md](CHANGELOG.md) for the changelog of this repositor

| Development Tools | Version |
|-------------------|--|
| Android Studio | Electric Eel 2022.1.1 |
| IntelliJ IDEA | 2022.3.1 |
| Android Studio | Flamingo 2022.2.1 Patch 1 |
| IntelliJ IDEA | 2023.1.1 |
| CMake | 3.1.0 |
| Gradle | 6.9 |

Expand Down
42 changes: 39 additions & 3 deletions Sample/src/full/java/com/live2d/demo/full/LAppModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.live2d.sdk.cubism.framework.id.CubismId;
import com.live2d.sdk.cubism.framework.id.CubismIdManager;
import com.live2d.sdk.cubism.framework.math.CubismMatrix44;
import com.live2d.sdk.cubism.framework.model.CubismMoc;
import com.live2d.sdk.cubism.framework.model.CubismUserModel;
import com.live2d.sdk.cubism.framework.motion.ACubismMotion;
import com.live2d.sdk.cubism.framework.motion.CubismExpressionMotion;
Expand All @@ -25,11 +26,20 @@
import com.live2d.sdk.cubism.framework.rendering.CubismRenderer;
import com.live2d.sdk.cubism.framework.rendering.android.CubismOffscreenSurfaceAndroid;
import com.live2d.sdk.cubism.framework.rendering.android.CubismRendererAndroid;
import com.live2d.sdk.cubism.framework.utils.CubismDebug;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class LAppModel extends CubismUserModel {
public LAppModel() {
if (LAppDefine.MOC_CONSISTENCY_VALIDATION_ENABLE) {
mocConsistency = true;
}

if (LAppDefine.DEBUG_LOG_ENABLE) {
debugMode = true;
}
Expand Down Expand Up @@ -107,6 +117,9 @@ public void update() {
// モデルの状態を保存
model.saveParameters();

// 不透明度
opacity = model.getModelOpacity();

// eye blink
// メインモーションの更新がないときだけまばたきする
if (!isMotionUpdated) {
Expand Down Expand Up @@ -363,6 +376,30 @@ public CubismOffscreenSurfaceAndroid getRenderingBuffer() {
return renderingBuffer;
}

/**
* .moc3ファイルの整合性をチェックする。
*
* @param mocFileName MOC3ファイル名
* @return MOC3に整合性があるかどうか。整合性があればtrue。
*/
public boolean hasMocConsistencyFromFile(String mocFileName) {
assert mocFileName != null && !mocFileName.isEmpty();

String path = mocFileName;
path = modelHomeDirectory + path;

byte[] buffer = createBuffer(path);
boolean consistency = CubismMoc.hasMocConsistency(buffer);

if (!consistency) {
CubismDebug.cubismLogInfo("Inconsistent MOC3.");
} else {
CubismDebug.cubismLogInfo("Consistent MOC3.");
}

return consistency;
}

private static byte[] createBuffer(final String path) {
if (LAppDefine.DEBUG_LOG_ENABLE) {
LAppPal.printLog("create buffer: " + path);
Expand All @@ -388,7 +425,7 @@ private void setupModel(ICubismModelSetting setting) {
}

byte[] buffer = createBuffer(path);
loadModel(buffer);
loadModel(buffer, mocConsistency);
}
}

Expand Down Expand Up @@ -576,7 +613,6 @@ private void setupTextures() {
}
}


private ICubismModelSetting modelSetting;
/**
* モデルのホームディレクトリ
Expand Down
6 changes: 3 additions & 3 deletions Sample/src/full/java/com/live2d/demo/full/LAppView.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ public void render() {
};

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

renderingSprite.setColor(1.0f, 1.0f, 1.0f, alpha);

LAppModel model = live2dManager.getModel(i);
if (model != null) {
renderingSprite.renderImmediate(model.getRenderingBuffer().getColorBuffer()[0], uvVertex);
}
Expand Down
64 changes: 43 additions & 21 deletions Sample/src/full/java/com/live2d/demo/full/LAppWavFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@

package com.live2d.demo.full;

import android.content.res.AssetFileDescriptor;
import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioTrack;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.os.Build;

import java.io.IOException;

public class LAppWavFileHandler extends Thread {
public LAppWavFileHandler(String filePath) {
Expand All @@ -24,33 +30,49 @@ public void run() {
}

public void loadWavFile() {
byte[] voiceBuffer = LAppPal.loadFileAsBytes(filePath);
// 対応していないAPI(API24未満)の場合は音声再生を行わない。
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
return;
}

MediaExtractor mediaExtractor = new MediaExtractor();
try {
AssetFileDescriptor afd = LAppDelegate.getInstance().getActivity().getAssets().openFd(filePath);
mediaExtractor.setDataSource(afd);
} catch (IOException e) {
// 例外が発生したらエラーだけだして再生せずreturnする。
e.printStackTrace();
return;
}

MediaFormat mf = mediaExtractor.getTrackFormat(0);
int samplingRate = mf.getInteger(MediaFormat.KEY_SAMPLE_RATE);

int bufferSize = AudioTrack.getMinBufferSize(
88187,
samplingRate,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT
);

AudioTrack audioTrack = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
audioTrack = new AudioTrack.Builder()
.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build())
.setAudioFormat(new AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(88187)
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
.build())
.setBufferSizeInBytes(bufferSize)
.build();
audioTrack.play();

int offset = 100;
audioTrack.write(voiceBuffer, offset, voiceBuffer.length - offset);
}
AudioTrack audioTrack;
audioTrack = new AudioTrack.Builder()
.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build())
.setAudioFormat(new AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(samplingRate)
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
.build())
.setBufferSizeInBytes(bufferSize)
.build();
audioTrack.play();

// ぶつぶつ音を回避
int offset = 100;
byte[] voiceBuffer = LAppPal.loadFileAsBytes(filePath);
audioTrack.write(voiceBuffer, offset, voiceBuffer.length - offset);
}

private final String filePath;
Expand Down
Loading

0 comments on commit 156ba1f

Please sign in to comment.