コード例 #1
0
        void Update()
        {
            if (recording == null)
            {
                return;
            }

            var now = Microphone.GetPosition(_usedMicrophone);

            var length = now - lastPos;

            if (now < lastPos)
            {
                lastPos = 0;
                length  = now;
            }

            while (length >= recordingBuffer.Length)
            {
                if (_isListening && recording.GetData(recordingBuffer, lastPos))
                {
                    //Send..
                    index++;
                    if (OnAudioGenerated != null)
                    {
                        //Downsample if needed.
                        if (recordingBuffer != resampleBuffer)
                        {
                            AudioUtils.Resample(recordingBuffer, resampleBuffer, inputFreq, AudioUtils.GetFrequency(encoder.mode));
                        }

                        var data = encoder.Encode(resampleBuffer);

                        VoipFragment frag = new VoipFragment(0, index, data, encoder.mode);

                        OnAudioGenerated?.Invoke(frag);
                    }
                }
                length  -= recordingBuffer.Length;
                lastPos += recordingBuffer.Length;
            }
        }
コード例 #2
0
        public SpeexCodex(BandMode mode)
        {
            this.mode = mode;

            encoder = new NSpeex.SpeexEncoder(SpeedxMode);
            decoder = new NSpeex.SpeexDecoder(SpeedxMode, false);

            encoder.VBR     = true;
            encoder.Quality = 4;

            dataSize = encoder.FrameSize * (mode == BandMode.Narrow ? 8 : mode == BandMode.Wide ? 8 : 2);

            encodingBuffer = new short[dataSize];
            encodedBuffer  = new byte[dataSize];
            decodeBuffer   = new short[dataSize];
            decodedBuffer  = new float[dataSize];

            int frequency = AudioUtils.GetFrequency(mode);
            var time      = frequency / (float)dataSize;

            UnityEngine.Debug.Log("SpeedCodex created mode:" + mode + " dataSize:" + dataSize + " time:" + time);
        }
コード例 #3
0
        void Update()
        {
            if (recording == null)
            {
                return;
            }

            var now    = Microphone.GetPosition(null);
            var length = now - lastPos;

            if (now < lastPos)
            {
                lastPos = 0;
                length  = now;
            }

            while (length >= recordingBuffer.Length)
            {
                if (recording.GetData(recordingBuffer, lastPos))
                {
                    //Send..
                    var amplitude = AudioUtils.GetMaxAmplitude(recordingBuffer);
                    if (amplitude >= minAmplitude)
                    {
                        index++;
                        if (OnAudioGenerated != null)
                        {
                            chunkCount++;

                            //Downsample if needed.
                            if (recordingBuffer != resampleBuffer)
                            {
                                AudioUtils.Resample(recordingBuffer, resampleBuffer, inputFreq, AudioUtils.GetFrequency(encoder.mode));
                            }

                            var data = encoder.Encode(resampleBuffer);

                            bytes += data.Length + 11;

                            VoipFragment frag = new VoipFragment(0, index, data, encoder.mode);

                            foreach (var action in OnAudioGenerated.GetInvocationList())
                            {
                                try
                                {
                                    action.DynamicInvoke(frag);
                                }
                                catch (Exception e)
                                {
                                    Misc.Logger.Error($"Exception on OnAudioGenerated event in {action.Target.GetType().ToString()}.{action.Method.Name}: {e}");
                                }
                            }
                        }
                    }
                }
                length  -= recordingBuffer.Length;
                lastPos += recordingBuffer.Length;
            }

            UpdateStats();
        }