public ulong PlaySound(byte[] data) { List <AudioClip> clipList = new List <AudioClip>(); int offset = 0; while (offset < data.Length) { int len = System.BitConverter.ToInt32(data, offset); byte[] frame = new byte[len + 6]; System.Array.Copy(data, offset, frame, 0, frame.Length); USpeakFrameContainer cont = default(USpeakFrameContainer); cont.LoadFrom(frame); AudioClip clip = USpeakAudioClipCompressor.DecompressAudioClip(cont.encodedData, (int)cont.Samples, 1, false, BandMode.Narrow, 1); //GetComponent<AudioSource>().clip = clip; //GetComponent<AudioSource>().Play(); clipList.Add(clip); offset += frame.Length; } ulong delay = 0; ulong totalLength = 0; foreach (AudioClip clip in clipList) { USpeakAudioManager.PlayClipAtPoint(clip, transform.position, delay, false); delay += (uint)((44100.0f / (float)8000) * ((uint)clip.samples)); totalLength += (uint)clip.samples; } return((ulong)(totalLength * (float)8000 / 44100.0f) / 1000); }
void Update() { talkTimer -= Time.deltaTime; playbuffTimer += Time.deltaTime; if (playbuffTimer >= PlayBufferSize) { playbuffTimer = 0.0f; temp_stitch.Clear(); foreach (AudioClip clip in playBuffer) { float[] samples = new float[clip.samples]; clip.GetData(samples, 0); DestroyImmediate(clip); temp_stitch.AddRange(samples); } if (temp_stitch.Count > 0) { // AudioClip stitch_clip = USpeakAudioManager.GetOrCreateAudioClip( temp_stitch.Count, 1, audioFrequency, false ); AudioClip stitch_clip = AudioClip.Create("clip", temp_stitch.Count, 1, audioFrequency, false, false); stitch_clip.SetData(temp_stitch.ToArray(), 0); USpeakAudioManager.PlayClipAtPoint(stitch_clip, transform.position, 0, Is3D, false); } playBuffer.Clear(); } if (SpeakerMode == SpeakerMode.Remote) { return; } if (audioHandler == null) { return; } if (Microphone.devices.Length == 0) { return; } else { if (Microphone.devices[Mathf.Min(InputDeviceID, Microphone.devices.Length - 1)] != currentDeviceName) { currentDeviceName = Microphone.devices[Mathf.Min(InputDeviceID, Microphone.devices.Length - 1)]; print("Using input device: " + currentDeviceName); recording = Microphone.Start(currentDeviceName, false, 21, audioFrequency); lastReadPos = 0; } } int readPos = Microphone.GetPosition(null); if (readPos >= audioFrequency * 20) { readPos = 0; lastReadPos = 0; DestroyImmediate(recording); Microphone.End(null); recording = Microphone.Start(currentDeviceName, false, 21, audioFrequency); } if (readPos <= overlap) { return; } //read in the latest chunk of audio try { int sz = readPos - lastReadPos; if (sz > 1) { float[] d = new float[(sz - 1)]; recording.GetData(d, lastReadPos); if (talkController == null || talkController.ShouldSend()) { talkTimer = 1.0f; OnAudioAvailable(d); } } lastReadPos = readPos; } catch (System.Exception) { } ProcessPendingEncodeBuffer(); bool allowSend = true; if (SendingMode == SendBehavior.RecordThenSend && talkController != null) { allowSend = !talkController.ShouldSend(); } sendTimer += Time.deltaTime; if (sendTimer >= sendt && allowSend) { sendTimer = 0.0f; //flush the send buffer tempSendBytes.Clear(); foreach (USpeakFrameContainer frame in sendBuffer) { tempSendBytes.AddRange(frame.ToByteArray()); } sendBuffer.Clear(); if (tempSendBytes.Count > 0) { audioHandler.USpeakOnSerializeAudio(tempSendBytes.ToArray()); } } }
void Update() { talkTimer -= Time.deltaTime; playbuffTimer += Time.deltaTime; if (playbuffTimer >= PlayBufferSize) { playbuffTimer = 0.0f; uint delay = 0; foreach (AudioClip clip in playBuffer) { USpeakAudioManager.PlayClipAtPoint(clip, transform.position, delay, settings.Is3D); delay += (uint)((44100.0f / (float)audioFrequency) * ((uint)clip.samples)); } playBuffer.Clear(); } if (SpeakerMode == SpeakerMode.Remote) { return; } if (audioHandler == null) { return; } if (Microphone.devices.Length == 0) { return; } else { if (Microphone.devices[Mathf.Min(InputDeviceID, Microphone.devices.Length - 1)] != currentDeviceName) { currentDeviceName = Microphone.devices[Mathf.Min(InputDeviceID, Microphone.devices.Length - 1)]; print("Using input device: " + currentDeviceName); recording = Microphone.Start(currentDeviceName, false, 1000, audioFrequency); lastReadPos = 0; } } int readPos = Microphone.GetPosition(null); if (readPos >= audioFrequency * 1000) { readPos = 0; lastReadPos = 0; Microphone.End(null); recording = Microphone.Start(currentDeviceName, false, 1000, audioFrequency); } if (readPos <= overlap) { return; } //read in the latest chunk of audio try { int sz = readPos - lastReadPos; if (sz > 1) { float[] d = new float[(sz - 1)]; recording.GetData(d, lastReadPos); if (talkController == null || talkController.ShouldSend()) { talkTimer = 1.0f; OnAudioAvailable(d); } } lastReadPos = readPos; } catch (System.Exception) { } bool allowSend = true; if (SendingMode == SendBehavior.RecordThenSend && talkController != null) { allowSend = !talkController.ShouldSend(); } sendTimer += Time.deltaTime; if (sendTimer >= sendt && allowSend) { sendTimer = 0.0f; //flush the send buffer tempSendBytes.Clear(); foreach (USpeakFrameContainer frame in sendBuffer) { tempSendBytes.AddRange(frame.ToByteArray()); } sendBuffer.Clear(); if (tempSendBytes.Count > 0) { audioHandler.USpeakOnSerializeAudio(tempSendBytes.ToArray()); } } }