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

Update demo for v0.8.7 #397

Merged
merged 1 commit into from
Nov 29, 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
1,548 changes: 1,486 additions & 62 deletions Demo/AITuber/AITuber.unity

Large diffs are not rendered by default.

87 changes: 64 additions & 23 deletions Demo/AITuber/AITuberMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEngine;
using Cysharp.Threading.Tasks;
using VRM;
using Newtonsoft.Json.Linq;
using ChatdollKit.IO;
using ChatdollKit.Model;
using ChatdollKit.Dialog;
Expand Down Expand Up @@ -89,17 +90,27 @@ private async UniTask HandleExternalMessage(ExternalInboundMessage message)

else if (message.Operation == "appearance")
{
var positionX = (float)(double)message.Payloads["position_x"];
var rotationY = (float)(double)message.Payloads["rotation_y"];
var positionX = Convert.ToSingle(message.Payloads["position_x"]);
var rotationY = Convert.ToSingle(message.Payloads["rotation_y"]);
modelController.AvatarModel.transform.position = new Vector3(positionX, 0, 0);
modelController.AvatarModel.transform.rotation = Quaternion.Euler(0, rotationY, 0);

var cameraPositionY = (float)(double)message.Payloads["camera_position_y"];
var cameraRotationX = (float)(double)message.Payloads["camera_rotation_x"];
var cameraFieldOfView = (float)(double)message.Payloads["camera_field_of_view"];
var cameraPositionY = Convert.ToSingle(message.Payloads["camera_position_y"]);
var cameraRotationX = Convert.ToSingle(message.Payloads["camera_rotation_x"]);
var cameraFieldOfView = Convert.ToSingle(message.Payloads["camera_field_of_view"]);
mainCamera.transform.position = new Vector3(0, cameraPositionY, 2);
mainCamera.transform.rotation = Quaternion.Euler(cameraRotationX, 180, 0);
mainCamera.fieldOfView = cameraFieldOfView;

if (message.Payloads.ContainsKey("camera_background_color"))
{
var bgString = (string)message.Payloads["camera_background_color"];
if (ColorUtility.TryParseHtmlString(bgString.StartsWith("#") ? bgString : "#" + bgString, out Color color))
{
color.a = 1.0f;
mainCamera.backgroundColor = color;
}
}
}
}

Expand All @@ -125,26 +136,56 @@ private async UniTask HandleExternalMessage(ExternalInboundMessage message)
{
if (message.Operation == "activate")
{
var speechSynthesizerName = ((string)message.Payloads["name"]).ToLower();

if (speechSynthesizerName == "voicevox")
{
styleBertVits2SpeechSynthesizer.IsEnabled = false;
voicevoxSpeechSynthesizer.IsEnabled = true;
voicevoxSpeechSynthesizer.EndpointUrl = (string)message.Payloads["url"];
voicevoxSpeechSynthesizer.Speaker = int.Parse($"{message.Payloads["voicevox_speaker"]}");
modelController.SpeechSynthesizerFunc = voicevoxSpeechSynthesizer.GetAudioClipAsync;
}
else if (speechSynthesizerName == "style-bert-vits2")
{
voicevoxSpeechSynthesizer.IsEnabled = false;
styleBertVits2SpeechSynthesizer.IsEnabled = true;
styleBertVits2SpeechSynthesizer.EndpointUrl = (string)message.Payloads["url"];
styleBertVits2SpeechSynthesizer.ModelId = int.Parse($"{message.Payloads["sbv2_model_id"]}");
styleBertVits2SpeechSynthesizer.SpeakerId = int.Parse($"{message.Payloads["sbv2_speaker_id"]}");
modelController.SpeechSynthesizerFunc = styleBertVits2SpeechSynthesizer.GetAudioClipAsync;
}
}
var speechSynthesizerName = ((string)message.Payloads["name"]).ToLower();

if (speechSynthesizerName == "voicevox")
{
styleBertVits2SpeechSynthesizer.IsEnabled = false;
voicevoxSpeechSynthesizer.IsEnabled = true;
voicevoxSpeechSynthesizer.EndpointUrl = (string)message.Payloads["url"];
voicevoxSpeechSynthesizer.Speaker = int.Parse($"{message.Payloads["voicevox_speaker"]}");
modelController.SpeechSynthesizerFunc = voicevoxSpeechSynthesizer.GetAudioClipAsync;
}
else if (speechSynthesizerName == "style-bert-vits2")
else if (message.Operation == "styles")
{
voicevoxSpeechSynthesizer.IsEnabled = false;
styleBertVits2SpeechSynthesizer.IsEnabled = true;
styleBertVits2SpeechSynthesizer.EndpointUrl = (string)message.Payloads["url"];
styleBertVits2SpeechSynthesizer.ModelId = int.Parse($"{message.Payloads["sbv2_model_id"]}");
styleBertVits2SpeechSynthesizer.SpeakerId = int.Parse($"{message.Payloads["sbv2_speaker_id"]}");
modelController.SpeechSynthesizerFunc = styleBertVits2SpeechSynthesizer.GetAudioClipAsync;
if (styleBertVits2SpeechSynthesizer.IsEnabled)
{
var styles = ((JObject)message.Payloads["styles"]).ToObject<Dictionary<string, string>>();
styleBertVits2SpeechSynthesizer.VoiceStyles.Clear();
foreach (var style in styles)
{
styleBertVits2SpeechSynthesizer.VoiceStyles.Add(
new StyleBertVits2SpeechSynthesizer.VoiceStyle() {
VoiceStyleValue = style.Key,
StyleBertVITSStyle = style.Value
}
);
}
}
else if (voicevoxSpeechSynthesizer.IsEnabled)
{
var styles = ((JObject)message.Payloads["styles"]).ToObject<Dictionary<string, int>>();
voicevoxSpeechSynthesizer.VoiceStyles.Clear();
foreach (var style in styles)
{
voicevoxSpeechSynthesizer.VoiceStyles.Add(
new VoicevoxSpeechSynthesizer.VoiceStyle() {
VoiceStyleValue = style.Key,
VoiceVoxSpeaker = style.Value
}
);
}
}
}
}

