public static VoiceChatPacket Compress(float[] sample) { VoiceChatPacket result = default(VoiceChatPacket); result.Compression = VoiceChatInnerSettings.Compression; switch (result.Compression) { case VoiceChatCompression.Alaw: result.Length = sample.Length; result.Data = ALawCompress(sample); break; case VoiceChatCompression.AlawZlib: { byte[] array = ALawCompress(sample); result.Data = ZlibCompress(array, sample.Length); result.Length = result.Data.Length; VoiceChatBytePool.Instance.Return(array); break; } case VoiceChatCompression.Speex: result.Data = SpeexCompress(sample, out result.Length); break; } return(result); }
void GetRelativeVolume(VoiceChatPacket packet, out float maxAmplitude, out int highVolumeFrequency, out int minFrequency, out int maxFrequency) { float[] sample = null; VoiceChatUtils.Decompress(new NSpeex.SpeexDecoder(NSpeex.BandMode.Narrow), packet, out sample); // clear fftBuffer for (int i = 0; i < fftBuffer.Length; ++i) { fftBuffer[i] = 0; } Array.Copy(sample, 0, fftBuffer, 0, sample.Length); Exocortex.DSP.Fourier.FFT(fftBuffer, fftBuffer.Length / 2, Exocortex.DSP.FourierDirection.Forward); highVolumeFrequency = -1; minFrequency = -1; maxFrequency = -1; maxAmplitude = -1; for (int i = 0; i < fftBuffer.Length; ++i) { if (fftBuffer[i] > maxAmplitude) { maxAmplitude = fftBuffer[i]; highVolumeFrequency = i; } if (minFrequency == -1 && fftBuffer[i] > 0) { minFrequency = i; } if (fftBuffer[i] > 0) { maxFrequency = i; } } }
public static VoiceChatPacket ReadPacket(this BitStream stream) { short compression = 0; PackShort pack = new PackShort(); VoiceChatPacket packet = new VoiceChatPacket(); stream.Serialize(ref packet.Length); stream.Serialize(ref compression); packet.Compression = (VoiceChatCompression)(byte)compression; packet.Data = VoiceChatBytePool.Instance.Get(); for (int i = 0; i < packet.Length; i += 2) { stream.Serialize(ref pack.Short); packet.Data[i] = pack.Byte0; if (i + 1 < packet.Length) { packet.Data[i + 1] = pack.Byte1; } } return packet; }
//播放录音 public void PlayRecording(int voiceLen) { if (source == null) { return; } iPhoneSpeaker.ForceToSpeaker(); int size = VoiceChatSettings.Instance.SampleSize; voiceLen = voiceLen / compressLen * size; VoiceChatPacket packet = new VoiceChatPacket(); packet.Compression = VoiceChatSettings.Instance.Compression; packet.Data = VoiceChatBytePool.Instance.Get(); packet.Length = compressLen; for (int i = 0; i < voiceLen; i += size) { float[] sample = null; Array.Copy(cacheData, i, packet.Data, 0, size); int length = VoiceChatUtils.Decompress(speexDec, packet, out sample); Array.Copy(sample, 0, data, i, length); VoiceChatFloatPool.Instance.Return(sample); } source.clip.SetData(data, 0); source.Play(); // isPlaying = true; VoiceChatBytePool.Instance.Return(packet.Data); }
void SendPacket(VoiceChatPacket packet, Sfs2X.Util.ByteArray data) { // SFVoicePacket sfPacket = new SFVoicePacket(packet); // CommunicationManager.SendObjectMsg(sfPacket.GetSerialized()); SFSObject voiceData = new SFSObject(); voiceData.PutUtfString("type", "voice"); voiceData.PutByte("c", (byte)packet.Compression); // Compression voiceData.PutInt("l", packet.Length); // Length voiceData.PutByteArray("d", new Sfs2X.Util.ByteArray(packet.Data)); // Data voiceData.PutInt("i", packet.NetworkId); // NetworkId bool sendToAllInZone = false; if (sendToAllInZone) { CommunicationManager.SendMsg(new AdminMessageRequest("v", (new MessageRecipientMode((int)MessageRecipientType.TO_ZONE, null)), voiceData)); } else { CommunicationManager.SendObjectMsg(voiceData); } // Optimization notes: // use class serialization, current method is probably slow (http://docs2x.smartfoxserver.com/AdvancedTopics/class-serialization) // can also use the pool setup for the demo, so you don't have to instantiate a new buffer for every message // don't need to send compression byte every time, only one networkID needed if a queue is grouped together. }
public static VoiceChatPacket ReadPacket(this BitStream stream) { short compression = 0; PackShort pack = new PackShort(); VoiceChatPacket packet = new VoiceChatPacket(); stream.Serialize(ref packet.Length); stream.Serialize(ref compression); packet.Compression = (VoiceChatCompression)(byte)compression; packet.Data = VoiceChatBytePool.Instance.Get(); for (int i = 0; i < packet.Length; i += 2) { stream.Serialize(ref pack.Short); packet.Data[i] = pack.Byte0; if (i + 1 < packet.Length) { packet.Data[i + 1] = pack.Byte1; } } return(packet); }
private void VoiceChat(VoiceChatPacket obj) { _Player.replay.voiceChatTime = Time.realtimeSinceStartup; if (!_Loader.banned) { _Player.photonView.RPC("SendAudio", PhotonTargets.All, obj.Data, obj.Length, PhotonNetwork.player.ID); } }
public static VoiceChatPacket Compress(float[] sample) { VoiceChatPacket packet = new VoiceChatPacket(); packet.Compression = VoiceChatSettings.Instance.Compression; switch (packet.Compression) { /* case VoiceChatCompression.Raw: { short[] buffer = VoiceChatShortPool.Instance.Get(); packet.Length = sample.Length * 2; sample.ToShortArray(shortBuffer); Buffer.BlockCopy(shortBuffer, 0, byteBuffer, 0, packet.Length); } break; case VoiceChatCompression.RawZlib: { packet.Length = sample.Length * 2; sample.ToShortArray(shortBuffer); Buffer.BlockCopy(shortBuffer, 0, byteBuffer, 0, packet.Length); packet.Data = ZlibCompress(byteBuffer, packet.Length); packet.Length = packet.Data.Length; } break; */ case VoiceChatCompression.Alaw: { packet.Length = sample.Length; packet.Data = ALawCompress(sample); } break; case VoiceChatCompression.AlawZlib: { byte[] alaw = ALawCompress(sample); packet.Data = ZlibCompress(alaw, sample.Length); packet.Length = packet.Data.Length; VoiceChatBytePool.Instance.Return(alaw); } break; case VoiceChatCompression.Speex: { packet.Data = SpeexCompress(sample, out packet.Length); } break; } return packet; }
void OnNewSample(VoiceChatPacket packet) { var packetMessage = new VoiceChatPacketMessage { proxyId = (short)localProxyId, packet = packet, }; NetworkManager.singleton.client.SendUnreliable(VoiceChatMsgType.Packet, packetMessage); }
public void OnNewSample(VoiceChatPacket newPacket) { // Set last time we got something lastRecvTime = Time.time; packetsToPlay.Add(newPacket.PacketId, newPacket); if (packetsToPlay.Count < 10) { return; } var pair = packetsToPlay.First(); var packet = pair.Value; packetsToPlay.Remove(pair.Key); // Decompress float[] sample = null; int length = VoiceChatUtils.Decompress(speexDec, packet, out sample); // Add more time to received received += VoiceChatSettings.Instance.SampleTime; // Push data to buffer Array.Copy(sample, 0, data, index, length); // Increase index index += length; // Handle wrap-around if (index >= audioSource.clip.samples) { index = 0; } // Set data audioSource.clip.SetData(data, 0); //if (!audioSource.isPlaying) // audioSource.Play(); // If we're not playing if (!audioSource.isPlaying) { // Set that we should be playing shouldPlay = true; // And if we have no delay set, set it. if (playDelay <= 0) { playDelay = (float)VoiceChatSettings.Instance.SampleTime * playbackDelay; } } VoiceChatFloatPool.Instance.Return(sample); }
public static VoiceChatPacket GetVoicePacketFromMsg(ISFSObject msgObj) { VoiceChatPacket packet = new VoiceChatPacket(); packet.Compression = (VoiceChatCompression)msgObj.GetByte("c"); packet.Length = msgObj.GetInt("l"); Sfs2X.Util.ByteArray t = msgObj.GetByteArray("d"); packet.Data = t.Bytes; packet.NetworkId = msgObj.GetInt("i"); return(packet); }
void TransmitBuffer(float[] buffer) { // Compress into packet VoiceChatPacket packet = VoiceChatUtils.Compress(buffer); // Set networkid of packet packet.NetworkId = NetworkId; // Raise event NewSample(packet); }
public void OnNewVoiceSample(VoiceChatPacket packet) { if (!GameManager.DoesLevelHaveSmartFoxRoom(GameManager.Inst.LevelLoaded) || !CommunicationManager.InASmartFoxRoom) { return; } Sfs2X.Util.ByteArray data = new Sfs2X.Util.ByteArray(); data.WriteBytes(packet.Data, 0, packet.Length); SendPacket(packet, data); }
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) { int count = packets.Count; if (stream.isWriting) { stream.Serialize(ref count); while (packets.Count > 0) { VoiceChatPacket packet = packets.Dequeue(); stream.WritePacket(packet); // If this packet is the same size as the sample size, we can return it if (packet.Data.Length == VoiceChatSettings.Instance.SampleSize) { VoiceChatBytePool.Instance.Return(packet.Data); } } } else { count = 0; if (Network.isServer) { stream.Serialize(ref count); for (int i = 0; i < count; ++i) { packets.Enqueue(stream.ReadPacket()); if (Network.connections.Length < 2) { packets.Dequeue(); } } } else { stream.Serialize(ref count); for (int i = 0; i < count; ++i) { var packet = stream.ReadPacket(); packet.PacketId = ++assignedPacketId; if (player != null) { player.OnNewSample(packet); } } } } }
void OnNewSample(VoiceChatPacket packet) { var packetMessage = new VoiceChatPacketMessage { proxyId = (short)localProxyId, packet = packet, }; Debug.Log("Got a new Voice Sample. Streaming!"); NetworkManager.singleton.client.Send(VoiceChatMsgType.Packet, packetMessage); }
void OnNewSample(VoiceChatPacket packet) { var packetMessage = new VoiceChatPacketMessage { proxyId = (short)localProxyId, packet = packet, }; //if (LogFilter.logDebug) //{ // Debug.Log("Got a new Voice Sample. Streaming!"); //} NetworkManager.singleton.client.SendUnreliable(VoiceChatMsgType.Packet, packetMessage); }
public void OnNewSample(VoiceChatPacket packet) { if (!initialized) { Start(); } // Store last packet // Set last time we got something lastRecvTime = Time.time; // Decompress float[] sample = null; int length = VoiceChatUtils.Decompress(speexDec, packet, out sample); // Add more time to received received += VoiceChatSettings.Instance.SampleTime; // Push data to buffer Array.Copy(sample, 0, data, index, length); // Increase index index += length; // Handle wrap-around if (index >= GetComponent <AudioSource>().clip.samples) { index = 0; } // Set data GetComponent <AudioSource>().clip.SetData(data, 0); // If we're not playing if (!GetComponent <AudioSource>().isPlaying) { // Set that we should be playing shouldPlay = true; // And if we have no delay set, set it. if (playDelay <= 0) { playDelay = (float)VoiceChatSettings.Instance.SampleTime * playbackDelay; } } VoiceChatFloatPool.Instance.Return(sample); }
private void ReadVoiceData() { string path = "C:/temp/"; string filename = "voice_packet"; string ext = ".data"; for (int i = 0; i < 37; ++i) { try { using (FileStream fsSource = new FileStream(path + filename + i + ext, FileMode.Open, FileAccess.Read)) // using makes sure file handle is cleaned up. { VoiceChatPacket packet = new VoiceChatPacket(); packet.Length = (int)fsSource.Length; packet.Data = new byte[packet.Length]; packet.Compression = VoiceChatCompression.Speex; packet.NetworkId = CommunicationManager.CurrentUserID; int numBytesToRead = (int)packet.Length; int numBytesRead = 0; while (numBytesToRead > 0) { // Read may return anything from 0 to numBytesToRead. int n = fsSource.Read(packet.Data, numBytesRead, numBytesToRead); // Break when the end of the file is reached. if (n == 0) { break; } numBytesRead += n; numBytesToRead -= n; } voiceData.Add(packet); } } catch (FileNotFoundException ioEx) { Debug.LogError("ReadVoiceData: FileNotFound, make sure the sample voice packets are in " + path + "msg: " + ioEx.Message); } catch (Exception e) { Debug.LogError("ReadVoiceData Exception: " + e.ToString()); } } }
public static int Decompress(NSpeex.SpeexDecoder speexDecoder, VoiceChatPacket packet, out float[] data) { switch (packet.Compression) { /* * case VoiceChatCompression.Raw: * { * short[9 buffer * * Buffer.BlockCopy(packet.Data, 0, shortBuffer, 0, packet.Length); * shortBuffer.ToFloatArray(data, packet.Length / 2); * return packet.Length / 2; * } * * case VoiceChatCompression.RawZlib: * { * byte[] unzipedData = ZlibDecompress(packet.Data, packet.Length); * * Buffer.BlockCopy(unzipedData, 0, shortBuffer, 0, unzipedData.Length); * shortBuffer.ToFloatArray(data, unzipedData.Length / 2); * return unzipedData.Length / 2; * } */ case VoiceChatCompression.Speex: { data = SpeexDecompress(speexDecoder, packet.Data, packet.Length); return(data.Length); } case VoiceChatCompression.Alaw: { data = ALawDecompress(packet.Data, packet.Length); return(packet.Length); } case VoiceChatCompression.AlawZlib: { byte[] alaw = ZlibDecompress(packet.Data, packet.Length); data = ALawDecompress(alaw, alaw.Length); return(alaw.Length); } } data = new float[0]; return(0); }
public void OnNewSample(VoiceChatPacket packet) { // Store last packet // Set last time we got something lastRecvTime = Time.realtimeSinceStartup; // Decompress float[] sample = null; int length = VoiceChatUtils.Decompress(speexDec, packet, out sample); //NormalizeSample(sample); // Add more time to received received += VoiceChatSettings.Instance.SampleTime; // Push data to buffer Array.Copy(sample, 0, data, index, length); // Increase index index += length; // Handle wrap-around if (index >= audio.clip.samples) { index = 0; } // Set data audio.clip.SetData(data, 0); // If we're not playing if (!audio.isPlaying) { // Set that we should be playing shouldPlay = true; // And if we have no delay set, set it. if (playDelay <= 0) { playDelay = (float)VoiceChatSettings.Instance.SampleTime * playbackDelay; } } VoiceChatFloatPool.Instance.Return(sample); }
public void OnNewSample(VoiceChatPacket packet) { //if(onSendVoice != null){ //Array.Copy(packet.Data,0,tempSendData,0,compressLen); Array.Copy(packet.Data, 0, tempSendAllData, sendLen, compressLen); sendLen += compressLen; //onSendVoice(tempSendData); //} // Array.Copy(packet.Data,0,cacheData,index,VoiceChatSettings.Instance.SampleSize); index += VoiceChatSettings.Instance.SampleSize; // auto stop if (index >= source.clip.samples) { if (onCommitVoice != null) { onCommitVoice(); } } }
public static void WritePacket(this BitStream stream, VoiceChatPacket packet) { PackShort pack = new PackShort(); short compression = (short)(byte)packet.Compression; stream.Serialize(ref packet.Length); stream.Serialize(ref compression); for (int i = 0; i < packet.Length; i += 2) { pack.Byte0 = packet.Data[i]; if (i + 1 < packet.Length) { pack.Byte1 = packet.Data[i + 1]; } stream.Serialize(ref pack.Short); } }
public void HandleMessage(ISFSObject msgObj) { if (!initialized) { return; } VoiceChatPacket packet = GetVoicePacketFromMsg(msgObj); if (!voiceChatPlayers.ContainsKey(packet.NetworkId)) { voiceChatPlayers.Add(packet.NetworkId, ((GameObject)Instantiate(Resources.Load("VoiceChat_Player"))).GetComponent <VoiceChatPlayer>()); voiceChatPlayers[packet.NetworkId].userId = packet.NetworkId; } voiceChatPlayers[packet.NetworkId].OnNewSample(packet); // plays audio if (ToggleToTalk && GameManager.Inst.LocalPlayer) { IsTalkingWhilePlayingAudio.AddSample(GameManager.Inst.LocalPlayer.IsTalking); } }
void SendPackets(Queue <VoiceChatPacket> packets) { if (packets.Count == 0) { return; } VoiceChatPacket v = new VoiceChatPacket(); Sfs2X.Util.ByteArray data = new Sfs2X.Util.ByteArray(); v.Length = 0; while (packets.Count > 0) { VoiceChatPacket packet = packets.Dequeue(); v.Compression = packet.Compression; data.WriteBytes(packet.Data, 0, packet.Length); v.Length += packet.Length; v.NetworkId = packet.NetworkId; } SendPacket(v, data); }
public static int Decompress(SpeexDecoder speexDecoder, VoiceChatPacket packet, out float[] data) { switch (packet.Compression) { case VoiceChatCompression.Speex: data = SpeexDecompress(speexDecoder, packet.Data, packet.Length); return(data.Length); case VoiceChatCompression.Alaw: data = ALawDecompress(packet.Data, packet.Length); return(packet.Length); case VoiceChatCompression.AlawZlib: { byte[] array = ZlibDecompress(packet.Data, packet.Length); data = ALawDecompress(array, array.Length); return(array.Length); } default: data = new float[0]; return(0); } }
private void VoiceChat(VoiceChatPacket obj) { _Player.voiceChatTime = Time.realtimeSinceStartup; _Player.CallRPCTo(_Player.SendAudio, PhotonTargets.All, obj.Data, obj.Length, PhotonNetwork.player.ID); }
public static int Decompress(NSpeex.SpeexDecoder speexDecoder, VoiceChatPacket packet, out float[] data) { switch (packet.Compression) { /* case VoiceChatCompression.Raw: { short[9 buffer Buffer.BlockCopy(packet.Data, 0, shortBuffer, 0, packet.Length); shortBuffer.ToFloatArray(data, packet.Length / 2); return packet.Length / 2; } case VoiceChatCompression.RawZlib: { byte[] unzipedData = ZlibDecompress(packet.Data, packet.Length); Buffer.BlockCopy(unzipedData, 0, shortBuffer, 0, unzipedData.Length); shortBuffer.ToFloatArray(data, unzipedData.Length / 2); return unzipedData.Length / 2; } */ case VoiceChatCompression.Speex: { data = SpeexDecompress(speexDecoder, packet.Data, packet.Length); return data.Length; } case VoiceChatCompression.Alaw: { data = ALawDecompress(packet.Data, packet.Length); return packet.Length; } case VoiceChatCompression.AlawZlib: { byte[] alaw = ZlibDecompress(packet.Data, packet.Length); data = ALawDecompress(alaw, alaw.Length); return alaw.Length; } } data = new float[0]; return 0; }
public static int Decompress(VoiceChatPacket packet, out float[] data) { return(Decompress(null, packet, out data)); }
void OnNewSample(VoiceChatPacket packet) { packets.Enqueue(packet); }
private void VoiceChat(VoiceChatPacket obj) { _Player.replay.voiceChatTime = Time.realtimeSinceStartup; if (!_Loader.banned) _Player.photonView.RPC("SendAudio", PhotonTargets.All, obj.Data, obj.Length, PhotonNetwork.player.ID); }
public static int Decompress(VoiceChatPacket packet, out float[] data) { return Decompress(null, packet, out data); }