예제 #1
0
        public sealed override int Quant(float[] target, float[] sw, int sws, float[] ak, float[] awk1, float[] awk2, float[] exc, int es, int start, int end, float pitch_coef, int p, int nsf, Bits bits, float[] exc2, int e2s, float[] r, int complexity)
        {
            int[] array = new int[1];
            int   num   = 0;
            int   data  = 0;
            int   num2  = 0;
            float num3  = -1f;
            int   num4  = complexity;

            if (num4 > 10)
            {
                num4 = 10;
            }
            int[]   array2 = new int[num4];
            float[] array3 = new float[num4];
            if (num4 == 0 || end < start)
            {
                bits.Pack(0, this.pitch_bits);
                bits.Pack(0, this.gain_bits);
                for (int i = 0; i < nsf; i++)
                {
                    exc[es + i] = 0f;
                }
                return(start);
            }
            float[] array4 = new float[nsf];
            if (num4 > end - start + 1)
            {
                num4 = end - start + 1;
            }
            Ltp.Open_loop_nbest_pitch(sw, sws, start, end, nsf, array2, array3, num4);
            for (int i = 0; i < num4; i++)
            {
                num = array2[i];
                for (int j = 0; j < nsf; j++)
                {
                    exc[es + j] = 0f;
                }
                float num5 = this.Pitch_gain_search_3tap(target, ak, awk1, awk2, exc, es, num, p, nsf, bits, exc2, e2s, r, array);
                if (num5 < num3 || num3 < 0f)
                {
                    for (int j = 0; j < nsf; j++)
                    {
                        array4[j] = exc[es + j];
                    }
                    num3 = num5;
                    num2 = num;
                    data = array[0];
                }
            }
            bits.Pack(num2 - start, this.pitch_bits);
            bits.Pack(data, this.gain_bits);
            for (int i = 0; i < nsf; i++)
            {
                exc[es + i] = array4[i];
            }
            return(num);
        }
예제 #2
0
 public SubMode(int lbrPitch, int forcedPitchGain, int haveSubframeGain, int doubleCodebook, LspQuant lspQuant, Ltp ltp, CodebookSearch innovation, float lpcEnhK1, float lpcEnhK2, float combGain, int bitsPerFrame)
 {
     this.lbrPitch         = lbrPitch;
     this.forcedPitchGain  = forcedPitchGain;
     this.haveSubframeGain = haveSubframeGain;
     this.doubleCodebook   = doubleCodebook;
     this.lsqQuant         = lspQuant;
     this.ltp          = ltp;
     this.innovation   = innovation;
     this.lpcEnhK1     = lpcEnhK1;
     this.lpcEnhK2     = lpcEnhK2;
     this.combGain     = combGain;
     this.bitsPerFrame = bitsPerFrame;
 }
