Multiplayer and microphone audio streaming
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.
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.
Answer by giulioaur · Apr 27, 2018 at 09:04 AM
Nvm, I figured out the problem was the frequency rate, that is too high, but it's not really clear to me why it is a problem only in multiplayer.
Hi, a bit late but i think what u tried to understand was that the buffer size and rate in networking can't be that high, you'll have to decrease the amount of data you'll send.
You can: lowpass filter the sound, get the data (high frequencies cutted out) Decrease the sample rate (8kHZ (8000) for example is used in Telephony) Respect the Shannon Criteria (SampleFreq >2 $$anonymous$$axFrequency (cutout frequency here)),
Hope that'll be usefull.
Your answer
Follow this Question
Related Questions
Recording with two microphones in Unity C# 0 Answers
Online client code execution 0 Answers
Unity noob here, need help adding footstep logic 1 Answer
Soundarray for player is not working properly 1 Answer
Take 2: Audio silent in Play Mode. 0 Answers