Example #1
0
 public unsafe void Configure(double sampleRate, int decimationStageCount)
 {
     this._audioDecimationRatio = 1 << decimationStageCount;
     if (this._sampleRate != sampleRate)
     {
         this._sampleRate        = sampleRate;
         this._pilotFilterBuffer = UnsafeBuffer.Create(sizeof(IirFilter));
         this._pilotFilter       = (IirFilter *)(void *)this._pilotFilterBuffer;
         this._pilotFilter->Init(IirFilterType.BandPass, 19000.0, this._sampleRate, 500.0);
         this._pll->SampleRate       = (float)this._sampleRate;
         this._pll->DefaultFrequency = 19000f;
         this._pll->Range            = 20f;
         this._pll->Bandwidth        = 10f;
         this._pll->Zeta             = 0.707f;
         this._pll->PhaseAdjM        = StereoDecoder._pllPhaseAdjM;
         this._pll->PhaseAdjB        = StereoDecoder._pllPhaseAdjB;
         this._pll->LockTime         = 0.5f;
         this._pll->LockThreshold    = 1f;
         double    num    = sampleRate / (double)this._audioDecimationRatio;
         Complex[] kernel = FilterBuilder.MakeComplexKernel(num, 250, Math.Min(0.9 * num, 30000.0), 0.0, WindowType.BlackmanHarris4);
         this._channelAFilter  = new ComplexFilter(kernel);
         this._channelBFilter  = new ComplexFilter(kernel);
         this._deemphasisAlpha = (float)(1.0 - Math.Exp(-1.0 / (num * (double)StereoDecoder._deemphasisTime)));
         this._deemphasisAvgL  = 0f;
         this._deemphasisAvgR  = 0f;
     }
     if (this._channelADecimator != null && this._channelBDecimator != null && this._audioDecimationRatio == this._channelADecimator.DecimationRatio)
     {
         return;
     }
     this._channelADecimator = new FloatDecimator(this._audioDecimationRatio);
     this._channelBDecimator = new FloatDecimator(this._audioDecimationRatio);
 }
        private void DSPProc()
        {
            #region Prepare buffers

            if (_dspInBuffer == null || _dspInBuffer.Length != _inputBufferSize)
            {
                _dspInBuffer = UnsafeBuffer.Create(_inputBufferSize, sizeof(Complex));
                _dspInPtr    = (Complex *)_dspInBuffer;
            }

            if (_dspOutBuffer == null || _dspOutBuffer.Length != _outputBufferSize)
            {
                _dspOutBuffer = UnsafeBuffer.Create(_outputBufferSize, sizeof(float));
                _dspOutPtr    = (float *)_dspOutBuffer;
            }

            #endregion

            while (IsPlaying)
            {
                var total = 0;
                while (IsPlaying && total < _dspInBuffer.Length)
                {
                    var len = _dspInBuffer.Length - total;
                    total += _iqStream.Read(_dspInPtr, total, len); // Blocking read
                }

                ProcessIQ();

                _audioStream.Write(_dspOutPtr, _dspOutBuffer.Length); // Blocking write
            }
        }
Example #3
0
        private void DuplexFiller(float *buffer, int frameCount)
        {
            #region Prepare buffers

            if (_dspInBuffer == null || _dspInBuffer.Length != frameCount)
            {
                _dspInBuffer = UnsafeBuffer.Create(frameCount, sizeof(Complex));
                _dspInPtr    = (Complex *)_dspInBuffer;
            }

            if (_dspOutBuffer == null || _dspOutBuffer.Length != _dspInBuffer.Length * 2)
            {
                _dspOutBuffer = UnsafeBuffer.Create(_dspInBuffer.Length * 2, sizeof(float));
                _dspOutPtr    = (float *)_dspOutBuffer;
            }

            #endregion

            Utils.Memcpy(_dspInPtr, buffer, frameCount * sizeof(Complex));

            ProcessIQ();

            ScaleBuffer(_dspOutPtr, _dspOutBuffer.Length);
            Utils.Memcpy(buffer, _dspOutPtr, _dspOutBuffer.Length * sizeof(float));
        }
