private static int downSample(ResampleContext rCtx) {
        int rps_backup = rCtx.rps;
        int s2p_backup = rCtx.sp;
        int i, j, k, ch, t1, bp, nsmplwrt2 = 0;
        double re, im, tmp;
        int bp2;
        int s2o;

        for (ch = 0; ch < rCtx.nch; ch++) {
            rCtx.rps = rps_backup;

            Arrays.fill(rCtx.buf1[ch],0,rCtx.rps,0);

            for (i = rCtx.rps, j = 0; i < rCtx.nb2; i += rCtx.osf, j++) {
//				assert(j < ((rCtx.nb2-rCtx.rps-1)/rCtx.osf+1));
                rCtx.buf1[ch][i] = rCtx.inbuf[j * rCtx.nch + ch];
                for (k = i + 1; k < i + rCtx.osf; k++) {
                    rCtx.buf1[ch][k] = 0;
                }
            }

//			assert(j == ((rCtx.nb2-rCtx.rps-1)/rCtx.osf+1));
            Arrays.fill(rCtx.buf1[ch],rCtx.nb2,rCtx.nb,0);

            rCtx.rps = i - rCtx.nb2;
            rCtx.fft.realDFT(rCtx.buf1[ch]);
            rCtx.buf1[ch][0] = rCtx.stageA[0] * rCtx.buf1[ch][0];
            rCtx.buf1[ch][1] = rCtx.stageA[1] * rCtx.buf1[ch][1];
            for (i = 1; i < rCtx.nb2; i++) {
                re = rCtx.stageA[i * 2] * rCtx.buf1[ch][i * 2] - rCtx.stageA[i * 2 + 1] * rCtx.buf1[ch][i * 2 + 1];
                im = rCtx.stageA[i * 2 + 1] * rCtx.buf1[ch][i * 2] + rCtx.stageA[i * 2] * rCtx.buf1[ch][i * 2 + 1];

                rCtx.buf1[ch][i * 2] = re;
                rCtx.buf1[ch][i * 2 + 1] = im;
            }

            rCtx.fft.realInverseDFT(rCtx.buf1[ch]);
            for (i = 0; i < rCtx.nb2; i++) {
                rCtx.buf2[ch][rCtx.nx + 1 + i] += rCtx.buf1[ch][i];
            }

            t1 = rCtx.rp / (rCtx.fs2 / rCtx.fs1);
            if (rCtx.rp % (rCtx.fs2 / rCtx.fs1) != 0) {
                t1++;
            }

            bp = t1; // bp = &(buf2[ch][t1]);
            rCtx.sp = s2p_backup;

            for (j = 0; bp < rCtx.nb2 + 1; j++) {
                tmp = 0;
                bp2 = bp;
                s2o = rCtx.fOrder[rCtx.sp];
                bp += rCtx.fInc[rCtx.sp];
                rCtx.sp++;

                if (rCtx.sp == rCtx.ny) {
                    rCtx.sp = 0;
                }

//				assert(bp2*(rCtx.fs2/rCtx.fs1)-(rCtx.rp+j*(rCtx.fs2/rCtx.dfrq)) == s2o);
                for (i = 0; i < rCtx.nx; i++) {
                    tmp += rCtx.stageB[s2o][i] * rCtx.buf2[ch][bp2++];
                }

                rCtx.outbuf[j * rCtx.nch + ch] = tmp;
            }

            nsmplwrt2 = j;
        }
        return nsmplwrt2;
    }