Unity-Technologies / com.unity.webrtc

WebRTC package for Unity
Other
738 stars 186 forks source link

[BUG]: How get spectrum data from receiving audio stream track? #1033

Open VadimFokin20 opened 1 month ago

VadimFokin20 commented 1 month ago

Package version

3.0.0-pre.7

Environment

* OS: Windows 11
* Unity version: 2023.2

Steps To Reproduce

void ReceivingAudio() {
  var receivedAudioSource = GetComponent<AudioSource>();
  var receiveStream = new MediaStream();
  receiveStream.OnAddTrack = e => {
    if(e.Track is AudioStreamTrack track)
    {
        receivedAudioSource.SetTrack(track);
        receivedAudioSource.loop = true;
        receivedAudioSource.Play();        
    }
  }
}

void FixedUpdate() {
  if(receivedAudioSource != null) {
    float[] data = new float[256]
    receivedAudioSource.GetSpectrumData(data, 0, FFTWindow.Rectangular)
    // do something
  }
}

After sending peer has connected to receiving peer data array is empty, cause receivedAudioSource.clip is null, but receiver has sound from sender

Current Behavior

I have implemented audio streaming channel on LAN with own signalling server. Everything works good. After receiving the audio stream track on remote PC I need get a output data or spectrum data from the Audio Source. But the receiving Audio Source doesn't contains a audio clip and i get the empty float array from the necessary methods (AudioSource.GetSpectrumData() or AudioSource.GetOutPutData()) I tried the Audio Sample. The Same thing there.

Expected Behavior

Maybe there are other ways to get a data from audio clip?

Anything else?

No response