unty stream microphone+
int lastSample = 0;
void FixedUpdate()
{
// If there is a 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 to convert the audio clip float array to 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 to convert the byte array to float array for the audio clip
public float[] ToFloatArray(byte[] byteArray) {
int len = byteArray.Length / 4;
float[] floatArray = new float[len];
for (int i = 0; i < byteArray.Length; i+=4) {
floatArray[i/4] = System.BitConverter.ToSingle(byteArray, i);
}
return floatArray;
}
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line