From ca03470ce4c25c6b17b6d5235477aaadaa69e249 Mon Sep 17 00:00:00 2001 From: arturtreiberg Date: Mon, 7 Oct 2024 10:20:32 +0300 Subject: [PATCH 1/3] make voice handler more durable for webgl --- Runtime/Core/Scripts/Animation/VoiceHandler.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Runtime/Core/Scripts/Animation/VoiceHandler.cs b/Runtime/Core/Scripts/Animation/VoiceHandler.cs index ba1e59cf..1233b749 100644 --- a/Runtime/Core/Scripts/Animation/VoiceHandler.cs +++ b/Runtime/Core/Scripts/Animation/VoiceHandler.cs @@ -46,7 +46,7 @@ public class VoiceHandler : MonoBehaviour private bool CanGetAmplitude => AudioSource != null && AudioSource.clip != null && AudioSource.isPlaying; private CancellationTokenSource ctxSource; - + private void Start() { CreateBlendshapeMeshMap(); @@ -165,10 +165,17 @@ public void PlayAudioClip(AudioClip audioClip) private float GetAmplitude() { - if (CanGetAmplitude) + if (CanGetAmplitude && AudioSource.clip.loadState == AudioDataLoadState.Loaded) { - var amplitude = 0f; + int currentPosition = AudioSource.timeSamples; + int remaining = AudioSource.clip.samples - currentPosition; + if (remaining > 0 && remaining < AUDIO_SAMPLE_LENGTH) + { + return 0f; + } + AudioSource.clip.GetData(audioSample, AudioSource.timeSamples); + var amplitude = 0f; foreach (var sample in audioSample) { @@ -178,7 +185,7 @@ private float GetAmplitude() return Mathf.Clamp01(amplitude / audioSample.Length * AMPLITUDE_MULTIPLIER); } - return 0; + return 0f; } #region Blend Shape Movement From 9e1b7239a4419d2ba32c1e07b4763505acb8d7ee Mon Sep 17 00:00:00 2001 From: Artur Treiberg <26140626+arturtreiberg@users.noreply.github.com> Date: Mon, 7 Oct 2024 11:58:37 +0300 Subject: [PATCH 2/3] Update Runtime/Core/Scripts/Animation/VoiceHandler.cs Co-authored-by: Harrison Hough --- Runtime/Core/Scripts/Animation/VoiceHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/Core/Scripts/Animation/VoiceHandler.cs b/Runtime/Core/Scripts/Animation/VoiceHandler.cs index 1233b749..4b19a8c3 100644 --- a/Runtime/Core/Scripts/Animation/VoiceHandler.cs +++ b/Runtime/Core/Scripts/Animation/VoiceHandler.cs @@ -167,7 +167,7 @@ private float GetAmplitude() { if (CanGetAmplitude && AudioSource.clip.loadState == AudioDataLoadState.Loaded) { - int currentPosition = AudioSource.timeSamples; + var currentPosition = AudioSource.timeSamples; int remaining = AudioSource.clip.samples - currentPosition; if (remaining > 0 && remaining < AUDIO_SAMPLE_LENGTH) { From 4f5a3a96377668d1ec69b1dcbd3f67a1c04b2473 Mon Sep 17 00:00:00 2001 From: arturtreiberg Date: Mon, 7 Oct 2024 11:59:36 +0300 Subject: [PATCH 3/3] int to var --- Runtime/Core/Scripts/Animation/VoiceHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/Core/Scripts/Animation/VoiceHandler.cs b/Runtime/Core/Scripts/Animation/VoiceHandler.cs index 4b19a8c3..61e59a68 100644 --- a/Runtime/Core/Scripts/Animation/VoiceHandler.cs +++ b/Runtime/Core/Scripts/Animation/VoiceHandler.cs @@ -168,7 +168,7 @@ private float GetAmplitude() if (CanGetAmplitude && AudioSource.clip.loadState == AudioDataLoadState.Loaded) { var currentPosition = AudioSource.timeSamples; - int remaining = AudioSource.clip.samples - currentPosition; + var remaining = AudioSource.clip.samples - currentPosition; if (remaining > 0 && remaining < AUDIO_SAMPLE_LENGTH) { return 0f;