Search Unity

Script that syncronize animation with Decibels (Facial Animations)

Discussion in 'Scripting' started by Dave4654, Jun 26, 2019.

  1. Dave4654

    Dave4654

    Joined:
    Feb 20, 2017
    Posts:
    3
    Hi! i have a problem this script.
    1. using UnityEngine;

    2. public class EspressioniScript : MonoBehaviour
    3. {
    4. public Animator animComp;
    5. private enum EspressioniFaccia {TalkB = 1, StopTalk = 0 }
    6. public AudioSource audioSource;
    7. public float updateStep = 0.1f;
    8. public int sampleDataLength = 1024;
    9. public float TimeChangeAnim = 2;

    10. private float currentUpdateTime = 0f;

    11. public float clipLoudness;
    12. private float[] clipSampleData;

    13. private int RandomAnimWord;
    14. public int AnimWord;

    15. public bool MyFunctionCalled;

    16. [Header("Prossimo Audio Script")]

    17. public AudioSource AudioS;

    18. [Header("Write here the Size Of AudioC")]
    19. public int n;
    20. [SerializeField]
    21. public AudioClip[] AudioC;

    22. public bool MyFunctionCalled2;

    23. public int NumeroAudio = 0;

    24. void Start()
    25. {
    26. clipSampleData = new float[sampleDataLength];

    27. FirstAudio();
    28. }



    29. // Update is called once per frame
    30. void Update()
    31. {
    32. currentUpdateTime += Time.deltaTime;
    33. if (currentUpdateTime >= updateStep)
    34. {
    35. currentUpdateTime = 0f;
    36. float[] samples = new float[audioSource.clip.samples * audioSource.clip.channels];
    37. audioSource.clip.GetData(clipSampleData, audioSource.timeSamples);
    38. clipLoudness = 0f;
    39. foreach (var sample in clipSampleData)
    40. {
    41. clipLoudness += Mathf.Abs(sample);
    42. }
    43. clipLoudness /= sampleDataLength;
    44. }
    45. {
    46. if (clipLoudness > 0.025)
    47. {
    48. animComp.SetInteger("EspressioniFaccia", AnimWord);
    49. }
    50. if (clipLoudness < 0.025 && clipLoudness != 0)
    51. {
    52. animComp.SetInteger("EspressioniFaccia", 0);
    53. {
    54. if (MyFunctionCalled == false)
    55. {
    56. RandomAnim();
    57. MyFunctionCalled = true;
    58. }
    59. }

    60. }
    61. if (clipLoudness == 0)
    62. {
    63. animComp.SetInteger("EspressioniFaccia", 0);
    64. StopRandomAnim();
    65. }
    66. }
    67. // Update Prossimo Audio
    68. if (NumeroAudio >= n + 1)
    69. {
    70. ResetAudio();
    71. }

    72. if (!AudioS.isPlaying && NumeroAudio <= n && MyFunctionCalled2 == false)
    73. {
    74. MyFunctionCalled2 = true;
    75. NextAudio();
    76. }
    77. }

    78. void RandomAnim()
    79. {
    80. int RandomAnimWord = Random.Range(1, 4);
    81. AnimWord = RandomAnimWord;
    82. Invoke("RandomAnim", TimeChangeAnim);

    83. }
    84. void StopRandomAnim()
    85. {
    86. MyFunctionCalled = false;
    87. CancelInvoke("RandomAnim");
    88. }
    89. //Script Audio
    90. void NextAudio()
    91. {
    92. MyFunctionCalled2 = false;
    93. NumeroAudio += 1;
    94. AudioS.clip = AudioC[NumeroAudio];
    95. AudioS.Play();
    96. }
    97. void FirstAudio()
    98. {
    99. MyFunctionCalled2 = false;
    100. NumeroAudio = 0;
    101. AudioS.clip = AudioC[NumeroAudio];
    102. AudioS.Play();
    103. }
    104. void ResetAudio()
    105. {
    106. NumeroAudio = 0;
    107. AudioS.clip = AudioC[NumeroAudio];
    108. AudioS.Play();
    109. }


    110. }
    This script syncronize an animation controlled by an animator with the Decibels of an audioclip (the character talk when there are Decibels). i get this two errors:

    -1- C:\buildslave\unity\build\Modules/Audio/Public/sound/SoundManager.cpp(519) : Error executing result = instance->m_Sound->lock(offsetBytes, lengthBytes, &ptr1, &ptr2, &len1, &len2) (An invalid parameter was passed to this function. ) UnityEngine.AudioClip:GetData() EspressioniScript:Update() (at Assets/Livelli/Dettagli Personaggi/Espressioni Script/EspressioniScript.cs:53)

    -2- IndexOutOfRangeException: Index was outside the bounds of the array. EspressioniScript.NextAudio () (at Assets/Livelli/Dettagli Personaggi/Espressioni Script/EspressioniScript.cs:114) EspressioniScript.Update () (at Assets/Livelli/Dettagli Personaggi/Espressioni Script/EspressioniScript.cs:93)

    -- Someone Can help me? thanks!