Example #4
0
 public unsafe DownConverter(int phaseCount)
 {
     this._phaseCount        = phaseCount;
     this._oscillatorsBuffer = UnsafeBuffer.Create(sizeof(Oscillator) * phaseCount);
     this._oscillators       = (Oscillator *)(void *)this._oscillatorsBuffer;
     this._isMultithreaded   = (Utils.ProcessorCount > 1);
 }
Example #5
0
 private unsafe void DSPProc()
 {
     if (this._dspInBuffer == null || this._dspInBuffer.Length != this._inputBufferSize)
     {
         this._dspInBuffer = UnsafeBuffer.Create(this._inputBufferSize, sizeof(Complex));
         this._dspInPtr    = (Complex *)(void *)this._dspInBuffer;
     }
     if (this._dspOutBuffer == null || this._dspOutBuffer.Length != this._outputBufferSize)
     {
         this._dspOutBuffer = UnsafeBuffer.Create(this._outputBufferSize, 4);
         this._dspOutPtr    = (float *)(void *)this._dspOutBuffer;
     }
     Console.WriteLine("DSPPROC started");
     while (this.IsPlaying)
     {
         int num = 0;
         while (this.IsPlaying && num < this._dspInBuffer.Length)
         {
             int count = this._dspInBuffer.Length - num;
             num += this._iqStream.Read(this._dspInPtr, num, count);
         }
         int count2 = this.ProcessBuffer();
         this._audioStream.Write(this._dspOutPtr, count2);
     }
     this._isBuffering = false;
     Console.WriteLine("DSPPROC stopped");
 }
Example #6
0
        public unsafe void Configure(double sampleRate, int decimationStageCount)
        {
            int num = (int)Math.Pow(2.0, (double)decimationStageCount);

            if (this._sampleRate != sampleRate || this._audioDecimationFactor != num)
            {
                this._sampleRate        = sampleRate;
                this._pilotFilterBuffer = UnsafeBuffer.Create(sizeof(IirFilter));
                this._pilotFilter       = (IirFilter *)(void *)this._pilotFilterBuffer;
                this._pilotFilter->Init(IirFilterType.BandPass, 19000.0, this._sampleRate, 500);
                this._pll->SampleRate       = (float)this._sampleRate;
                this._pll->DefaultFrequency = 19000f;
                this._pll->Range            = 20f;
                this._pll->Bandwidth        = 10f;
                this._pll->Zeta             = 0.707f;
                this._pll->PhaseAdjM        = StereoDecoder._pllPhaseAdjM;
                this._pll->PhaseAdjB        = StereoDecoder._pllPhaseAdjB;
                this._pll->LockTime         = 0.5f;
                this._pll->LockThreshold    = 1f;
                double  num2         = sampleRate / (double)num;
                float[] coefficients = FilterBuilder.MakeBandPassKernel(num2, 250, 20.0, 16000.0, WindowType.BlackmanHarris4);
                this._channelAFilter  = new FirFilter(coefficients, 1);
                this._channelBFilter  = new FirFilter(coefficients, 1);
                this._deemphasisAlpha = (float)(1.0 - Math.Exp(-1.0 / (num2 * (double)StereoDecoder._deemphasisTime)));
                this._deemphasisAvgL  = 0f;
                this._deemphasisAvgR  = 0f;
            }
            if (this._channelADecimator != null && this._channelBDecimator != null && this._audioDecimationFactor == num)
            {
                return;
            }
            this._channelADecimator     = new FloatDecimator(decimationStageCount);
            this._channelBDecimator     = new FloatDecimator(decimationStageCount);
            this._audioDecimationFactor = num;
        }
Example #7
0
        public static void StartHW(int freq)
        {
            if (_dllHandle == IntPtr.Zero || _startHW == null)
            {
                return;
            }

            _iqBuffer = null;
            _iqPtr    = null;

            int result = _startHW(freq);

            if (result < 0)
            {
                throw new Exception("ExtIO StartHW() returned " + result);
            }

            _isHWStarted = true;
            _sampleCount = result;

            /* Allocate the sample buffers */
            /* We must do it here since we do not know the size until the hardware is started! */
            _iqBuffer = UnsafeBuffer.Create(_sampleCount, sizeof(Complex));
            _iqPtr    = (Complex *)_iqBuffer;
        }
