Exemplo n.º 1
0
        internal LocalVoice(Client client, byte id, IAudioStream audioStream, object userData, int encoderSamplingRateHz, int numChannels, int bitrate, int delay)
            : base((SamplingRate)encoderSamplingRateHz, (Channels)numChannels, bitrate, OpusApplicationType.Voip, (Delay)delay)
        {
            this.client               = client;
            this.id                   = id;
            this.audioStream          = audioStream;
            this.sourceSamplingRateHz = audioStream.SamplingRate;
            this.userData             = userData;
            this.frameSize            = VoiceUtil.DelayToFrameSize((int)delay, (int)encoderSamplingRateHz, (int)numChannels);
            this.sourceFrameSize      = this.frameSize * this.sourceSamplingRateHz / (int)this.InputSamplingRate;
            this.frameBuffer          = new float[this.frameSize];
            if (this.sourceFrameSize == this.frameSize)
            {
                this.sourceFrameBuffer = this.frameBuffer;
            }
            else
            {
                this.sourceSamplingRateHz = audioStream.SamplingRate;
                this.sourceFrameBuffer    = new float[this.sourceFrameSize];

                this.client.DebugReturn(DebugLevel.WARNING, "[PV] Local voice #" + this.id + " audio source frequency " + this.sourceSamplingRateHz + " and encoder sampling rate " + (int)this.InputSamplingRate + " do not match. Resampling will occur before encoding.");
            }

            this.LevelMeter    = new LevelMeter(this.sourceSamplingRateHz, numChannels); //1/2 sec
            this.VoiceDetector = new VoiceDetector(this.sourceSamplingRateHz, numChannels);
//            _debug_decoder = new OpusDecoder(this.InputSamplingRate, this.InputChannels);
        }
        private bool readStream()
        {
            if (!this.audioStream.GetData(this.sourceFrameBuffer))
            {
                return(false);
            }

            this.LevelMeter.process(this.sourceFrameBuffer);

            // process VAD calibration (could be moved to process method of yet another processor)
            if (this.voiceDetectorCalibrateCount != 0)
            {
                this.voiceDetectorCalibrateCount -= this.sourceFrameBuffer.Length;
                if (this.voiceDetectorCalibrateCount <= 0)
                {
                    this.voiceDetectorCalibrateCount = 0;
                    this.VoiceDetector.Threshold     = LevelMeter.AccumAvgPeakAmp * 2;
                }
            }

            if (this.VoiceDetector.On)
            {
                this.VoiceDetector.process(this.sourceFrameBuffer);
                if (!this.VoiceDetector.Detected)
                {
                    return(false);
                }
            }
            if (this.sourceFrameBuffer != this.frameBuffer)
            {
                VoiceUtil.Resample(this.sourceFrameBuffer, this.frameBuffer, (int)this.opusEncoder.InputChannels);
            }
            return(true);
        }
        internal void receiveBytes(byte[] receivedBytes, byte evNumber)
        {
            // receive-gap detection and compensation
            if (evNumber != this.lastEvNumber)
            {
                int missing = VoiceUtil.byteDiff(evNumber, this.lastEvNumber);
                if (missing != 0)
                {
                    this.voiceClient.frontend.DebugReturn(DebugLevel.ALL, "[PV] evNumer: " + evNumber + " playerVoice.lastEvNumber: " + this.lastEvNumber + " missing: " + missing);
                }

                this.lastEvNumber = evNumber;

                // restoring missing frames
                for (int i = 0; i < missing; i++)
                {
                    this.receiveFrame(null);
                }
                this.voiceClient.FramesLost += missing;
            }
            this.receiveFrame(receivedBytes);
        }
Exemplo n.º 4
0
 /// <summary>Converts delay to frame size (samples*channels)</summary>
 public static int DelayToFrameSize(int _encoderDelay, int _inputSamplingRate, int numChannels)
 {
     return(VoiceUtil.DelayToSamples(_encoderDelay, _inputSamplingRate) * numChannels);
 }