Пример #1
0
    //void OnGUI () {
    void Update()
    {
        // When the server is started then issue a "LoadLevel" RPC call for the local GS client to load his own level
        // then also buffer the LoadLevel request so any new client that connects will also receive the "LoadLevel"
        if (Network.peerType != NetworkPeerType.Disconnected &&
            mgs.gamemenustate == NetworkMasterServer.menustate.networklobby &&
            Network.isServer)
        {
            // Make sure no old RPC calls are buffered and then send load level command
            Network.RemoveRPCsInGroup(0);
            Network.RemoveRPCsInGroup(1);
            // Load level with incremented level prefix (for view IDs)
            // NOTE: The RPCMode.AllBuffered sends the request to all clients, including the client sending the
            //       request.  Also, the Buffered command tells the game server to hold onto the RPC command and also
            //       send it to any new clients that connect.  This is important for level loading or player instantiation
            //       so we make sure all objects are correctly created on all clients, but would not be used for example
            //       to indicate a death as the new client would never have seen the player in the first place and
            //       therefore doesn't need to see the death.

            /* This is the network call that every player who starts/joins a server.
             * Change "DefaultChatroom" to the name of the scene you want the player to load at first */
            networkView.RPC("LoadLevel", RPCMode.AllBuffered, "DefaultChatroom", lastLevelPrefix + 1);
        }

        else if (Network.peerType != NetworkPeerType.Disconnected && !hasCreatedProxy)
        {
            proxy           = VoiceChatUtils.CreateProxy();
            hasCreatedProxy = true;
        }
    }
    public bool StartRecording()
    {
        if (NetworkId == -1 && !VoiceChatSettings.Instance.LocalDebug)
        {
            Debug.LogError("NetworkId is -1");
            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);
    }
Пример #3
0
    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;
            }
        }
    }
Пример #4
0
    //播放录音
    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);
    }
Пример #5
0
        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);
        }
Пример #6
0
        private void TransmitBuffer(float[] buffer)
        {
            // Compress into packet
            var packet = VoiceChatUtils.Compress(buffer);

            packet.PacketId = ++packetId;

            // Raise event
            NewSample?.Invoke(packet);
        }
Пример #7
0
    void TransmitBuffer(float[] buffer)
    {
        // Compress into packet
        VoiceChatPacket packet = VoiceChatUtils.Compress(buffer);

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

        // Raise event
        NewSample(packet);
    }
Пример #8
0
    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);
    }
Пример #9
0
    public bool StartRecording()
    {
        if (NetworkId == -1 && !VoiceChatSettings.Instance.LocalDebug)
        {
            Debug.LogError("NetworkId is -1");
            return(false);
        }

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

        if (Microphone.devices.Length == 0)
        {
            if (!reportedInitErrors)
            {
                Debug.LogError("No Microphone found");
                AnnouncementManager.Inst.Announce("Warning", "No Microphone Found on this computer, your voice will not be heard by others");
                UpdateUserMicHWI("(none)");
                reportedInitErrors = true;
            }
            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);
    }
Пример #10
0
 void OnConnectedToServer()
 {
     proxy = VoiceChatUtils.CreateProxy();
 }