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): const int FREQUENCY = 44100; AudioClip mic; int lastPos, pos; // Use this for initialization void Start () { mic = Microphone.Start(null, true, 10, FREQUENCY); AudioSource audio = GetComponent<AudioSource>(); audio.clip = AudioClip.Create("test", 10 * FREQUENCY, mic.channels, FREQUENCY, false); audio.loop = true; } // Update is called once per frame void Update () { if((pos = Microphone.GetPosition(null)) > 0){ if(lastPos > pos) lastPos = 0; if(pos - lastPos > 0){ // Allocate the space for the sample. float[] sample = new float[(pos - lastPos) * mic.channels]; // Get the data from microphone. mic.GetData(sample, lastPos); // Put the data in the audio source. AudioSource audio = GetComponent<AudioSource>(); audio.clip.SetData(sample, lastPos); if(!audio.isPlaying) audio.Play(); lastPos = pos; } } } void OnDestroy(){ Microphone.End(null); } 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.
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.
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.
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.
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.