예제 #3
0
        public int Encode(Bits bits, float[] vin)
        {
            int i;

            float[] res, target, mem;
            float[] syn_resp;
            float[] orig;
            /* Copy new data in input buffer */
            System.Array.Copy(frmBuf, frameSize, frmBuf, 0, bufSize - frameSize);
            frmBuf[bufSize - frameSize] = vin[0] - preemph * pre_mem;
            for (i = 1; i < frameSize; i++)
            {
                frmBuf[bufSize - frameSize + i] = vin[i] - preemph * vin[i - 1];
            }
            pre_mem = vin[frameSize - 1];

            /* Move signals 1 frame towards the past */
            System.Array.Copy(exc2Buf, frameSize, exc2Buf, 0, bufSize - frameSize);
            System.Array.Copy(excBuf, frameSize, excBuf, 0, bufSize - frameSize);
            System.Array.Copy(swBuf, frameSize, swBuf, 0, bufSize - frameSize);

            /* Window for analysis */
            for (i = 0; i < windowSize; i++)
            {
                buf2[i] = frmBuf[i + frmIdx] * window[i];
            }

            /* Compute auto-correlation */
            Lpc.autocorr(buf2, autocorr, lpcSize + 1, windowSize);

            autocorr[0] += 10;        /* prevents NANs */
            autocorr[0] *= lpc_floor; /* Noise floor in auto-correlation domain */

            /* Lag windowing: equivalent to filtering in the power-spectrum domain */
            for (i = 0; i < lpcSize + 1; i++)
            {
                autocorr[i] *= lagWindow[i];
            }

            /* Levinson-Durbin */
            Lpc.wld(lpc, autocorr, rc, lpcSize); // tmperr
            System.Array.Copy(lpc, 0, lpc, 1, lpcSize);
            lpc[0] = 1;

            /* LPC to LSPs (x-domain) transform */
            int roots = Lsp.lpc2lsp(lpc, lpcSize, lsp, 15, 0.2f);

            /* Check if we found all the roots */
            if (roots == lpcSize)
            {
                /* LSP x-domain to angle domain*/
                for (i = 0; i < lpcSize; i++)
                {
                    lsp[i] = (float)Math.Acos(lsp[i]);
                }
            }
            else
            {
                /* Search again if we can afford it */
                if (complexity > 1)
                {
                    roots = Lsp.lpc2lsp(lpc, lpcSize, lsp, 11, 0.05f);
                }
                if (roots == lpcSize)
                {
                    /* LSP x-domain to angle domain*/
                    for (i = 0; i < lpcSize; i++)
                    {
                        lsp[i] = (float)Math.Acos(lsp[i]);
                    }
                }
                else
                {
                    /*If we can't find all LSP's, do some damage control and use previous filter*/
                    for (i = 0; i < lpcSize; i++)
                    {
                        lsp[i] = old_lsp[i];
                    }
                }
            }

            float lsp_dist = 0;

            for (i = 0; i < lpcSize; i++)
            {
                lsp_dist += (old_lsp[i] - lsp[i]) * (old_lsp[i] - lsp[i]);
            }

            /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */
            float ol_gain;
            int   ol_pitch;
            float ol_pitch_coef;

            {
                if (first != 0)
                {
                    for (i = 0; i < lpcSize; i++)
                    {
                        interp_lsp[i] = lsp[i];
                    }
                }
                else
                {
                    for (i = 0; i < lpcSize; i++)
                    {
                        interp_lsp[i] = .375f * old_lsp[i] + .625f * lsp[i];
                    }
                }

                Lsp.enforce_margin(interp_lsp, lpcSize, .002f);

                /* Compute interpolated LPCs (unquantized) for whole frame*/
                for (i = 0; i < lpcSize; i++)
                {
                    interp_lsp[i] = (float)Math.Cos(interp_lsp[i]);
                }
                m_lsp.lsp2lpc(interp_lsp, interp_lpc, lpcSize);

                /*Open-loop pitch*/
                if (submodes[submodeID] == null ||
                    vbr_enabled != 0 || vad_enabled != 0 ||
                    submodes[submodeID].forced_pitch_gain != 0 ||
                    submodes[submodeID].lbr_pitch != -1)
                {
                    int[]   nol_pitch      = new int[6];
                    float[] nol_pitch_coef = new float[6];

                    Filters.bw_lpc(gamma1, interp_lpc, bw_lpc1, lpcSize);
                    Filters.bw_lpc(gamma2, interp_lpc, bw_lpc2, lpcSize);

                    Filters.filter_mem2(frmBuf, frmIdx, bw_lpc1, bw_lpc2, swBuf, swIdx,
                                        frameSize, lpcSize, mem_sw_whole, 0);

                    Ltp.open_loop_nbest_pitch(swBuf, swIdx, min_pitch, max_pitch, frameSize,
                                              nol_pitch, nol_pitch_coef, 6);
                    ol_pitch      = nol_pitch[0];
                    ol_pitch_coef = nol_pitch_coef[0];
                    /*Try to remove pitch multiples*/
                    for (i = 1; i < 6; i++)
                    {
                        if ((nol_pitch_coef[i] > .85 * ol_pitch_coef) &&
                            (Math.Abs(nol_pitch[i] - ol_pitch / 2.0) <= 1 ||
                             Math.Abs(nol_pitch[i] - ol_pitch / 3.0) <= 1 ||
                             Math.Abs(nol_pitch[i] - ol_pitch / 4.0) <= 1 ||
                             Math.Abs(nol_pitch[i] - ol_pitch / 5.0) <= 1))
                        {
                            /*ol_pitch_coef=nol_pitch_coef[i];*/
                            ol_pitch = nol_pitch[i];
                        }
                    }

                    /*if (ol_pitch>50)
                     * ol_pitch/=2;*/
                    /*ol_pitch_coef = sqrt(ol_pitch_coef);*/
                }
                else
                {
                    ol_pitch      = 0;
                    ol_pitch_coef = 0;
                }
                /*Compute "real" excitation*/
                Filters.fir_mem2(frmBuf, frmIdx, interp_lpc, excBuf, excIdx, frameSize, lpcSize, mem_exc);

                /* Compute open-loop excitation gain */
                ol_gain = 0;
                for (i = 0; i < frameSize; i++)
                {
                    ol_gain += excBuf[excIdx + i] * excBuf[excIdx + i];
                }

                ol_gain = (float)Math.Sqrt(1 + ol_gain / frameSize);
            }

            /*VBR stuff*/
            if (vbr != null && (vbr_enabled != 0 || vad_enabled != 0))
            {
                if (abr_enabled != 0)
                {
                    float qual_change = 0;
                    if (abr_drift2 * abr_drift > 0)
                    {
                        /* Only adapt if long-term and short-term drift are the same sign */
                        qual_change = -.00001f * abr_drift / (1 + abr_count);
                        if (qual_change > .05f)
                        {
                            qual_change = .05f;
                        }
                        if (qual_change < -.05f)
                        {
                            qual_change = -.05f;
                        }
                    }
                    vbr_quality += qual_change;
                    if (vbr_quality > 10)
                    {
                        vbr_quality = 10;
                    }
                    if (vbr_quality < 0)
                    {
                        vbr_quality = 0;
                    }
                }
                relative_quality = vbr.analysis(vin, frameSize, ol_pitch, ol_pitch_coef);
                /*if (delta_qual<0)*/
                /*  delta_qual*=.1*(3+st->vbr_quality);*/
                if (vbr_enabled != 0)
                {
                    int   mode;
                    int   choice   = 0;
                    float min_diff = 100;
                    mode = 8;
                    while (mode > 0)
                    {
                        int   v1;
                        float thresh;
                        v1 = (int)Math.Floor(vbr_quality);
                        if (v1 == 10)
                        {
                            thresh = NSpeex.Vbr.nb_thresh[mode][v1];
                        }
                        else
                        {
                            thresh = (vbr_quality - v1) * NSpeex.Vbr.nb_thresh[mode][v1 + 1] +
                                     (1 + v1 - vbr_quality) * NSpeex.Vbr.nb_thresh[mode][v1];
                        }
                        if (relative_quality > thresh &&
                            relative_quality - thresh < min_diff)
                        {
                            choice   = mode;
                            min_diff = relative_quality - thresh;
                        }
                        mode--;
                    }
                    mode = choice;
                    if (mode == 0)
                    {
                        if (dtx_count == 0 || lsp_dist > .05 || dtx_enabled == 0 || dtx_count > 20)
                        {
                            mode      = 1;
                            dtx_count = 1;
                        }
                        else
                        {
                            mode = 0;
                            dtx_count++;
                        }
                    }
                    else
                    {
                        dtx_count = 0;
                    }


                    Mode = mode;

                    if (abr_enabled != 0)
                    {
                        int bitrate;
                        bitrate    = BitRate;
                        abr_drift += (bitrate - abr_enabled);
                        abr_drift2 = .95f * abr_drift2 + .05f * (bitrate - abr_enabled);
                        abr_count += 1.0f;
                    }
                }
                else
                {
                    /*VAD only case*/
                    int mode;
                    if (relative_quality < 2)
                    {
                        if (dtx_count == 0 || lsp_dist > .05 || dtx_enabled == 0 || dtx_count > 20)
                        {
                            dtx_count = 1;
                            mode      = 1;
                        }
                        else
                        {
                            mode = 0;
                            dtx_count++;
                        }
                    }
                    else
                    {
                        dtx_count = 0;
                        mode      = submodeSelect;
                    }
                    /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
                    submodeID = mode;
                }
            }
            else
            {
                relative_quality = -1;
            }

            /* First, transmit a zero for narrowband */
            bits.Pack(0, 1);

            /* Transmit the sub-mode we use for this frame */
            bits.Pack(submodeID, NB_SUBMODE_BITS);

            /* If null mode (no transmission), just set a couple things to zero*/
            if (submodes[submodeID] == null)
            {
                for (i = 0; i < frameSize; i++)
                {
                    excBuf[excIdx + i] = exc2Buf[exc2Idx + i] = swBuf[swIdx + i] = VERY_SMALL;
                }

                for (i = 0; i < lpcSize; i++)
                {
                    mem_sw[i] = 0;
                }
                first         = 1;
                bounded_pitch = 1;

                /* Final signal synthesis from excitation */
                Filters.iir_mem2(excBuf, excIdx, interp_qlpc, frmBuf, frmIdx, frameSize, lpcSize, mem_sp);

                vin[0] = frmBuf[frmIdx] + preemph * pre_mem2;
                for (i = 1; i < frameSize; i++)
                {
                    vin[i] = frmBuf[frmIdx = i] + preemph * vin[i - 1];
                }
                pre_mem2 = vin[frameSize - 1];

                return(0);
            }

            /* LSP Quantization */
            if (first != 0)
            {
                for (i = 0; i < lpcSize; i++)
                {
                    old_lsp[i] = lsp[i];
                }
            }

            /*Quantize LSPs*/
            //#if 1 /*0 for unquantized*/
            submodes[submodeID].lsqQuant.quant(lsp, qlsp, lpcSize, bits);
            //#else
            //     for (i=0;i<lpcSize;i++)
            //       qlsp[i]=lsp[i];
            //#endif

            /*If we use low bit-rate pitch mode, transmit open-loop pitch*/
            if (submodes[submodeID].lbr_pitch != -1)
            {
                bits.Pack(ol_pitch - min_pitch, 7);
            }

            if (submodes[submodeID].forced_pitch_gain != 0)
            {
                int quant;
                quant = (int)Math.Floor(.5 + 15 * ol_pitch_coef);
                if (quant > 15)
                {
                    quant = 15;
                }
                if (quant < 0)
                {
                    quant = 0;
                }
                bits.Pack(quant, 4);
                ol_pitch_coef = (float)0.066667 * quant;
            }

            /*Quantize and transmit open-loop excitation gain*/
            {
                int qe = (int)(Math.Floor(0.5 + 3.5 * Math.Log(ol_gain)));
                if (qe < 0)
                {
                    qe = 0;
                }
                if (qe > 31)
                {
                    qe = 31;
                }
                ol_gain = (float)Math.Exp(qe / 3.5);
                bits.Pack(qe, 5);
            }

            /* Special case for first frame */
            if (first != 0)
            {
                for (i = 0; i < lpcSize; i++)
                {
                    old_qlsp[i] = qlsp[i];
                }
            }

            /* Filter response */
            res = new float[subframeSize];
            /* Target signal */
            target   = new float[subframeSize];
            syn_resp = new float[subframeSize];
            mem      = new float[lpcSize];
            orig     = new float[frameSize];
            for (i = 0; i < frameSize; i++)
            {
                orig[i] = frmBuf[frmIdx + i];
            }

            /* Loop on sub-frames */
            for (int sub = 0; sub < nbSubframes; sub++)
            {
                float tmp;
                int   offset;
                int   sp, sw, exc, exc2;
                int   pitchval;

                /* Offset relative to start of frame */
                offset = subframeSize * sub;
                /* Original signal */
                sp = frmIdx + offset;
                /* Excitation */
                exc = excIdx + offset;
                /* Weighted signal */
                sw = swIdx + offset;

                exc2 = exc2Idx + offset;

                /* LSP interpolation (quantized and unquantized) */
                tmp = (float)(1.0 + sub) / nbSubframes;
                for (i = 0; i < lpcSize; i++)
                {
                    interp_lsp[i] = (1 - tmp) * old_lsp[i] + tmp * lsp[i];
                }
                for (i = 0; i < lpcSize; i++)
                {
                    interp_qlsp[i] = (1 - tmp) * old_qlsp[i] + tmp * qlsp[i];
                }

                /* Make sure the filters are stable */
                Lsp.enforce_margin(interp_lsp, lpcSize, .002f);
                Lsp.enforce_margin(interp_qlsp, lpcSize, .002f);

                /* Compute interpolated LPCs (quantized and unquantized) */
                for (i = 0; i < lpcSize; i++)
                {
                    interp_lsp[i] = (float)Math.Cos(interp_lsp[i]);
                }
                m_lsp.lsp2lpc(interp_lsp, interp_lpc, lpcSize);

                for (i = 0; i < lpcSize; i++)
                {
                    interp_qlsp[i] = (float)Math.Cos(interp_qlsp[i]);
                }
                m_lsp.lsp2lpc(interp_qlsp, interp_qlpc, lpcSize);

                /* Compute analysis filter gain at w=pi (for use in SB-CELP) */
                tmp          = 1;
                pi_gain[sub] = 0;
                for (i = 0; i <= lpcSize; i++)
                {
                    pi_gain[sub] += tmp * interp_qlpc[i];
                    tmp           = -tmp;
                }

                /* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */
                Filters.bw_lpc(gamma1, interp_lpc, bw_lpc1, lpcSize);
                if (gamma2 >= 0)
                {
                    Filters.bw_lpc(gamma2, interp_lpc, bw_lpc2, lpcSize);
                }
                else
                {
                    bw_lpc2[0] = 1;
                    bw_lpc2[1] = -preemph;
                    for (i = 2; i <= lpcSize; i++)
                    {
                        bw_lpc2[i] = 0;
                    }
                }

                /* Compute impulse response of A(z/g1) / ( A(z)*A(z/g2) )*/
                for (i = 0; i < subframeSize; i++)
                {
                    excBuf[exc + i] = 0;
                }
                excBuf[exc] = 1;
                Filters.syn_percep_zero(excBuf, exc, interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, subframeSize, lpcSize);

                /* Reset excitation */
                for (i = 0; i < subframeSize; i++)
                {
                    excBuf[exc + i] = 0;
                }
                for (i = 0; i < subframeSize; i++)
                {
                    exc2Buf[exc2 + i] = 0;
                }

                /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */
                for (i = 0; i < lpcSize; i++)
                {
                    mem[i] = mem_sp[i];
                }
                Filters.iir_mem2(excBuf, exc, interp_qlpc, excBuf, exc, subframeSize, lpcSize, mem);

                for (i = 0; i < lpcSize; i++)
                {
                    mem[i] = mem_sw[i];
                }
                Filters.filter_mem2(excBuf, exc, bw_lpc1, bw_lpc2, res, 0, subframeSize, lpcSize, mem, 0);

                /* Compute weighted signal */
                for (i = 0; i < lpcSize; i++)
                {
                    mem[i] = mem_sw[i];
                }
                Filters.filter_mem2(frmBuf, sp, bw_lpc1, bw_lpc2, swBuf, sw, subframeSize, lpcSize, mem, 0);

                /* Compute target signal */
                for (i = 0; i < subframeSize; i++)
                {
                    target[i] = swBuf[sw + i] - res[i];
                }

                for (i = 0; i < subframeSize; i++)
                {
                    excBuf[exc + i] = exc2Buf[exc2 + i] = 0;
                }

                /* If we have a long-term predictor (otherwise, something's wrong) */
                //    if (submodes[submodeID].ltp.quant)
                //    {
                int pit_min, pit_max;
                /* Long-term prediction */
                if (submodes[submodeID].lbr_pitch != -1)
                {
                    /* Low bit-rate pitch handling */
                    int margin;
                    margin = submodes[submodeID].lbr_pitch;
                    if (margin != 0)
                    {
                        if (ol_pitch < min_pitch + margin - 1)
                        {
                            ol_pitch = min_pitch + margin - 1;
                        }
                        if (ol_pitch > max_pitch - margin)
                        {
                            ol_pitch = max_pitch - margin;
                        }
                        pit_min = ol_pitch - margin + 1;
                        pit_max = ol_pitch + margin;
                    }
                    else
                    {
                        pit_min = pit_max = ol_pitch;
                    }
                }
                else
                {
                    pit_min = min_pitch;
                    pit_max = max_pitch;
                }

                /* Force pitch to use only the current frame if needed */
                if (bounded_pitch != 0 && pit_max > offset)
                {
                    pit_max = offset;
                }

                /* Perform pitch search */
                pitchval = submodes[submodeID].ltp.Quant(target, swBuf, sw, interp_qlpc, bw_lpc1, bw_lpc2,
                                                         excBuf, exc, pit_min, pit_max, ol_pitch_coef, lpcSize,
                                                         subframeSize, bits, exc2Buf, exc2, syn_resp, complexity);

                pitch[sub] = pitchval;

                //    } else {
                //      speex_error ("No pitch prediction, what's wrong");
                //    }

                /* Update target for adaptive codebook contribution */
                Filters.syn_percep_zero(excBuf, exc, interp_qlpc, bw_lpc1, bw_lpc2, res, subframeSize, lpcSize);
                for (i = 0; i < subframeSize; i++)
                {
                    target[i] -= res[i];
                }

                /* Quantization of innovation */
                {
                    int   innovptr;
                    float ener = 0, ener_1;

                    innovptr = sub * subframeSize;
                    for (i = 0; i < subframeSize; i++)
                    {
                        innov[innovptr + i] = 0;
                    }

                    Filters.residue_percep_zero(target, 0, interp_qlpc, bw_lpc1, bw_lpc2, buf2, subframeSize, lpcSize);
                    for (i = 0; i < subframeSize; i++)
                    {
                        ener += buf2[i] * buf2[i];
                    }
                    ener = (float)Math.Sqrt(.1f + ener / subframeSize);

                    /*for (i=0;i<subframeSize;i++)
                     * System.out.print(buf2[i]/ener + "\t");
                     */

                    ener /= ol_gain;

                    /* Calculate gain correction for the sub-frame (if any) */
                    if (submodes[submodeID].have_subframe_gain != 0)
                    {
                        int qe;
                        ener = (float)Math.Log(ener);
                        if (submodes[submodeID].have_subframe_gain == 3)
                        {
                            qe = VQ.index(ener, exc_gain_quant_scal3, 8);
                            bits.Pack(qe, 3);
                            ener = exc_gain_quant_scal3[qe];
                        }
                        else
                        {
                            qe = VQ.index(ener, exc_gain_quant_scal1, 2);
                            bits.Pack(qe, 1);
                            ener = exc_gain_quant_scal1[qe];
                        }
                        ener = (float)Math.Exp(ener);
                    }
                    else
                    {
                        ener = 1;
                    }

                    ener *= ol_gain;

                    /*System.out.println(ener + " " + ol_gain);*/

                    ener_1 = 1 / ener;

                    /* Normalize innovation */
                    for (i = 0; i < subframeSize; i++)
                    {
                        target[i] *= ener_1;
                    }

                    /* Quantize innovation */
                    //      if (submodes[submodeID].innovation != null)
                    //      {
                    /* Codebook search */
                    submodes[submodeID].innovation.Quant(target, interp_qlpc, bw_lpc1, bw_lpc2,
                                                         lpcSize, subframeSize, innov,
                                                         innovptr, syn_resp, bits, complexity);

                    /* De-normalize innovation and update excitation */
                    for (i = 0; i < subframeSize; i++)
                    {
                        innov[innovptr + i] *= ener;
                    }
                    for (i = 0; i < subframeSize; i++)
                    {
                        excBuf[exc + i] += innov[innovptr + i];
                    }
                    //      } else {
                    //        speex_error("No fixed codebook");
                    //      }

                    /* In some (rare) modes, we do a second search (more bits) to reduce noise even more */
                    if (submodes[submodeID].double_codebook != 0)
                    {
                        float[] innov2 = new float[subframeSize];
                        //          for (i=0;i<subframeSize;i++)
                        //            innov2[i]=0;
                        for (i = 0; i < subframeSize; i++)
                        {
                            target[i] *= 2.2f;
                        }
                        submodes[submodeID].innovation.Quant(target, interp_qlpc, bw_lpc1, bw_lpc2,
                                                             lpcSize, subframeSize, innov2, 0,
                                                             syn_resp, bits, complexity);
                        for (i = 0; i < subframeSize; i++)
                        {
                            innov2[i] *= ener * (1f / 2.2f);
                        }
                        for (i = 0; i < subframeSize; i++)
                        {
                            excBuf[exc + i] += innov2[i];
                        }
                    }

                    for (i = 0; i < subframeSize; i++)
                    {
                        target[i] *= ener;
                    }
                }

                /*Keep the previous memory*/
                for (i = 0; i < lpcSize; i++)
                {
                    mem[i] = mem_sp[i];
                }
                /* Final signal synthesis from excitation */
                Filters.iir_mem2(excBuf, exc, interp_qlpc, frmBuf, sp, subframeSize, lpcSize, mem_sp);

                /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */
                Filters.filter_mem2(frmBuf, sp, bw_lpc1, bw_lpc2, swBuf, sw, subframeSize, lpcSize, mem_sw, 0);
                for (i = 0; i < subframeSize; i++)
                {
                    exc2Buf[exc2 + i] = excBuf[exc + i];
                }
            }

            /* Store the LSPs for interpolation in the next frame */
            if (submodeID >= 1)
            {
                for (i = 0; i < lpcSize; i++)
                {
                    old_lsp[i] = lsp[i];
                }
                for (i = 0; i < lpcSize; i++)
                {
                    old_qlsp[i] = qlsp[i];
                }
            }

            if (submodeID == 1)
            {
                if (dtx_count != 0)
                {
                    bits.Pack(15, 4);
                }
                else
                {
                    bits.Pack(0, 4);
                }
            }

            /* The next frame will not be the first (Duh!) */
            first = 0;

            {
                float ener = 0, err = 0;
                float snr;
                for (i = 0; i < frameSize; i++)
                {
                    ener += frmBuf[frmIdx + i] * frmBuf[frmIdx + i];
                    err  += (frmBuf[frmIdx + i] - orig[i]) * (frmBuf[frmIdx + i] - orig[i]);
                }
                snr = (float)(10 * Math.Log((ener + 1) / (err + 1)));
                /*System.out.println("Frame result: SNR="+snr+" E="+ener+" Err="+err+"\r\n");*/
            }

            /* Replace input by synthesized speech */
            vin[0] = frmBuf[frmIdx] + preemph * pre_mem2;
            for (i = 1; i < frameSize; i++)
            {
                vin[i] = frmBuf[frmIdx + i] + preemph * vin[i - 1];
            }
            pre_mem2 = vin[frameSize - 1];

            if (submodes[submodeID].innovation is NoiseSearch || submodeID == 0)
            {
                bounded_pitch = 1;
            }
            else
            {
                bounded_pitch = 0;
            }

            return(1);
        }
