Skip to content

Commit

Permalink
SRV-1434 webgl voice handler outofbounds (#322)
Browse files Browse the repository at this point in the history
<!-- Copy the TICKETID for this task from Jira and add it to the PR name
in brackets -->
<!-- PR name should look like: [TICKETID] My Pull Request -->

<!-- Add link for the ticket here editing the TICKETID-->

## [SRV-1434](https://ready-player-me.atlassian.net/browse/SRV-1434)

## Description

- This changes adds additional checks that would limit reaching memory
access out of bounds on WebGL error.

<!-- Fill the section below with Added, Updated and Removed information.
-->
<!-- If there is no item under one of the lists remove it's title. -->

<!-- Testability -->

## How to Test

-   Create new project, import example scene
-   add VoiceHandler component
-  set build target to webGL and run the built application

<!-- Update your progress with the task here -->

## Checklist

-   [ ] Tests written or updated for the changes.
-   [ ] Documentation is updated.
-   [ ] Changelog is updated.

<!--- Remember to copy the Changes Section into the commit message when
you close the PR -->





[SRV-1434]:
https://ready-player-me.atlassian.net/browse/SRV-1434?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
arturtreiberg authored Oct 7, 2024
2 parents 70f3e7a + 4f5a3a9 commit 8335560
Showing 1 changed file with 11 additions and 4 deletions.
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;
var currentPosition = AudioSource.timeSamples;
var 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

0 comments on commit 8335560

Please sign in to comment.