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

SRV-1434 webgl voice handler outofbounds #322

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions Runtime/Core/Scripts/Animation/VoiceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
arturtreiberg marked this conversation as resolved.
Show resolved Hide resolved
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)
{
Expand All @@ -178,7 +185,7 @@ private float GetAmplitude()
return Mathf.Clamp01(amplitude / audioSample.Length * AMPLITUDE_MULTIPLIER);
}

return 0;
return 0f;
}

#region Blend Shape Movement
Expand Down
Loading