예제 #4
0
        public virtual int Encode(Bits bits, float[] ins0)
        {
            Array.Copy(this.frmBuf, this.frameSize, this.frmBuf, 0, this.bufSize - this.frameSize);
            this.frmBuf[this.bufSize - this.frameSize] = ins0[0] - this.preemph * this.pre_mem;
            for (int i = 1; i < this.frameSize; i++)
            {
                this.frmBuf[this.bufSize - this.frameSize + i] = ins0[i] - this.preemph * ins0[i - 1];
            }
            this.pre_mem = ins0[this.frameSize - 1];
            Array.Copy(this.exc2Buf, this.frameSize, this.exc2Buf, 0, this.bufSize - this.frameSize);
            Array.Copy(this.excBuf, this.frameSize, this.excBuf, 0, this.bufSize - this.frameSize);
            Array.Copy(this.swBuf, this.frameSize, this.swBuf, 0, this.bufSize - this.frameSize);
            for (int i = 0; i < this.windowSize; i++)
            {
                this.buf2[i] = this.frmBuf[i + this.frmIdx] * this.window[i];
            }
            Lpc.Autocorr(this.buf2, this.autocorr, this.lpcSize + 1, this.windowSize);
            this.autocorr[0] += 10f;
            this.autocorr[0] *= this.lpc_floor;
            for (int i = 0; i < this.lpcSize + 1; i++)
            {
                this.autocorr[i] *= this.lagWindow[i];
            }
            Lpc.Wld(this.lpc, this.autocorr, this.rc, this.lpcSize);
            Array.Copy(this.lpc, 0, this.lpc, 1, this.lpcSize);
            this.lpc[0] = 1f;
            int num = Lsp.Lpc2lsp(this.lpc, this.lpcSize, this.lsp, 15, 0.2f);

            if (num == this.lpcSize)
            {
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.lsp[i] = (float)Math.Acos((double)this.lsp[i]);
                }
            }
            else
            {
                if (this.complexity > 1)
                {
                    num = Lsp.Lpc2lsp(this.lpc, this.lpcSize, this.lsp, 11, 0.05f);
                }
                if (num == this.lpcSize)
                {
                    for (int i = 0; i < this.lpcSize; i++)
                    {
                        this.lsp[i] = (float)Math.Acos((double)this.lsp[i]);
                    }
                }
                else
                {
                    for (int i = 0; i < this.lpcSize; i++)
                    {
                        this.lsp[i] = this.old_lsp[i];
                    }
                }
            }
            float num2 = 0f;

            for (int i = 0; i < this.lpcSize; i++)
            {
                num2 += (this.old_lsp[i] - this.lsp[i]) * (this.old_lsp[i] - this.lsp[i]);
            }
            if (this.first != 0)
            {
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.interp_lsp[i] = this.lsp[i];
                }
            }
            else
            {
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.interp_lsp[i] = 0.375f * this.old_lsp[i] + 0.625f * this.lsp[i];
                }
            }
            Lsp.Enforce_margin(this.interp_lsp, this.lpcSize, 0.002f);
            for (int i = 0; i < this.lpcSize; i++)
            {
                this.interp_lsp[i] = (float)Math.Cos((double)this.interp_lsp[i]);
            }
            this.m_lsp.Lsp2lpc(this.interp_lsp, this.interp_lpc, this.lpcSize);
            int   num3;
            float num4;

            if (this.submodes[this.submodeID] == null || this.vbr_enabled != 0 || this.vad_enabled != 0 || this.submodes[this.submodeID].ForcedPitchGain != 0 || this.submodes[this.submodeID].LbrPitch != -1)
            {
                int[]   array  = new int[6];
                float[] array2 = new float[6];
                Filters.Bw_lpc(this.gamma1, this.interp_lpc, this.bw_lpc1, this.lpcSize);
                Filters.Bw_lpc(this.gamma2, this.interp_lpc, this.bw_lpc2, this.lpcSize);
                Filters.Filter_mem2(this.frmBuf, this.frmIdx, this.bw_lpc1, this.bw_lpc2, this.swBuf, this.swIdx, this.frameSize, this.lpcSize, this.mem_sw_whole, 0);
                Ltp.Open_loop_nbest_pitch(this.swBuf, this.swIdx, this.min_pitch, this.max_pitch, this.frameSize, array, array2, 6);
                num3 = array[0];
                num4 = array2[0];
                for (int i = 1; i < 6; i++)
                {
                    if ((double)array2[i] > 0.85 * (double)num4 && (Math.Abs((double)array[i] - (double)num3 / 2.0) <= 1.0 || Math.Abs((double)array[i] - (double)num3 / 3.0) <= 1.0 || Math.Abs((double)array[i] - (double)num3 / 4.0) <= 1.0 || Math.Abs((double)array[i] - (double)num3 / 5.0) <= 1.0))
                    {
                        num3 = array[i];
                    }
                }
            }
            else
            {
                num3 = 0;
                num4 = 0f;
            }
            Filters.Fir_mem2(this.frmBuf, this.frmIdx, this.interp_lpc, this.excBuf, this.excIdx, this.frameSize, this.lpcSize, this.mem_exc);
            float num5 = 0f;

            for (int i = 0; i < this.frameSize; i++)
            {
                num5 += this.excBuf[this.excIdx + i] * this.excBuf[this.excIdx + i];
            }
            num5 = (float)Math.Sqrt((double)(1f + num5 / (float)this.frameSize));
            if (this.vbr != null && (this.vbr_enabled != 0 || this.vad_enabled != 0))
            {
                if (this.abr_enabled != 0)
                {
                    float num6 = 0f;
                    if (this.abr_drift2 * this.abr_drift > 0f)
                    {
                        num6 = -1E-05f * this.abr_drift / (1f + this.abr_count);
                        if (num6 > 0.05f)
                        {
                            num6 = 0.05f;
                        }
                        if (num6 < -0.05f)
                        {
                            num6 = -0.05f;
                        }
                    }
                    this.vbr_quality += num6;
                    if (this.vbr_quality > 10f)
                    {
                        this.vbr_quality = 10f;
                    }
                    if (this.vbr_quality < 0f)
                    {
                        this.vbr_quality = 0f;
                    }
                }
                this.relative_quality = this.vbr.Analysis(ins0, this.frameSize, num3, num4);
                if (this.vbr_enabled != 0)
                {
                    int   num7 = 0;
                    float num8 = 100f;
                    int   j;
                    for (j = 8; j > 0; j--)
                    {
                        int   num9 = (int)Math.Floor((double)this.vbr_quality);
                        float num10;
                        if (num9 == 10)
                        {
                            num10 = NSpeex.Vbr.nb_thresh[j][num9];
                        }
                        else
                        {
                            num10 = (this.vbr_quality - (float)num9) * NSpeex.Vbr.nb_thresh[j][num9 + 1] + ((float)(1 + num9) - this.vbr_quality) * NSpeex.Vbr.nb_thresh[j][num9];
                        }
                        if (this.relative_quality > num10 && this.relative_quality - num10 < num8)
                        {
                            num7 = j;
                            num8 = this.relative_quality - num10;
                        }
                    }
                    j = num7;
                    if (j == 0)
                    {
                        if (this.dtx_count == 0 || (double)num2 > 0.05 || this.dtx_enabled == 0 || this.dtx_count > 20)
                        {
                            j = 1;
                            this.dtx_count = 1;
                        }
                        else
                        {
                            j = 0;
                            this.dtx_count++;
                        }
                    }
                    else
                    {
                        this.dtx_count = 0;
                    }
                    this.Mode = j;
                    if (this.abr_enabled != 0)
                    {
                        int bitRate = this.BitRate;
                        this.abr_drift += (float)(bitRate - this.abr_enabled);
                        this.abr_drift2 = 0.95f * this.abr_drift2 + 0.05f * (float)(bitRate - this.abr_enabled);
                        this.abr_count += new float?((float)1.0).Value;
                    }
                }
                else
                {
                    int submodeID;
                    if (this.relative_quality < 2f)
                    {
                        if (this.dtx_count == 0 || (double)num2 > 0.05 || this.dtx_enabled == 0 || this.dtx_count > 20)
                        {
                            this.dtx_count = 1;
                            submodeID      = 1;
                        }
                        else
                        {
                            submodeID = 0;
                            this.dtx_count++;
                        }
                    }
                    else
                    {
                        this.dtx_count = 0;
                        submodeID      = this.submodeSelect;
                    }
                    this.submodeID = submodeID;
                }
            }
            else
            {
                this.relative_quality = -1f;
            }
            bits.Pack(0, 1);
            bits.Pack(this.submodeID, 4);
            if (this.submodes[this.submodeID] == null)
            {
                for (int i = 0; i < this.frameSize; i++)
                {
                    this.excBuf[this.excIdx + i] = (this.exc2Buf[this.exc2Idx + i] = (this.swBuf[this.swIdx + i] = 0f));
                }
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.mem_sw[i] = 0f;
                }
                this.first         = 1;
                this.bounded_pitch = 1;
                Filters.Iir_mem2(this.excBuf, this.excIdx, this.interp_qlpc, this.frmBuf, this.frmIdx, this.frameSize, this.lpcSize, this.mem_sp);
                ins0[0] = this.frmBuf[this.frmIdx] + this.preemph * this.pre_mem2;
                for (int i = 1; i < this.frameSize; i++)
                {
                    ins0[i] = this.frmBuf[this.frmIdx = i] + this.preemph * ins0[i - 1];
                }
                this.pre_mem2 = ins0[this.frameSize - 1];
                return(0);
            }
            if (this.first != 0)
            {
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.old_lsp[i] = this.lsp[i];
                }
            }
            this.submodes[this.submodeID].LsqQuant.Quant(this.lsp, this.qlsp, this.lpcSize, bits);
            if (this.submodes[this.submodeID].LbrPitch != -1)
            {
                bits.Pack(num3 - this.min_pitch, 7);
            }
            if (this.submodes[this.submodeID].ForcedPitchGain != 0)
            {
                int num11 = (int)Math.Floor(0.5 + (double)(15f * num4));
                if (num11 > 15)
                {
                    num11 = 15;
                }
                if (num11 < 0)
                {
                    num11 = 0;
                }
                bits.Pack(num11, 4);
                num4 = 0.066667f * (float)num11;
            }
            int num12 = (int)Math.Floor(0.5 + 3.5 * Math.Log((double)num5));

            if (num12 < 0)
            {
                num12 = 0;
            }
            if (num12 > 31)
            {
                num12 = 31;
            }
            num5 = (float)Math.Exp((double)num12 / 3.5);
            bits.Pack(num12, 5);
            if (this.first != 0)
            {
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.old_qlsp[i] = this.qlsp[i];
                }
            }
            float[] array3 = new float[this.subframeSize];
            float[] array4 = new float[this.subframeSize];
            float[] array5 = new float[this.subframeSize];
            float[] array6 = new float[this.lpcSize];
            float[] array7 = new float[this.frameSize];
            for (int i = 0; i < this.frameSize; i++)
            {
                array7[i] = this.frmBuf[this.frmIdx + i];
            }
            for (int k = 0; k < this.nbSubframes; k++)
            {
                int   num13 = this.subframeSize * k;
                int   num14 = this.frmIdx + num13;
                int   num15 = this.excIdx + num13;
                int   num16 = this.swIdx + num13;
                int   num17 = this.exc2Idx + num13;
                float num18 = (float)(1.0 + (double)k) / (float)this.nbSubframes;
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.interp_lsp[i] = (1f - num18) * this.old_lsp[i] + num18 * this.lsp[i];
                }
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.interp_qlsp[i] = (1f - num18) * this.old_qlsp[i] + num18 * this.qlsp[i];
                }
                Lsp.Enforce_margin(this.interp_lsp, this.lpcSize, 0.002f);
                Lsp.Enforce_margin(this.interp_qlsp, this.lpcSize, 0.002f);
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.interp_lsp[i] = (float)Math.Cos((double)this.interp_lsp[i]);
                }
                this.m_lsp.Lsp2lpc(this.interp_lsp, this.interp_lpc, this.lpcSize);
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.interp_qlsp[i] = (float)Math.Cos((double)this.interp_qlsp[i]);
                }
                this.m_lsp.Lsp2lpc(this.interp_qlsp, this.interp_qlpc, this.lpcSize);
                num18           = 1f;
                this.pi_gain[k] = 0f;
                for (int i = 0; i <= this.lpcSize; i++)
                {
                    this.pi_gain[k] += num18 * this.interp_qlpc[i];
                    num18            = -num18;
                }
                Filters.Bw_lpc(this.gamma1, this.interp_lpc, this.bw_lpc1, this.lpcSize);
                if (this.gamma2 >= 0f)
                {
                    Filters.Bw_lpc(this.gamma2, this.interp_lpc, this.bw_lpc2, this.lpcSize);
                }
                else
                {
                    this.bw_lpc2[0] = 1f;
                    this.bw_lpc2[1] = -this.preemph;
                    for (int i = 2; i <= this.lpcSize; i++)
                    {
                        this.bw_lpc2[i] = 0f;
                    }
                }
                for (int i = 0; i < this.subframeSize; i++)
                {
                    this.excBuf[num15 + i] = 0f;
                }
                this.excBuf[num15] = 1f;
                Filters.Syn_percep_zero(this.excBuf, num15, this.interp_qlpc, this.bw_lpc1, this.bw_lpc2, array5, this.subframeSize, this.lpcSize);
                for (int i = 0; i < this.subframeSize; i++)
                {
                    this.excBuf[num15 + i] = 0f;
                }
                for (int i = 0; i < this.subframeSize; i++)
                {
                    this.exc2Buf[num17 + i] = 0f;
                }
                for (int i = 0; i < this.lpcSize; i++)
                {
                    array6[i] = this.mem_sp[i];
                }
                Filters.Iir_mem2(this.excBuf, num15, this.interp_qlpc, this.excBuf, num15, this.subframeSize, this.lpcSize, array6);
                for (int i = 0; i < this.lpcSize; i++)
                {
                    array6[i] = this.mem_sw[i];
                }
                Filters.Filter_mem2(this.excBuf, num15, this.bw_lpc1, this.bw_lpc2, array3, 0, this.subframeSize, this.lpcSize, array6, 0);
                for (int i = 0; i < this.lpcSize; i++)
                {
                    array6[i] = this.mem_sw[i];
                }
                Filters.Filter_mem2(this.frmBuf, num14, this.bw_lpc1, this.bw_lpc2, this.swBuf, num16, this.subframeSize, this.lpcSize, array6, 0);
                for (int i = 0; i < this.subframeSize; i++)
                {
                    array4[i] = this.swBuf[num16 + i] - array3[i];
                }
                for (int i = 0; i < this.subframeSize; i++)
                {
                    this.excBuf[num15 + i] = (this.exc2Buf[num17 + i] = 0f);
                }
                int start;
                int num19;
                if (this.submodes[this.submodeID].LbrPitch != -1)
                {
                    int lbrPitch = this.submodes[this.submodeID].LbrPitch;
                    if (lbrPitch != 0)
                    {
                        if (num3 < this.min_pitch + lbrPitch - 1)
                        {
                            num3 = this.min_pitch + lbrPitch - 1;
                        }
                        if (num3 > this.max_pitch - lbrPitch)
                        {
                            num3 = this.max_pitch - lbrPitch;
                        }
                        start = num3 - lbrPitch + 1;
                        num19 = num3 + lbrPitch;
                    }
                    else
                    {
                        num19 = (start = num3);
                    }
                }
                else
                {
                    start = this.min_pitch;
                    num19 = this.max_pitch;
                }
                if (this.bounded_pitch != 0 && num19 > num13)
                {
                    num19 = num13;
                }
                int num20 = this.submodes[this.submodeID].Ltp.Quant(array4, this.swBuf, num16, this.interp_qlpc, this.bw_lpc1, this.bw_lpc2, this.excBuf, num15, start, num19, num4, this.lpcSize, this.subframeSize, bits, this.exc2Buf, num17, array5, this.complexity);
                this.pitch[k] = num20;
                Filters.Syn_percep_zero(this.excBuf, num15, this.interp_qlpc, this.bw_lpc1, this.bw_lpc2, array3, this.subframeSize, this.lpcSize);
                for (int i = 0; i < this.subframeSize; i++)
                {
                    array4[i] -= array3[i];
                }
                float num21 = 0f;
                int   num22 = k * this.subframeSize;
                for (int i = 0; i < this.subframeSize; i++)
                {
                    this.innov[num22 + i] = 0f;
                }
                Filters.Residue_percep_zero(array4, 0, this.interp_qlpc, this.bw_lpc1, this.bw_lpc2, this.buf2, this.subframeSize, this.lpcSize);
                for (int i = 0; i < this.subframeSize; i++)
                {
                    num21 += this.buf2[i] * this.buf2[i];
                }
                num21  = (float)Math.Sqrt((double)(0.1f + num21 / (float)this.subframeSize));
                num21 /= num5;
                if (this.submodes[this.submodeID].HaveSubframeGain != 0)
                {
                    num21 = (float)Math.Log((double)num21);
                    if (this.submodes[this.submodeID].HaveSubframeGain == 3)
                    {
                        int num23 = VQ.Index(num21, NbCodec.exc_gain_quant_scal3, 8);
                        bits.Pack(num23, 3);
                        num21 = NbCodec.exc_gain_quant_scal3[num23];
                    }
                    else
                    {
                        int num23 = VQ.Index(num21, NbCodec.exc_gain_quant_scal1, 2);
                        bits.Pack(num23, 1);
                        num21 = NbCodec.exc_gain_quant_scal1[num23];
                    }
                    num21 = (float)Math.Exp((double)num21);
                }
                else
                {
                    num21 = 1f;
                }
                num21 *= num5;
                float num24 = 1f / num21;
                for (int i = 0; i < this.subframeSize; i++)
                {
                    array4[i] *= num24;
                }
                this.submodes[this.submodeID].Innovation.Quantify(array4, this.interp_qlpc, this.bw_lpc1, this.bw_lpc2, this.lpcSize, this.subframeSize, this.innov, num22, array5, bits, this.complexity);
                for (int i = 0; i < this.subframeSize; i++)
                {
                    this.innov[num22 + i] *= num21;
                }
                for (int i = 0; i < this.subframeSize; i++)
                {
                    this.excBuf[num15 + i] += this.innov[num22 + i];
                }
                if (this.submodes[this.submodeID].DoubleCodebook != 0)
                {
                    float[] array8 = new float[this.subframeSize];
                    for (int i = 0; i < this.subframeSize; i++)
                    {
                        array4[i] *= 2.2f;
                    }
                    this.submodes[this.submodeID].Innovation.Quantify(array4, this.interp_qlpc, this.bw_lpc1, this.bw_lpc2, this.lpcSize, this.subframeSize, array8, 0, array5, bits, this.complexity);
                    for (int i = 0; i < this.subframeSize; i++)
                    {
                        array8[i] *= (float)((double)num21 * 0.45454545454545453);
                    }
                    for (int i = 0; i < this.subframeSize; i++)
                    {
                        this.excBuf[num15 + i] += array8[i];
                    }
                }
                for (int i = 0; i < this.subframeSize; i++)
                {
                    array4[i] *= num21;
                }
                for (int i = 0; i < this.lpcSize; i++)
                {
                    array6[i] = this.mem_sp[i];
                }
                Filters.Iir_mem2(this.excBuf, num15, this.interp_qlpc, this.frmBuf, num14, this.subframeSize, this.lpcSize, this.mem_sp);
                Filters.Filter_mem2(this.frmBuf, num14, this.bw_lpc1, this.bw_lpc2, this.swBuf, num16, this.subframeSize, this.lpcSize, this.mem_sw, 0);
                for (int i = 0; i < this.subframeSize; i++)
                {
                    this.exc2Buf[num17 + i] = this.excBuf[num15 + i];
                }
            }
            if (this.submodeID >= 1)
            {
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.old_lsp[i] = this.lsp[i];
                }
                for (int i = 0; i < this.lpcSize; i++)
                {
                    this.old_qlsp[i] = this.qlsp[i];
                }
            }
            if (this.submodeID == 1)
            {
                if (this.dtx_count != 0)
                {
                    bits.Pack(15, 4);
                }
                else
                {
                    bits.Pack(0, 4);
                }
            }
            this.first = 0;
            float num25 = 0f;
            float num26 = 0f;

            for (int i = 0; i < this.frameSize; i++)
            {
                num25 += this.frmBuf[this.frmIdx + i] * this.frmBuf[this.frmIdx + i];
                num26 += (this.frmBuf[this.frmIdx + i] - array7[i]) * (this.frmBuf[this.frmIdx + i] - array7[i]);
            }
            ins0[0] = this.frmBuf[this.frmIdx] + this.preemph * this.pre_mem2;
            for (int i = 1; i < this.frameSize; i++)
            {
                ins0[i] = this.frmBuf[this.frmIdx + i] + this.preemph * ins0[i - 1];
            }
            this.pre_mem2 = ins0[this.frameSize - 1];
            if (this.submodes[this.submodeID].Innovation is NoiseSearch || this.submodeID == 0)
            {
                this.bounded_pitch = 1;
            }
            else
            {
                this.bounded_pitch = 0;
            }
            return(1);
        }