Example #8
0
        unsafe static ExtIO()
        {
            ExtIO._iqBuffer = UnsafeBuffer.Create(1024, sizeof(Complex));

            ExtIO._callbackInst = ExtIO.extIOCallback;
            GCHandle.Alloc(ExtIO._callbackInst);
        }
        static Trig()
        {
            _mask = ~(-1 << ResolutionInBits);
            var sampleCount = _mask + 1;

            _sinBuffer = UnsafeBuffer.Create(sampleCount, sizeof(float));
            _cosBuffer = UnsafeBuffer.Create(sampleCount, sizeof(float));
            _sinPtr    = (float *)_sinBuffer;
            _cosPtr    = (float *)_cosBuffer;

            const float twoPi = (float)(Math.PI * 2.0);
            const float pi2   = (float)(Math.PI / 2.0);

            _indexScale = sampleCount / twoPi;

            for (var i = 0; i < sampleCount; i++)
            {
                _sinPtr[i] = (float)Math.Sin((i + 0.5f) / sampleCount * twoPi);
                _cosPtr[i] = (float)Math.Cos((i + 0.5f) / sampleCount * twoPi);
            }

            for (var angle = 0.0f; angle < twoPi; angle += pi2)
            {
                _sinPtr[(int)(angle * _indexScale) & _mask] = (float)Math.Sin(angle);
                _cosPtr[(int)(angle * _indexScale) & _mask] = (float)Math.Cos(angle);
            }
        }
        public unsafe OverlapCrossfadeProcessor(int fftSize, float crossFadingRatio = 0f)
        {
            this._fftSize           = fftSize;
            this._halfSize          = this._fftSize / 2;
            this._crossFadingSize   = (int)((float)this._halfSize * crossFadingRatio);
            this._outputSize        = this._halfSize - this._crossFadingSize;
            this._inputPos          = this._halfSize + this._crossFadingSize;
            this._queuepBuffer      = UnsafeBuffer.Create(this._fftSize, sizeof(Complex));
            this._queuePtr          = (Complex *)(void *)this._queuepBuffer;
            this._windowBuffer      = UnsafeBuffer.Create(this._crossFadingSize, 4);
            this._windowPtr         = (float *)(void *)this._windowBuffer;
            this._fftBuffer         = UnsafeBuffer.Create(this._fftSize, sizeof(Complex));
            this._fftPtr            = (Complex *)(void *)this._fftBuffer;
            this._outputBuffer      = UnsafeBuffer.Create(this._outputSize, sizeof(Complex));
            this._outputPtr         = (Complex *)(void *)this._outputBuffer;
            this._crossFadingBuffer = UnsafeBuffer.Create(this._crossFadingSize, sizeof(Complex));
            this._crossFadingPtr    = (Complex *)(void *)this._crossFadingBuffer;
            double num = 1.5707963267948966 / (double)(this._crossFadingSize - 1);

            for (int i = 0; i < this._crossFadingSize; i++)
            {
                double a = (double)i * num;
                this._windowPtr[i] = (float)Math.Pow(Math.Sin(a), 2.0);
            }
        }
Example #11
0
 private unsafe void ProcessMono(float *baseBand, float *interleavedStereo, int length)
 {
     if (this._channelABuffer == null || this._channelABuffer.Length != length)
     {
         this._channelABuffer = UnsafeBuffer.Create(length, 4);
         this._channelAPtr    = (float *)(void *)this._channelABuffer;
     }
     Utils.Memcpy(this._channelAPtr, baseBand, length * 4);
     this._channelADecimator.Process(this._channelAPtr, length);
     length /= this._audioDecimationFactor;
     if (this._useFilter)
     {
         this._channelAFilter.Process(this._channelAPtr, length);
     }
     if (this._useFilter)
     {
         for (int i = 0; i < length; i++)
         {
             this._deemphasisAvgL += this._deemphasisAlpha * (this._channelAPtr[i] - this._deemphasisAvgL);
             this._channelAPtr[i]  = this._deemphasisAvgL;
         }
     }
     for (int j = 0; j < length; j++)
     {
         interleavedStereo[j * 2 + 1] = (interleavedStereo[j * 2] = this._channelAPtr[j] * 0.2f);
     }
 }
