Failing to measure loudness

Philipp
Philipp
edited October 2019 in Photon Voice
Hi! I want to animate people's mouths when they speak (organically from 0f - 1f, being fully close to fully open). However, when I try to measure the current average loudness of the audioClip, all I get is the same value 0.0007006526f all the time. I can hear people's voices in the room, so that part works fine.

This is the core of my loudness getter (edit: Sorry, the Code formatting is broken when I try that button -- I removed that Code tag now):

-----
    const int sampleDataLength = 1024;
    float[] clipSampleData = null;

    void Start()
    {
        // Photon Voice View & Speaker & AudioSource is one node up, so grab it
        liveSpeechAudio = GetComponentInParent<AudioSource>();

        if (!photonView.IsMine)
        {
            clipSampleData = new float[sampleDataLength];
        }
    }

    float GetClipLoudness() // only called if (!photonView.IsMine)
    {
        float loudness = 0f;
        
        if (liveSpeechAudio.clip != null)
        {
            liveSpeechAudio.clip.GetData(clipSampleData, liveSpeechAudio.timeSamples);
            foreach (float sample in clipSampleData)
            {
                loudness += Mathf.Abs(sample);
            }
            if (loudness > 0f) { loudness /= sampleDataLength; }
        }
        
        return loudness;
    }
-----

Any help? Thanks!

Comments

  • Philipp
    Philipp
    edited October 2019
    This problem has now solved itself. I had misued GetComponentInParent<AudioSource> where I should have used transform.parent.GetComponent<AudioSource>, as GetComponentInParent starts its upwards search on the own node, not the parent -- where it had then found another static AudioSource sound file that happened to be attached.
  • (Sorry, the comment formatter ate some of my parentheses.)