예제 #5
0
        private float Pitch_gain_search_3tap(float[] target, float[] ak, float[] awk1, float[] awk2, float[] exc, int es, int pitch, int p, int nsf, Bits bits, float[] exc2, int e2s, float[] r, int[] cdbk_index)
        {
            float[]   array  = new float[3];
            float[][] array2 = this.CreateJaggedArray <float>(3, 3);
            int       num    = 1 << this.gain_bits;

            float[][] array3 = this.CreateJaggedArray <float>(3, nsf);
            this.e = this.CreateJaggedArray <float>(3, nsf);
            for (int i = 2; i >= 0; i--)
            {
                int num2 = pitch + 1 - i;
                for (int j = 0; j < nsf; j++)
                {
                    if (j - num2 < 0)
                    {
                        this.e[i][j] = exc2[e2s + j - num2];
                    }
                    else if (j - num2 - pitch < 0)
                    {
                        this.e[i][j] = exc2[e2s + j - num2 - pitch];
                    }
                    else
                    {
                        this.e[i][j] = 0f;
                    }
                }
                if (i == 2)
                {
                    Filters.Syn_percep_zero(this.e[i], 0, ak, awk1, awk2, array3[i], nsf, p);
                }
                else
                {
                    for (int j = 0; j < nsf - 1; j++)
                    {
                        array3[i][j + 1] = array3[i + 1][j];
                    }
                    array3[i][0] = 0f;
                    for (int j = 0; j < nsf; j++)
                    {
                        array3[i][j] += this.e[i][0] * r[j];
                    }
                }
            }
            for (int i = 0; i < 3; i++)
            {
                array[i] = Ltp.Inner_prod(array3[i], 0, target, 0, nsf);
            }
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    array2[i][j] = (array2[j][i] = Ltp.Inner_prod(array3[i], 0, array3[j], 0, nsf));
                }
            }
            float[] array4 = new float[9];
            int     num3   = 0;
            float   num4   = 0f;

            array4[0] = array[2];
            array4[1] = array[1];
            array4[2] = array[0];
            array4[3] = array2[1][2];
            array4[4] = array2[0][1];
            array4[5] = array2[0][2];
            array4[6] = array2[2][2];
            array4[7] = array2[1][1];
            array4[8] = array2[0][0];
            for (int i = 0; i < num; i++)
            {
                float num5 = 0f;
                int   num6 = 3 * i;
                float num7 = 0.015625f * (float)this.gain_cdbk[num6] + 0.5f;
                float num8 = 0.015625f * (float)this.gain_cdbk[num6 + 1] + 0.5f;
                float num9 = 0.015625f * (float)this.gain_cdbk[num6 + 2] + 0.5f;
                num5 += array4[0] * num7;
                num5 += array4[1] * num8;
                num5 += array4[2] * num9;
                num5 -= array4[3] * num7 * num8;
                num5 -= array4[4] * num9 * num8;
                num5 -= array4[5] * num9 * num7;
                num5 -= 0.5f * array4[6] * num7 * num7;
                num5 -= 0.5f * array4[7] * num8 * num8;
                num5 -= 0.5f * array4[8] * num9 * num9;
                if (num5 > num4 || i == 0)
                {
                    num4 = num5;
                    num3 = i;
                }
            }
            this.gain[0]  = 0.015625f * (float)this.gain_cdbk[num3 * 3] + 0.5f;
            this.gain[1]  = 0.015625f * (float)this.gain_cdbk[num3 * 3 + 1] + 0.5f;
            this.gain[2]  = 0.015625f * (float)this.gain_cdbk[num3 * 3 + 2] + 0.5f;
            cdbk_index[0] = num3;
            for (int i = 0; i < nsf; i++)
            {
                exc[es + i] = this.gain[0] * this.e[2][i] + this.gain[1] * this.e[1][i] + this.gain[2] * this.e[0][i];
            }
            float num10 = 0f;
            float num11 = 0f;

            for (int i = 0; i < nsf; i++)
            {
                num10 += target[i] * target[i];
            }
            for (int i = 0; i < nsf; i++)
            {
                num11 += (target[i] - this.gain[2] * array3[0][i] - this.gain[1] * array3[1][i] - this.gain[0] * array3[2][i]) * (target[i] - this.gain[2] * array3[0][i] - this.gain[1] * array3[1][i] - this.gain[0] * array3[2][i]);
            }
            return(num11);
        }
