c# - How to stream live audio via microphone in Unity3D? -
here's part of main code far(the networking done separately):
void update() { if (network.connections.length > 0) { audiofloat = new float[audioinfo.clip.samples * audioinfo.clip.channels]; audioinfo.clip.getdata (audiofloat, 0); networkview.rpc("playmicrophone", rpcmode.others, tobytearray(audiofloat), audioinfo.clip.channels); } } [rpc] void playmicrophone(byte[] ba, int c) { float[] flar = tofloatarray (ba); audio.clip = audioclip.create ("test", flar.length, c, 44100, true, false); audio.clip.setdata (flar, 0); audio.loop = true; while (!(microphone.getposition("") > 0)) {} audio.play(); } void onconnectedtoserver() { audioinfo.clip = microphone.start(null, true, 100, 44100); } // when server , client connected void onplayerconnected(networkplayer player) { audioinfo.clip = microphone.start(null, true, 100, 44100); } public byte[] tobytearray(float[] floatarray) { int len = floatarray.length * 4; byte[] bytearray = new byte[len]; int pos = 0; foreach (float f in floatarray) { byte[] data = system.bitconverter.getbytes(f); system.array.copy(data, 0, bytearray, pos, 4); pos += 4; } return bytearray; } public float[] tofloatarray(byte[] bytearray) { int len = bytearray.length / 4; float[] floatarray = new float[len]; (int = 0; < bytearray.length; i+=4) { floatarray[i/4] = system.bitconverter.tosingle(bytearray, i); } return floatarray; }
i know won't playback perfect, expecting hear slightest sound second never happened. data appears being sent on rpcs fine audio not play.
in update() function continually call rpc start playing microphone. happens after connection has been made either client or server.
since can't send arrays besides byte arrays audio data float array convert byte array sent on other person connected. there no errors when code ran sound still never plays.
the idea have live streaming 2-way audio gathered respective player's microphone. feel though buffer 100 seconds; should still play sound point.
i end using circular buffer when time comes. sound hasn't played @ yet.
i tried using examples here , here , still unable produce results.
any appreciated, reading!
i decided go code posted here , try different. differences made using fixedupdate instead of update , initiating microphone inside fixedupdate when connection made. there lot of stutter when using update chose work fixedupdate @ 0.5 second interval , works.
int lastsample = 0; void fixedupdate() { // if there connection if (network.connections.length > 0) { if (notrecording) { notrecording = false; sendingclip = microphone.start(null, true, 100, frequency); sending = true; } else if(sending) { int pos = microphone.getposition(null); int diff = pos-lastsample; if (diff > 0) { float[] samples = new float[diff * sendingclip.channels]; sendingclip.getdata (samples, lastsample); byte[] ba = tobytearray (samples); networkview.rpc ("send", rpcmode.others, ba, sendingclip.channels); debug.log(microphone.getposition(null).tostring()); } lastsample = pos; } } } [rpc] public void send(byte[] ba, int chan) { float[] f = tofloatarray(ba); audio.clip = audioclip.create("", f.length, chan, frequency,true,false); audio.clip.setdata(f, 0); if (!audio.isplaying) audio.play(); } // used convert audio clip float array bytes public byte[] tobytearray(float[] floatarray) { int len = floatarray.length * 4; byte[] bytearray = new byte[len]; int pos = 0; foreach (float f in floatarray) { byte[] data = system.bitconverter.getbytes(f); system.array.copy(data, 0, bytearray, pos, 4); pos += 4; } return bytearray; } // used convert byte array float array audio clip public float[] tofloatarray(byte[] bytearray) { int len = bytearray.length / 4; float[] floatarray = new float[len]; (int = 0; < bytearray.length; i+=4) { floatarray[i/4] = system.bitconverter.tosingle(bytearray, i); } return floatarray; }
Comments
Post a Comment