Example #12
0
        private void ProcessSquelch(float *audio, int length)
        {
            if (_squelchThreshold > 0)
            {
                if (_hissBuffer == null || _hissBuffer.Length != length)
                {
                    _hissBuffer = UnsafeBuffer.Create(length, sizeof(float));
                    _hissPtr    = (float *)_hissBuffer;
                }

                Utils.Memcpy(_hissPtr, audio, length * sizeof(float));

                _hissFilter.Process(_hissPtr, length);

                for (var i = 0; i < _hissBuffer.Length; i++)
                {
                    var n = (1 - _noiseAveragingRatio) * _noiseLevel + _noiseAveragingRatio * Math.Abs(_hissPtr[i]);
                    if (!float.IsNaN(n))
                    {
                        _noiseLevel = n;
                    }
                    if (_noiseLevel > _noiseThreshold)
                    {
                        audio[i] = 0.0f;
                    }
                }

                _isSquelchOpen = _noiseLevel < _noiseThreshold;
            }
            else
            {
                _isSquelchOpen = true;
            }
        }
Example #13
0
 public unsafe void Demodulate(Complex *iq, float *rPtr, float *lPtr, int length)
 {
     if (this._cpxBuf == null || this._cpxBuf.Length != length)
     {
         if (this._cpxBuf != null)
         {
             this._cpxBuf.Dispose();
         }
         this._cpxBuf = UnsafeBuffer.Create(length, sizeof(Complex));
         this._cpxPtr = (Complex *)(void *)this._cpxBuf;
     }
     for (int i = 0; i < length; i++)
     {
         float num  = (float)Math.Cos((double)this._phase);
         float num2 = (float)Math.Sin((double)this._phase);
         float num3 = num * iq[i].Real + num2 * iq[i].Imag;
         float num4 = (0f - num2) * iq[i].Real + num * iq[i].Imag;
         float num5 = (float)Math.Atan2((double)num4, (double)num3);
         this._freqN         += this._beta * num5;
         this._freqN          = Math.Max(this._fLowN, Math.Min(this._fhighN, this._freqN));
         this._phase         += this._freqN + this._alpha * num5;
         this._phase         %= this.TWOPI;
         this._dcReal         = 0.9999f * this._dcReal + 0.0001f * num3;
         this._dcImag         = 0.9999f * this._dcImag + 0.0001f * num4;
         this._cpxPtr[i].Real = num3 - this._dcReal;
         this._cpxPtr[i].Imag = num4 - this._dcImag;
     }
     this._cuteFir.ProcessFilter(length, this._cpxPtr, this._cpxPtr);
     for (int j = 0; j < length; j++)
     {
         Complex complex = this._cpxPtr[j];
         rPtr[j] = (complex.Real - complex.Imag) * 0.002f;
         lPtr[j] = (complex.Real + complex.Imag) * 0.002f;
     }
 }
Example #14
0
 public unsafe OverlapSlideProcessor(int fftSize, float overlapRatio)
 {
     if (overlapRatio < 0.75f)
     {
         throw new ArgumentException("Overlap ratio must be greater than or equal to 0.75", "overlapRatio");
     }
     if (overlapRatio > 1f)
     {
         throw new ArgumentException("Overlap ratio must be less than 1.0", "overlapRatio");
     }
     this._fftSize    = fftSize;
     this._outputSize = (int)Math.Round((double)((float)this._fftSize * (1f - overlapRatio)));
     if (this._outputSize < 3)
     {
         this._outputSize = 3;
     }
     this._overlapRatio  = 1f - (float)this._outputSize / (float)this._fftSize;
     this._inputPos      = this._fftSize - this._outputSize;
     this._queuepBuffer  = UnsafeBuffer.Create(this._fftSize, sizeof(Complex));
     this._queuePtr      = (Complex *)(void *)this._queuepBuffer;
     this._fftBuffer     = UnsafeBuffer.Create(this._fftSize, sizeof(Complex));
     this._fftPtr        = (Complex *)(void *)this._fftBuffer;
     this._outputBuffer  = UnsafeBuffer.Create(this._outputSize, sizeof(Complex));
     this._outputPtr     = (Complex *)(void *)this._outputBuffer;
     this._overlapBuffer = UnsafeBuffer.Create(this._outputSize, sizeof(Complex));
     this._overlapPtr    = (Complex *)(void *)this._overlapBuffer;
 }
