public BigArray <float> GenerateMissingFrame(int frequency)
        {
            int          frameSize = frameSizes[frequency * 1000];
            codecWrapper codec     = encoders[frequency * 1000];

            short[] decodedFrame16Bit = TempArray <short> .Obtain(frameSize * 4);

            int decoded = codec.decoder.Decode(null, 0, 0, decodedFrame16Bit, 0, true);

            if (tempOutputArray.Length != decoded)
            {
                tempOutputArray.Resize(decoded);
            }

            for (int i = 0; i < decoded; i++)
            {
                float val = (float)decodedFrame16Bit[i];
                val /= short.MaxValue;
                tempOutputArray[i] = val;
            }

            TempArray <short> .Release(decodedFrame16Bit);

            return(tempOutputArray);
        }
        public BigArray <float> DecodeFrame(VoicePacketWrapper data)
        {
            int          frameSize = frameSizes[data.Frequency * 1000];
            codecWrapper codec     = encoders[data.Frequency * 1000];

            short[] decodedFrame16Bit = TempArray <short> .Obtain(frameSize * 4);

            int decoded = codec.decoder.Decode(data.RawData, 0, data.RawData.Length, decodedFrame16Bit, 0, false);

            if (tempOutputArray.Length != decoded)
            {
                tempOutputArray.Resize(decoded);
            }

            for (int i = 0; i < decoded; i++)
            {
                float val = (float)decodedFrame16Bit[i];
                val /= short.MaxValue;
                tempOutputArray[i] = val;
            }

            TempArray <short> .Release(decodedFrame16Bit);

            return(tempOutputArray);
        }
        /// <summary>
        /// For internal use only.
        /// </summary>
        // @private
        internal T[] ToTempArray()
        {
            var result = TempArray <T> .Obtain(this.count);

            Array.Copy(items, 0, result, 0, this.count);
            return(result);
        }
        public void BufferAudio(BigArray <float> audioData)
        {
            if (GetComponent <AudioSource>() == null)
            {
                return;
            }

            float[] temp = TempArray <float> .Obtain(audioData.Length);

            audioData.CopyTo(0, temp, 0, audioData.Length * 4);

            if (Equalize)
            {
                float maxAmp = AudioUtils.GetMaxAmplitude(temp);
                targetGain = TargetEqualizeVolume / maxAmp;

                if (targetGain > MaxEqualization)
                {
                    targetGain = MaxEqualization;
                }

                if (targetGain < currentGain)
                {
                    currentGain = targetGain;
                }

                AudioUtils.ApplyGain(temp, currentGain);
            }

            playClip.SetData(temp, writeHead);
            TempArray <float> .Release(temp);

            writeHead    += audioData.Length;
            totalWritten += audioData.Length;
            writeHead    %= playClip.samples;

            if (!GetComponent <AudioSource>().isPlaying)
            {
                delayForFrames--;
                if (delayForFrames <= 0)
                {
                    GetComponent <AudioSource>().Play();
                }
            }
        }
        public VoicePacketWrapper?GetNextEncodedFrame(int frequency)
        {
            int          frameSize = frameSizes[frequency];
            codecWrapper codec     = encoders[frequency];

            float[] chunk = TempArray <float> .Obtain(frameSize);

            bool chunkAvailable = chunkBuffer.RetrieveChunk(chunk);

            if (!chunkAvailable)
            {
                TempArray <float> .Release(chunk);

                return(null);
            }

            tempPacketWrapper           = new VoicePacketWrapper();
            tempPacketWrapper.Frequency = (byte)(frequency / 1000);

            short[] audio16bit = TempArray <short> .Obtain(frameSize);

            for (int i = 0; i < frameSize; i++)
            {
                float val = chunk[i] * short.MaxValue;
                audio16bit[i] = (short)val;
            }
            TempArray <float> .Release(chunk);

            byte[] buffer = TempArray <byte> .Obtain(audio16bit.Length * 2);

            int encoded = codec.encoder.Encode(audio16bit, 0, frameSize, buffer, 0, buffer.Length);

            TempArray <short> .Release(audio16bit);

            tempPacketWrapper.RawData = new byte[encoded];

            System.Buffer.BlockCopy(buffer, 0, tempPacketWrapper.RawData, 0, encoded);

            TempArray <byte> .Release(buffer);

            return(tempPacketWrapper);
        }
Beispiel #6
0
        void Update()
        {
            if (!Microphone.IsRecording(device) || recordedAudio == null)
            {
                return;
            }

            float[] tempArray = TempArray <float> .Obtain(ChunkSize);

            // in case of recompile
            if (resampleBuffer == null)
            {
                resampleBuffer = new BigArray <float>(ChunkSize, 0);
            }

            int readPosition = Microphone.GetPosition(device);

            if (readPosition >= (prevReadPosition + ChunkSize))
            {
                while (readPosition >= (prevReadPosition + ChunkSize))
                {
                    if (canTalk())
                    {
                        recordedAudio.GetData(tempArray, prevReadPosition);
                        if (exceedsVolumeThreshold(tempArray))
                        {
                            resample(tempArray);
                            bufferReady(resampleBuffer, this.recordingFrequency);
                        }
                    }

                    prevReadPosition += ChunkSize;
                }
            }
            else if (prevReadPosition > readPosition)
            {
                var endReadPos = readPosition + recordedAudio.samples;
                var diff       = endReadPos - prevReadPosition;
                while (diff >= ChunkSize)
                {
                    if (canTalk())
                    {
                        recordedAudio.GetData(tempArray, prevReadPosition);
                        if (exceedsVolumeThreshold(tempArray))
                        {
                            resample(tempArray);
                            bufferReady(resampleBuffer, this.recordingFrequency);
                        }
                    }

                    prevReadPosition += ChunkSize;
                    if (prevReadPosition >= recordedAudio.samples)
                    {
                        prevReadPosition -= recordedAudio.samples;
                        break;
                    }

                    endReadPos = readPosition + recordedAudio.samples;
                    diff       = endReadPos - prevReadPosition;
                }
            }

            TempArray <float> .Release(tempArray);
        }