Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

テストコードの追加及びテストの自動化環境の追加 #3

Merged
merged 7 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Test

# Controls when the action will run.
on:
pull_request:
branches:
- main
types: [opened, synchronize, reopened]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
test:
name: Run EditMode and PlayMode Test
runs-on: ubuntu-latest
steps:
- name: Check out my unity project.
uses: actions/checkout@v4.1.1
- name: Run EditMode and PlayMode Test
uses: game-ci/unity-test-runner@v4.1.1
env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
with:
projectPath: .
githubToken: ${{ secrets.GITHUB_TOKEN }}
unityVersion: 2023.2.4f1
customParameters: -testMode EditMode
# テストの実行結果をアーティファクトにアップロードして後から参照可能にする
- uses: actions/upload-artifact@v4.0.0
if: always()
with:
name: Test results
path: artifacts
8 changes: 8 additions & 0 deletions Assets/Tests.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Assets/Tests/AudioConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using NUnit.Framework;
using UniLlamaVoiceChat.Util;

namespace Tests
{
public class AudioConverterTests
{
[Test]
public void ConvertByteToAudioClipWithInvalidWavReturnsNull()
{
// 不適切なWAVファイルデータを用意
var invalidWavData = new byte[] { 0, 1, 2, 3, 4, 5 }; // 不正なデータ

// ConvertByteToAudioClipを実行
var result = AudioConverter.ConvertByteToAudioClip(invalidWavData);

// 結果がnullであることを確認
Assert.IsNull(result);
}
}
}
3 changes: 3 additions & 0 deletions Assets/Tests/AudioConverterTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Assets/Tests/Tests.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "Tests",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"UniLlamaVoiceChat"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Assets/Tests/Tests.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

140 changes: 86 additions & 54 deletions Assets/UniLlamaVoiceChat/Scripts/Util/AudioConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,81 +17,113 @@ private enum Resolution
_24bit = 24,
_32bit = 32
}