Example #15
0
 private unsafe void ProcessSquelch(float *audio, int length)
 {
     if (this._squelchThreshold > 0)
     {
         if (this._hissBuffer == null || this._hissBuffer.Length != length)
         {
             this._hissBuffer = UnsafeBuffer.Create(length, 4);
             this._hissPtr    = (float *)(void *)this._hissBuffer;
         }
         Utils.Memcpy(this._hissPtr, audio, length * 4);
         this._hissFilter.Process(this._hissPtr, length);
         for (int i = 0; i < this._hissBuffer.Length; i++)
         {
             this._noiseLevel = (1f - this._noiseAveragingRatio) * this._noiseLevel + this._noiseAveragingRatio * Math.Abs(this._hissPtr[i]);
             if (this._noiseLevel > this._noiseThreshold)
             {
                 audio[i] *= 1E-15f;
             }
         }
         this._isSquelchOpen = (this._noiseLevel < this._noiseThreshold);
     }
     else
     {
         this._isSquelchOpen = true;
     }
 }
Example #16
0
 public unsafe static void StartHW(int freq)
 {
     if (!(ExtIO._dllHandle == IntPtr.Zero) && ExtIO._startHW != null)
     {
         ExtIO.logInfo("StartHW(), freq=" + freq.ToString());
         if (!ExtIO._isHWInit)
         {
             ExtIO.OpenHW(true);
         }
         if (ExtIO._iqBuffer != null)
         {
             ExtIO._iqBuffer.Dispose();
         }
         ExtIO._iqBuffer = null;
         ExtIO._iqPtr    = null;
         int num = ExtIO._startHW(freq);
         ExtIO.logResult("StartHW: ");
         if (num <= 0)
         {
             ExtIO.logInfo("StartHW() returned " + num);
             throw new Exception("ExtIO StartHW() returned " + num);
         }
         ExtIO._isHWStarted = true;
         ExtIO._sampleCount = num;
         ExtIO._iqBuffer    = UnsafeBuffer.Create(ExtIO._sampleCount, sizeof(Complex));
         ExtIO._iqPtr       = (Complex *)(void *)ExtIO._iqBuffer;
         ExtIO.logInfo("StartHW succeeded, samplecount=" + ExtIO._sampleCount.ToString() + ", iqBuffer created.");
     }
 }
Example #17
0
        public unsafe Resampler(double inputSampleRate, double outputSampleRate, int tapsPerPhase = 160, double protectedPassband = 0.45)
        {
            Resampler.DoubleToFraction(outputSampleRate / inputSampleRate, out this._interpolationFactor, out this._decimationFactor);
            this._tapsPerPhase = tapsPerPhase;
            int num = tapsPerPhase * this._interpolationFactor;

            this._firKernelBuffer = UnsafeBuffer.Create(num, 4);
            this._firKernel       = (float *)(void *)this._firKernelBuffer;
            double cutoffFrequency = Math.Min(inputSampleRate, outputSampleRate) * protectedPassband;

            float[] array  = FilterBuilder.MakeLowPassKernel(inputSampleRate * (double)this._interpolationFactor, num - 1, cutoffFrequency, WindowType.BlackmanHarris4);
            float[] array2 = array;
            fixed(float *ptr = array2)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    ptr[i] *= (float)this._interpolationFactor;
                }
                Utils.Memcpy(this._firKernel, ptr, (num - 1) * 4);
                this._firKernel[num - 1] = 0f;
            }

            this._firQueueBuffer = UnsafeBuffer.Create(num, 4);
            this._firQueue       = (float *)(void *)this._firQueueBuffer;
        }
Example #18
0
 private unsafe UnsafeBuffer AllocBlock()
 {
     if (this._usedBlocks.Count <= 0)
     {
         return(UnsafeBuffer.Create(8192, sizeof(Complex)));
     }
     return(this._usedBlocks.Pop());
 }
Example #19
0
 private UnsafeBuffer AllocBlock()
 {
     if (this._usedBlocks.Count <= 0)
     {
         return(UnsafeBuffer.Create(16384, 4));
     }
     return(this._usedBlocks.Pop());
 }