Expand All @@ -156,7 +197,7 @@ private async UniTask HandleExternalMessage(ExternalInboundMessage message)
var name = ((string)message.Payloads["name"]).ToLower();
var apiKey = message.Payloads.ContainsKey("api_key") ? (string)message.Payloads["api_key"] : null;
var model = message.Payloads.ContainsKey("model") ? (string)message.Payloads["model"] : null;
var temperature = message.Payloads.ContainsKey("temperature") ? (float)(double)message.Payloads["temperature"] : -1;
var temperature = message.Payloads.ContainsKey("temperature") ? Convert.ToSingle(message.Payloads["temperature"]) : -1;

if (name == "chatgpt")
{
Expand Down
19 changes: 19 additions & 0 deletions Demo/AITuber/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ private void HandleLog(string logString, string stackTrace, LogType type)
writer.WriteLine(message);
}

public void OpenLogFile()
{
try
{
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = logFilePath,
UseShellExecute = true
});
#elif UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
System.Diagnostics.Process.Start("open", $"\"{logFilePath}\"");
#endif
}
catch (Exception ex)
{
Debug.LogError($"Error in opening log file: {ex.Message}");
}
}
private void OnDestroy()
{
Application.logMessageReceived -= HandleLog;
Expand Down
17 changes: 15 additions & 2 deletions Demo/AITuber/MainAITuber.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using ChatdollKit.Model;
using ChatdollKit.Dialog;
using Cysharp.Threading.Tasks;

namespace ChatdollKit.Demo
{
Expand All @@ -22,6 +23,9 @@ public class MainAITuber : MonoBehaviour
[TextArea(1, 6)]
public string AutoPilotRequestText;

[SerializeField]
private GameObject licensePanel;

private void Start()
{
// Get ChatdollKit components
Expand All @@ -45,8 +49,12 @@ private void Start()

// Add handler for auto pilot
aiTuberMessageHandler.AddHandler("dialog", "auto_pilot", async (message) => {
autoPilot = (bool)message.Payloads["is_on"];
Debug.LogWarning($"auto_pilog: {autoPilot}");
if (message.Payloads == null) return;

if (message.Payloads.ContainsKey("is_on"))
{
autoPilot = (bool)message.Payloads["is_on"];
}
if (message.Payloads.ContainsKey("auto_pilot_request"))
{
AutoPilotRequestText = (string)message.Payloads["auto_pilot_request"];
Expand All @@ -66,5 +74,10 @@ private void Update()
}
}
}

public void OnLicenseButton()
{
licensePanel.SetActive(!licensePanel.activeSelf);
}
}
}
Loading