예제 #6
0
        protected internal static void Open_loop_nbest_pitch(float[] sw, int swIdx, int start, int end, int len, int[] pitch, float[] gain, int N)
        {
            float[] array  = new float[N];
            float[] array2 = new float[end - start + 1];
            float[] array3 = new float[end - start + 2];
            float[] array4 = new float[end - start + 1];
            for (int i = 0; i < N; i++)
            {
                array[i] = -1f;
                gain[i]  = 0f;
                pitch[i] = start;
            }
            array3[0] = Ltp.Inner_prod(sw, swIdx - start, sw, swIdx - start, len);
            float num = Ltp.Inner_prod(sw, swIdx, sw, swIdx, len);

            for (int i = start; i <= end; i++)
            {
                array3[i - start + 1] = array3[i - start] + sw[swIdx - i - 1] * sw[swIdx - i - 1] - sw[swIdx - i + len - 1] * sw[swIdx - i + len - 1];
                if (array3[i - start + 1] < 1f)
                {
                    array3[i - start + 1] = 1f;
                }
            }
            for (int i = start; i <= end; i++)
            {
                array2[i - start] = 0f;
                array4[i - start] = 0f;
            }
            for (int i = start; i <= end; i++)
            {
                array2[i - start] = Ltp.Inner_prod(sw, swIdx, sw, swIdx - i, len);
                array4[i - start] = array2[i - start] * array2[i - start] / (array3[i - start] + 1f);
            }
            for (int i = start; i <= end; i++)
            {
                if (array4[i - start] > array[N - 1])
                {
                    float num2 = array2[i - start] / (array3[i - start] + 10f);
                    float num3 = (float)Math.Sqrt((double)(num2 * array2[i - start] / (num + 10f)));
                    if (num3 > num2)
                    {
                        num3 = num2;
                    }
                    if (num3 < 0f)
                    {
                        num3 = 0f;
                    }
                    for (int j = 0; j < N; j++)
                    {
                        if (array4[i - start] > array[j])
                        {
                            for (int k = N - 1; k > j; k--)
                            {
                                array[k] = array[k - 1];
                                pitch[k] = pitch[k - 1];
                                gain[k]  = gain[k - 1];
                            }
                            array[j] = array4[i - start];
                            pitch[j] = i;
                            gain[j]  = num3;
                            break;
                        }
                    }
                }
            }
        }