Example #20
0
        public unsafe int Resample(int inLength, double rate, float *pInBuf, float *pOutBuf)
        {
            if (this._workL == null || this._workL.Length != inLength / 2 + 28)
            {
                if (this._workL != null)
                {
                    this._workL.Dispose();
                }
                this._workL  = UnsafeBuffer.Create(inLength / 2 + 28, 8);
                this._pWorkL = (double *)(void *)this._workL;
            }
            if (this._workR == null || this._workR.Length != inLength / 2 + 28)
            {
                if (this._workR != null)
                {
                    this._workR.Dispose();
                }
                this._workR  = UnsafeBuffer.Create(inLength / 2 + 28, 8);
                this._pWorkR = (double *)(void *)this._workR;
            }
            int num  = (int)this._fTime;
            int num2 = 0;
            int num3 = 0;
            int i;

            for (i = 28; i < 28 + inLength / 2; i++)
            {
                this._pWorkL[i] = (double)pInBuf[num3++];
                this._pWorkR[i] = (double)pInBuf[num3++];
            }
            while (num < inLength / 2)
            {
                double num6 = 0.0;
                double num7 = 0.0;
                for (i = 1; i <= 28; i++)
                {
                    num3 = num + i;
                    int num8 = (int)(((double)num3 - this._fTime) * 10000.0);
                    num6 += this._pWorkL[num3] * this._pSinc[num8];
                    num7 += this._pWorkR[num3] * this._pSinc[num8];
                }
                pOutBuf[num2++] = (float)num6;
                pOutBuf[num2++] = (float)num7;
                this._fTime    += rate;
                num             = (int)this._fTime;
            }
            this._fTime -= (double)inLength / 2.0;
            num3         = inLength / 2;
            i            = 0;
            while (i < 28)
            {
                this._pWorkL[i] = this._pWorkL[num3];
                this._pWorkR[i] = this._pWorkR[num3];
                i++;
                num3++;
            }
            return(num2);
        }
Example #21
0
 public unsafe RdsDecoder()
 {
     this._pllBuffer        = UnsafeBuffer.Create(sizeof(Pll));
     this._pll              = (Pll *)(void *)this._pllBuffer;
     this._oscBuffer        = UnsafeBuffer.Create(sizeof(Oscillator));
     this._osc              = (Oscillator *)(void *)this._oscBuffer;
     this._syncFilterBuffer = UnsafeBuffer.Create(sizeof(IirFilter));
     this._syncFilter       = (IirFilter *)(void *)this._syncFilterBuffer;
 }
Example #22
0
 public unsafe RdsDecoder()
 {
     this._pllBuffer                  = UnsafeBuffer.Create(sizeof(Pll));
     this._pll                        = (Pll *)(void *)this._pllBuffer;
     this._syncFilterBuffer           = UnsafeBuffer.Create(sizeof(IirFilter));
     this._syncFilter                 = (IirFilter *)(void *)this._syncFilterBuffer;
     this._bitDecoder                 = new RdsDetectorBank();
     this._bitDecoder.FrameAvailable += this.FrameAvailableHandler;
 }