/// <summary>
/// WAVのbyte[]データをAudioClipに変換する
/// </summary>
/// <param name="wavFile"></param>
/// <param name="stream"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static AudioClip ConvertByteToAudioClip(byte[] wavFile, bool stream = false)
{
// WAVファイルかどうかをチェック
if (!IsWAVFile(wavFile))
{
return null;
}

var subchunk1Size = BitConverter.ToInt32(wavFile, 16);
int audioFormat = BitConverter.ToInt16(wavFile, 20);
int numChannels = BitConverter.ToInt16(wavFile, 22);
var sampleRate = BitConverter.ToInt32(wavFile, 24);
var bitsPerSample = (Resolution)BitConverter.ToInt16(wavFile, 34);
// PCM WAV method:
if (audioFormat == 1)
if (audioFormat != 1)
{
return null;
}

// Find where data starts:
var dataIndex = 20 + subchunk1Size;
for (var i = dataIndex; i < wavFile.Length; i++)
{
// Find where data starts:
var dataIndex = 20 + subchunk1Size;
for (var i = dataIndex; i < wavFile.Length; i++)
if (wavFile[i] == 'd' && wavFile[i + 1] == 'a' && wavFile[i + 2] == 't' && wavFile[i + 3] == 'a')
{
if (wavFile[i] == 'd' && wavFile[i + 1] == 'a' && wavFile[i + 2] == 't' && wavFile[i + 3] == 'a')
{
dataIndex = i + 4; // "data" string size = 4
break;
}
dataIndex = i + 4; // "data" string size = 4
break;
}
}

// Data parameters:
var subchunk2Size = BitConverter.ToInt32(wavFile, dataIndex); // Data size (Subchunk2Size).
dataIndex += 4; // Subchunk2Size = 4
var sampleSize = (int)bitsPerSample / 8; // Size of a sample.
var sampleCount = subchunk2Size / sampleSize; // How many samples into data.
// Data conversion:
var audioBuffer = new float[sampleCount]; // Size for all available channels.
for (var i = 0; i < sampleCount; i++)
// Data parameters:
var subchunk2Size = BitConverter.ToInt32(wavFile, dataIndex); // Data size (Subchunk2Size).
dataIndex += 4; // Subchunk2Size = 4
var sampleSize = (int)bitsPerSample / 8; // Size of a sample.
var sampleCount = subchunk2Size / sampleSize; // How many samples into data.
// Data conversion:
var audioBuffer = new float[sampleCount]; // Size for all available channels.
for (var i = 0; i < sampleCount; i++)
{
var sampleIndex = dataIndex + i * sampleSize;
var intSample = 0;
float sample = 0;
switch (bitsPerSample)
{
var sampleIndex = dataIndex + i * sampleSize;
var intSample = 0;
float sample = 0;
switch (bitsPerSample)
{
case Resolution._16bit:
intSample = BitConverter.ToInt16(wavFile, sampleIndex);
sample = intSample / 32767f;
break;
case Resolution._24bit:
intSample = BitConverter.ToInt32(
new byte[]
{
0, wavFile[sampleIndex], wavFile[sampleIndex + 1], wavFile[sampleIndex + 2]
}, 0) >> 8;
sample = intSample / 8388607f;
break;
case Resolution._32bit:
intSample = BitConverter.ToInt32(wavFile, sampleIndex);
sample = intSample / 2147483647f;
break;
default:
throw new ArgumentOutOfRangeException();
}

audioBuffer[i] = sample;
case Resolution._16bit:
intSample = BitConverter.ToInt16(wavFile, sampleIndex);
sample = intSample / 32767f;
break;
case Resolution._24bit:
intSample = BitConverter.ToInt32(
new byte[]
{
0, wavFile[sampleIndex], wavFile[sampleIndex + 1], wavFile[sampleIndex + 2]
}, 0) >> 8;
sample = intSample / 8388607f;
break;
case Resolution._32bit:
intSample = BitConverter.ToInt32(wavFile, sampleIndex);
sample = intSample / 2147483647f;
break;
default:
throw new ArgumentOutOfRangeException();
}

// Create the AudioClip:
var audioClip =
AudioClip.Create(string.Empty, sampleCount / numChannels, numChannels, sampleRate, stream);
audioClip.SetData(audioBuffer, 0);
return audioClip;
audioBuffer[i] = sample;
}

Debug.LogError("AudioFormat is not PCM");
return null;

// Create the AudioClip:
var audioClip =
AudioClip.Create(string.Empty, sampleCount / numChannels, numChannels, sampleRate, stream);
audioClip.SetData(audioBuffer, 0);
return audioClip;
}

/// <summary>
/// byte配列データがWAVファイルかどうかを判定する
/// </summary>
/// <returns></returns>
private static bool IsWAVFile(byte[] data)
{
// WAVファイルの最小長チェック
if (data == null || data.Length < 44)
{
return false;
}

// "RIFF"ヘッダのチェック
var riffHeader = System.Text.Encoding.ASCII.GetString(data, 0, 4);
if (riffHeader != "RIFF")
{
return false;
}

// "WAVE"フォーマットのチェック
var waveFormat = System.Text.Encoding.ASCII.GetString(data, 8, 4);
if (waveFormat != "WAVE")
{
return false;
}

return true;
}
}
}
2 changes: 1 addition & 1 deletion Assets/UniLlamaVoiceChat/Scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jp.ayutaz.uni-llama-voice-chat" ,
"displayName": "uni-llama-voice-chat",
"version": "0.1.2" ,
"version": "0.1.3" ,
"unity": "2023.2" ,
"description": "llama.cpp as an LLM server to perform inference and generate speech using Synthetic voice library" ,
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ PlayerSettings:
vulkanEnableLateAcquireNextImage: 0
vulkanEnableCommandBufferRecycling: 1
loadStoreDebugModeEnabled: 0
bundleVersion: 0.1.0
bundleVersion: 0.1.3
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
Expand Down