Search Unity

Audio Streaming from Mic.

Discussion in 'Multiplayer' started by giulioaur, Apr 26, 2018.

  1. giulioaur

    giulioaur

    Joined:
    Apr 26, 2018
    Posts:
    5
    Hi guys,
    I have tried to wrote a script that basically take the input from the mic and stream it on a audio source. This script works perfectly on local, but when I try to make the object a network object adding network identity and making it spawned by NetworkManager, it stops to work correctly.

    Code (CSharp):
    1.  
    2.     const int FREQUENCY = 44100;
    3.     AudioClip mic;
    4.     int lastPos, pos;
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.         mic = Microphone.Start(null, true, 10, FREQUENCY);
    9.  
    10.         AudioSource audio = GetComponent<AudioSource>();
    11.         audio.clip = AudioClip.Create("test", 10 * FREQUENCY, mic.channels, FREQUENCY, false);
    12.         audio.loop = true;
    13.  
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         if((pos = Microphone.GetPosition(null)) > 0){
    19.             if(lastPos > pos)    lastPos = 0;
    20.  
    21.             if(pos - lastPos > 0){
    22.                 // Allocate the space for the sample.
    23.                 float[] sample = new float[(pos - lastPos) * mic.channels];
    24.  
    25.                 // Get the data from microphone.
    26.                 mic.GetData(sample, lastPos);
    27.  
    28.                 // Put the data in the audio source.
    29.                 AudioSource audio = GetComponent<AudioSource>();
    30.                 audio.clip.SetData(sample, lastPos);
    31.                
    32.                 if(!audio.isPlaying)    audio.Play();
    33.  
    34.                 lastPos = pos;  
    35.             }
    36.         }
    37.     }
    38.  
    39.     void OnDestroy(){
    40.         Microphone.End(null);
    41.     }
    I thought the problem could be related to the network traffic, but update it's local and as far as I know, no automatic replication is set, so it cannot be the problem, right? Can someone figure out what is the problem?

    NB: I know that to assign directly the AudioClip from the microphone is the right way to do it, but it's not the way I need it.
     
  2. giulioaur

    giulioaur

    Joined:
    Apr 26, 2018
    Posts:
    5
    The problem was the frequency rate, too high for networking. Anyway it's not clear to me why this is a problem only in networking.
     
  3. AviarLabs

    AviarLabs

    Joined:
    Jun 26, 2017
    Posts:
    8
    I'm curious what was the Frequency rate which did work for networking?
     
  4. thelghome

    thelghome

    Joined:
    Jul 23, 2016
    Posts:
    743
    I am currently working on microphone streaming in my plugin FMETP STREAM.
    I can stream microphone smoothly over networking(UDP/TCP/WebSockets), but still looking for some solution to minimise the delay to 0.1~0.2sec.

    In my case, my audio streaming in WebGL build & HTML is almost no delay.
    But I got around 0.5 second delay when using AudioClip.Create() on Non-Web platforms.
     
    Last edited: May 26, 2020
    AviarLabs, varan941 and unicoea like this.
  5. will19

    will19

    Joined:
    Feb 9, 2020
    Posts:
    1
    Hi Guilioaur, thanks for sharing your code, it works fine for me too when playing locally. However, I don't get why the sound is delayed by almost 1 second, rather than instantaneously, could you please explain? And I am also planning to send it over the network for a multiplayer audio chat, I am using a mono channel and I'm going to bring the sampling frequency down to 10 kHz to avoid super high data rates.
     
  6. thelghome

    thelghome

    Joined:
    Jul 23, 2016
    Posts:
    743
    The delay is from Unity's audio playback, it has minimal 0.5~1.0sec latency, which we can't avoid it.
    Meanwhile, reducing DSP Buffer Size in audio setting may help a bit.

    Some customers improved the latency by using their own PCM audio player, with native audio playback solution. Then the latency will be ~0.1 sec instead.

    Our temporary suggestion will be adding delay second via GameViewDecoder, which can keep your video and audio syncing.
     
    SebinLee likes this.