2

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?

mjsalinger
  • 660
  • 6
  • 17
  • 1
    That's a particularly grotty and unmaintainable bit of code. What it certainly doesn't appear to be is [resampling by decimation](http://www.dspguru.com/dsp/faqs/multirate/resampling) which is the orthodox way of achieving this conversion. If there is a LPF in there, it's a fairly crappy one. I very much doubt the results sound nice. There almost certainly has to be library support in Android for this rather than rolling your own. – marko Oct 26 '13 at 14:52
  • Android has library support from Honeycomb onwards, but unfortunately I need to Gingerbread for this app. I'm going to work on a resampling by decimation algorithm to replace the one that's there now. – mjsalinger Oct 27 '13 at 10:30
  • @mjsalinger can you write here the name of the library you was taliking about? Thanks! – albertopasqualetto Jun 06 '23 at 15:51

1 Answers1

1

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.

Hartmut Pfitzinger
  • 2,304
  • 3
  • 28
  • 48