Question

I've taken over maintaining an android application that records an audio source using AudioRecord in 44100, 16-bit mono. I need to downsample it to 8000. The current app has a downsample algorithm in it that I'm not sure is working correctly. When another app (which is a black box) receives it, the audio plays but with a loud buzzing sound in the background and slower than expected audio. Other audio in the proper format piped into the receiving app is received fine with no issues.

The algorithm that I have is listed here (I suspect it was originally decompiled code from somewhere based on the variable names).

private void downSample() {
    int v5 = 0;
    int v4 = 0;
    int v2;
    for (v2 = 0; v2 < 0xA0; ++v2) {
        v5 += 0xD755;
        int v6 = v5 / 0x2710;
        v5 -= v6 * 0x2710;
        double v0 = 0;
        int v3 = v4;
        v4 += v6;
        while (v3 < v4) {
            v0 += ((double) ((((float) this.readBuffer[v3])) * this.volume));
            ++v3;
        }

        v0 /= ((double) v6);
        if (32767 < v0) {
            v0 = 32767;
        }
        else if (-32768 > v0) {
            v0 = -32768;
        }

        this.downSampledBuffer[v2] = ((short) (((int) v0)));
    }
}

readBuffer is a short[] that is populated by the recording source. downSampledBuffer is also a short[]. Any thoughts on what is going wrong?

Was it helpful?

Solution

The "buzzing sound" and "slower than expected audio" is produced outside this algorithm.

The algorithm processes exactly 882 input samples and produces 160 output samples. So, prior to each call of downSample() you have to fill this.readBuffer with exactly 882 new short values, and after returning from downSample() you have to concatenate the resulting 160 short values to the already processed samples.

The integrated low-pass "filter" is just a simple averaging over 5 or 6 input samples, so you can't get rid of all aliasing, but on the other hand it is not the slowest solution to the downsampling problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top