BananaHemic / Mumble-Unity

Performant Mumble Client For Unity3D
MIT License
77 stars 29 forks source link

Getting Amplitude of MumbleAudioPlayer #33

Closed bouwie closed 4 years ago

bouwie commented 4 years ago

Hi,

Is there any way to get the amplitude of the Mumble audio player pefab. When you are connected to the server you can see the amplitude in the editor moving but I cannot seem to find the source of this.

BananaHemic commented 4 years ago

Take a look in MumbleAudioPlayer, the amplitude is pretty much just the abs sum of the floating point audio data

bouwie commented 4 years ago

Thank you for your reply,

I'm not really good with audio so I have trouble calculating this value on my own. can you point me to the line of code where the value is applied to the editors value. that way I can just use that value cause that is just what I need, or is this piece of code hidden for users?

Capture

BananaHemic commented 4 years ago

That number is generated by Unity, it's not set through this asset

bouwie commented 4 years ago

You are right i just found out the slider is generated when you have the OnAudioFilterRead function in your script. When i have a solution i will post it

bouwie commented 4 years ago

I managed to get it kinda working if you smooth the audiofilterread data

code:

public float m_amplitude; private float m_amplitudeSmooth = 2.7f; private float m_rawAmplitude;

   void OnAudioFilterRead(float[] data, int channels)
    {
        if (_mumbleClient == null || !_mumbleClient.ConnectionSetupFinished)
            return;
        //Debug.Log("Filter read for: " + GetUsername());

        int numRead = _mumbleClient.LoadArrayWithVoiceData(Session, data, 0, data.Length);
        float percentUnderrun = 1f - numRead / data.Length;

        if (OnAudioSample != null)
            OnAudioSample(data, percentUnderrun);

        //Debug.Log("playing audio with avg: " + data.Average() + " and max " + data.Max());

        //       if (Gain == 1)
        //      return;

        for(int i = 0; i < data.Length; i++) {

            if(Gain != 1) {
                data[i] = Mathf.Clamp(data[i] * Gain, -1f, 1f);
            }

            m_rawAmplitude = Math.Abs(data[i] * Gain);
        }

        //Debug.Log("playing audio with avg: " + data.Average() + " and max " + data.Max());

    }

   void Update()
    {
        if (_mumbleClient == null)
            return;
        if (!_isPlaying && _mumbleClient.HasPlayableAudio(Session))
        {
            _audioSource.Play();
            _isPlaying = true;
            Debug.Log("Playing audio for: " + GetUsername());
        }
        else if (_isPlaying && !_mumbleClient.HasPlayableAudio(Session))
        {
            _audioSource.Stop();
            _isPlaying = false;
            Debug.Log("Stopping audio for: " + GetUsername());
        }

        m_amplitude = Mathf.Lerp(m_amplitude, m_rawAmplitude, Time.deltaTime * m_amplitudeSmooth);
    }

Then you have to tweak the m_amplitude value a bit to what you need