Example #23
0
 unsafe static Fourier()
 {
     Fourier._lutBuffer = UnsafeBuffer.Create(32768, sizeof(Complex));
     Fourier._lut       = (Complex *)(void *)Fourier._lutBuffer;
     for (int i = 0; i < 32768; i++)
     {
         Fourier._lut[i] = Complex.FromAngle(9.5873799242852573E-05 * (double)i).Conjugate();
     }
 }
        private void Configure(bool resetBuffers)
        {
            if (resetBuffers)
            {
                _sigDelayPtr = 0;
                _hangTimer   = 0;
                _peak        = -16.0f;
                _decayAve    = -5.0f;
                _attackAve   = -5.0f;
                _magBufPos   = 0;

                if (_sampleRate > 0)
                {
                    _delaySamples  = (int)(_sampleRate * DelayTimeconst);
                    _windowSamples = (int)(_sampleRate * WindowTimeconst);

                    _sigDelayBuf    = UnsafeBuffer.Create(_delaySamples, sizeof(float));
                    _sigDelayBufPtr = (float *)_sigDelayBuf;

                    _magBuf    = UnsafeBuffer.Create(_windowSamples, sizeof(float));
                    _magBufPtr = (float *)_magBuf;

                    for (int i = 0; i < _windowSamples; i++)
                    {
                        _magBufPtr[i] = -16.0f;
                    }

                    //clamp Delay samples within buffer limit
                    if (_delaySamples >= _sigDelayBuf.Length - 1)
                    {
                        _delaySamples = _sigDelayBuf.Length - 1;
                    }
                }
            }

            //calculate parameters for AGC gain as a function of input magnitude
            _knee      = _threshold / 20.0f;
            _gainSlope = _slopeFactor / 100.0f;
            _fixedGain = AGCOutscale * (float)Math.Pow(10.0, _knee * (_gainSlope - 1.0));     //fixed gain value used below knee threshold

            //calculate fast and slow filter values.
            _attackRiseAlpha = (1.0f - (float)Math.Exp(-1.0 / (_sampleRate * AttackRiseTimeconst)));
            _attackFallAlpha = (1.0f - (float)Math.Exp(-1.0 / (_sampleRate * AttackFallTimeconst)));

            _decayRiseAlpha = (1.0f - (float)Math.Exp(-1.0 / (_sampleRate * Decay * .001 * DecayRisefallRatio)));    //make rise time DECAY_RISEFALL_RATIO of fall
            _hangTime       = (int)(_sampleRate * Decay * .001);

            if (_useHang)
            {
                _decayFallAlpha = (1.0f - (float)Math.Exp(-1.0 / (_sampleRate * ReleaseTimeconst)));
            }
            else
            {
                _decayFallAlpha = (1.0f - (float)Math.Exp(-1.0 / (_sampleRate * Decay * .001)));
            }
        }
Example #25
0
 public unsafe void Process(float *baseBand, int length)
 {
     if (this._configureNeeded)
     {
         this.Configure();
         this._configureNeeded = false;
     }
     if (this._rawBuffer == null || this._rawBuffer.Length != length)
     {
         this._rawBuffer = UnsafeBuffer.Create(length, sizeof(Complex));
         this._rawPtr    = (Complex *)(void *)this._rawBuffer;
     }
     if (this._magBuffer == null || this._magBuffer.Length != length)
     {
         this._magBuffer = UnsafeBuffer.Create(length, 4);
         this._magPtr    = (float *)(void *)this._magBuffer;
     }
     if (this._dataBuffer == null || this._dataBuffer.Length != length)
     {
         this._dataBuffer = UnsafeBuffer.Create(length, 4);
         this._dataPtr    = (float *)(void *)this._dataBuffer;
     }
     for (int i = 0; i < length; i++)
     {
         this._osc.Tick();
         this._rawPtr[i] = this._osc.Phase * baseBand[i];
     }
     this._decimator.Process(this._rawPtr, length);
     length /= this._decimationFactor;
     this._baseBandFilter.Process(this._rawPtr, length);
     for (int j = 0; j < length; j++)
     {
         this._dataPtr[j] = this._pll->Process(this._rawPtr[j]).Imag;
     }
     this._matchedFilter.Process(this._dataPtr, length);
     for (int k = 0; k < length; k++)
     {
         this._magPtr[k] = Math.Abs(this._dataPtr[k]);
     }
     this._syncFilter->Process(this._magPtr, length);
     for (int l = 0; l < length; l++)
     {
         float lastData = this._dataPtr[l];
         float num      = this._magPtr[l];
         float num2     = num - this._lastSync;
         this._lastSync = num;
         if (num2 < 0f && this._lastSyncSlope * num2 < 0f)
         {
             bool flag = this._lastData > 0f;
             this._bitDecoder.Process(flag ^ this._lastBit);
             this._lastBit = flag;
         }
         this._lastData      = lastData;
         this._lastSyncSlope = num2;
     }
 }
Example #26
0
 protected CircularBuffer(int bufferSize, int elementSize, int maxBufferCount)
 {
     this._bufferSize     = bufferSize;
     this._elementSize    = elementSize;
     this._maxBufferCount = maxBufferCount;
     for (int i = 0; i < this._maxBufferCount; i++)
     {
         this._buffers.Add(UnsafeBuffer.Create(this._bufferSize, this._elementSize));
     }
 }
