Esempio n. 1
0
        public void OnNewSample(VoiceChatPacket newPacket)
        {
            // Set last time we got something
            lastRecvTime = Time.time;

            // New Line Here
            if (packetsToPlay.ContainsKey(newPacket.PacketId))
            {
                return;
            }

            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 >= 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);
        }
Esempio n. 2
0
        public bool StartRecording()
        {
            if (NetworkId < 0 && !VoiceChatSettings.Instance.LocalDebug)
            {
                Debug.LogError("NetworkId is not set");
                return(false);
            }

            if (recording)
            {
                Debug.LogError("Already recording");
                return(false);
            }

            targetFrequency  = VoiceChatSettings.Instance.Frequency;
            targetSampleSize = VoiceChatSettings.Instance.SampleSize;

            int minFreq;
            int maxFreq;

            Microphone.GetDeviceCaps(Device, out minFreq, out maxFreq);

            recordFrequency  = minFreq == 0 && maxFreq == 0 ? 44100 : maxFreq;
            recordSampleSize = recordFrequency / (targetFrequency / targetSampleSize);

            clip         = Microphone.Start(Device, true, 1, recordFrequency);
            sampleBuffer = new float[recordSampleSize];
            fftBuffer    = new float[VoiceChatUtils.ClosestPowerOfTwo(targetSampleSize)];
            recording    = true;

            return(recording);
        }
Esempio n. 3
0
        void TransmitBuffer(float[] buffer)
        {
            // Compress into packet
            VoiceChatPacket packet = VoiceChatUtils.Compress(buffer);

            // Set networkid of packet
            packet.NetworkId = NetworkId;

            // Raise event
            NewSample(packet);
        }
Esempio n. 4
0
        void TransmitBuffer(float[] buffer)
        {
            // Compress into packet
            VoiceChatPacket packet = VoiceChatUtils.Compress(buffer);

            // Set networkid of packet
            packet.NetworkId = NetworkId;

            Debug.Log("[Transmit] netid:" + packet.NetworkId + " -> " + packet.Length);
            // Raise event
            NewSample(packet);
        }
Esempio n. 5
0
        void TransmitBuffer(float[] buffer)
        {
            int compressedSize = 0;

            byte[] compressedData = VoiceChatUtils.Compress(buffer, out compressedSize);
            if (finalPacket != null)
            {
                //Добавляем новый кусок в конец массива в пакете
                int    endOfPacket = finalPacket.Data.Length;
                byte[] resultArray = new byte[endOfPacket + compressedData.Length];
                for (int i = 0; i < finalPacket.Data.Length; i++)
                {
                    resultArray[i] = finalPacket.Data[i];
                }
                for (int i = 0; i < compressedData.Length; i++)
                {
                    resultArray[i + endOfPacket] = compressedData[i];
                }
                finalPacket.Data = resultArray;

                //если массив достаточно маленький или сжатых аудиокусков мало не отправляем.
                if (finalPacket.Data.Length < 400 && (finalPacket.Data.Length / finalPacket.CompressedSampleLen) < 4)
                {
                    return;
                }
            }
            else
            {
                finalPacket = new VoiceChatPacket();
                finalPacket.CompressedSampleLen = compressedSize;
                finalPacket.Data = compressedData;

                packetId            += 20;
                finalPacket.PacketId = packetId;

                //Debug.Log("create Packet. base Length= " + finalPacket.CompressedSampleLen + " array Length= " + finalPacket.Data.Length );

                //алав жмет плохо. поэтому пакет отправляется сразу
                if (VoiceChatSettings.compression == VoiceChatCompression.Speex)
                {
                    return;
                }
            }
            // Debug.Log("send Packet. base Length= " + finalPacket.CompressedSampleLen +  " array Length= " + finalPacket.Data.Length);

            totalSendedBytes += finalPacket.Data.Length;
            // Debug.Log("totalSendedBytes=" + totalSendedBytes);
            VoiceChatClient.SendPacket(finalPacket);
            finalPacket = null;
        }
Esempio n. 6
0
        private void TryPlay()
        {
            if (packetsToPlay.Count < 10)
            {
                return;
            }

            if (VoiceChatRecorder.Instance.CheckMutedAndIndicateTalk(enemyServerId))
            {
                packetsToPlay.Clear();
                return;
            }

            var pair   = packetsToPlay.First();
            var packet = pair.Value;

            packetsToPlay.Remove(pair.Key);

            float[] sample = null;
            int     length = VoiceChatUtils.Decompress(packet, out sample);

            if (Settings.voiceVolume != 1f)
            {
                for (int i = 0; i < sample.Length; ++i)
                {
                    sample[i] = sample[i] * Settings.voiceVolume;
                }
            }

            received += VoiceChatSettings.SampleTime;

            Array.Copy(sample, 0, data, index, length);

            index += length;

            if (index >= GetComponent <AudioSource>().clip.samples)
            {
                index = 0;
            }
            GetComponent <AudioSource>().clip.SetData(data, 0);
            if (!GetComponent <AudioSource>().isPlaying)
            {
                shouldPlay = true;
            }
        }
Esempio n. 7
0
        public void OnNewSample(VoiceChatPacket packet)
        {
            // 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);
        }