Example #27
0
 public unsafe IQBalancer()
 {
     this._dcRemoverIBuffer = UnsafeBuffer.Create(sizeof(DcRemover));
     this._dcRemoverI       = (DcRemover *)(void *)this._dcRemoverIBuffer;
     this._dcRemoverI->Init(1E-05f);
     this._dcRemoverQBuffer = UnsafeBuffer.Create(sizeof(DcRemover));
     this._dcRemoverQ       = (DcRemover *)(void *)this._dcRemoverQBuffer;
     this._dcRemoverQ->Init(1E-05f);
     this._isMultithreaded = (Environment.ProcessorCount > 1);
 }
Example #28
0
        private unsafe void ProcessStereo(float *baseBand, float *interleavedStereo, int length)
        {
            if (this._channelABuffer == null || this._channelABuffer.Length != length)
            {
                this._channelABuffer = UnsafeBuffer.Create(length, 4);
                this._channelAPtr    = (float *)(void *)this._channelABuffer;
            }
            if (this._channelBBuffer == null || this._channelBBuffer.Length != length)
            {
                this._channelBBuffer = UnsafeBuffer.Create(length, 4);
                this._channelBPtr    = (float *)(void *)this._channelBBuffer;
            }
            int num = length / this._audioDecimationRatio;

            Utils.Memcpy(this._channelAPtr, baseBand, length * 4);
            this._channelADecimator.Process(this._channelAPtr, length);
            this._channelAFilter.Process(this._channelAPtr, num, 1);
            for (int i = 0; i < length; i++)
            {
                float sample = this._pilotFilter->Process(baseBand[i]);
                this._pll->Process(sample);
                this._channelBPtr[i] = baseBand[i] * Trig.Sin((float)((double)this._pll->AdjustedPhase * 2.0));
            }
            if (!this._pll->IsLocked)
            {
                for (int j = 0; j < num; j++)
                {
                    this._deemphasisAvgL += this._deemphasisAlpha * (this._channelAPtr[j] - this._deemphasisAvgL);
                    this._channelAPtr[j]  = this._deemphasisAvgL;
                }
                for (int k = 0; k < num; k++)
                {
                    interleavedStereo[k * 2 + 1] = (interleavedStereo[k * 2] = this._channelAPtr[k] * 0.2f);
                }
            }
            else
            {
                this._channelBDecimator.Process(this._channelBPtr, length);
                this._channelBFilter.Process(this._channelBPtr, num, 1);
                for (int l = 0; l < num; l++)
                {
                    float num2 = this._channelAPtr[l];
                    float num3 = 2f * this._channelBPtr[l];
                    interleavedStereo[l * 2]     = (num2 + num3) * 0.2f;
                    interleavedStereo[l * 2 + 1] = (num2 - num3) * 0.2f;
                }
                for (int m = 0; m < num; m++)
                {
                    this._deemphasisAvgL        += this._deemphasisAlpha * (interleavedStereo[m * 2] - this._deemphasisAvgL);
                    interleavedStereo[m * 2]     = this._deemphasisAvgL;
                    this._deemphasisAvgR        += this._deemphasisAlpha * (interleavedStereo[m * 2 + 1] - this._deemphasisAvgR);
                    interleavedStereo[m * 2 + 1] = this._deemphasisAvgR;
                }
            }
        }
Example #29
0
        public RdsDecoder()
        {
            _pllBuffer = UnsafeBuffer.Create(sizeof(Pll));
            _pll       = (Pll *)_pllBuffer;

            _oscBuffer = UnsafeBuffer.Create(sizeof(Oscillator));
            _osc       = (Oscillator *)_oscBuffer;

            _syncFilterBuffer = UnsafeBuffer.Create(sizeof(IirFilter));
            _syncFilter       = (IirFilter *)_syncFilterBuffer;
        }
Example #30
0
 unsafe static Fourier()
 {
     Fourier._lrBuffer = UnsafeBuffer.Create(32768, 4);
     Fourier._liBuffer = UnsafeBuffer.Create(32768, 4);
     Fourier._lr       = (float *)(void *)Fourier._lrBuffer;
     Fourier._li       = (float *)(void *)Fourier._liBuffer;
     for (int i = 0; i < 32768; i++)
     {
         Fourier._lr[i] = (float)Math.Cos(9.5873799242852573E-05 * (double)i);
         Fourier._li[i] = (float)(0.0 - Math.Sin(9.5873799242852573E-05 * (double)i));
     }
 }