AudioResampler: Improve resampling for voice quality devices

Enabled for devices with sampling rates 16000 or less.

Test: instrumented log, resampler_tests
Bug: 123920996
Change-Id: Icb2a410b09eccb97188100fd78217123444b2eff
diff --git a/media/libaudioprocessing/AudioResamplerDyn.cpp b/media/libaudioprocessing/AudioResamplerDyn.cpp
index 52936cc..0a389bb 100644
--- a/media/libaudioprocessing/AudioResamplerDyn.cpp
+++ b/media/libaudioprocessing/AudioResamplerDyn.cpp
@@ -383,8 +383,16 @@
                     ? 0.5 : 0.5 * mSampleRate / mInSampleRate;
             fcr *= mPropertyCutoffPercent / 100.;
         } else {
+            // Voice quality devices have lower sampling rates
+            // (and may be a consequence of downstream AMR-WB / G.722 codecs).
+            // For these devices, we ensure a wider resampler passband
+            // at the expense of aliasing noise (stopband attenuation
+            // and stopband frequency).
+            //
+            constexpr uint32_t kVoiceDeviceSampleRate = 16000;
+
             if (mFilterQuality == DYN_HIGH_QUALITY) {
-                // 32b coefficients, 64 length
+                // float or 32b coefficients
                 useS32 = true;
                 stopBandAtten = 98.;
                 if (inSampleRate >= mSampleRate * 4) {
@@ -394,8 +402,18 @@
                 } else {
                     halfLength = 32;
                 }
+
+                if (mSampleRate <= kVoiceDeviceSampleRate) {
+                    if (inSampleRate >= mSampleRate * 2) {
+                        halfLength += 16;
+                    } else {
+                        halfLength += 8;
+                    }
+                    stopBandAtten = 84.;
+                    tbwCheat = 1.05;
+                }
             } else if (mFilterQuality == DYN_LOW_QUALITY) {
-                // 16b coefficients, 16-32 length
+                // float or 16b coefficients
                 useS32 = false;
                 stopBandAtten = 80.;
                 if (inSampleRate >= mSampleRate * 4) {
@@ -405,13 +423,18 @@
                 } else {
                     halfLength = 8;
                 }
-                if (inSampleRate <= mSampleRate) {
+                if (mSampleRate <= kVoiceDeviceSampleRate) {
+                    if (inSampleRate >= mSampleRate * 2) {
+                        halfLength += 8;
+                    }
+                    tbwCheat = 1.05;
+                } else if (inSampleRate <= mSampleRate) {
                     tbwCheat = 1.05;
                 } else {
                     tbwCheat = 1.03;
                 }
             } else { // DYN_MED_QUALITY
-                // 16b coefficients, 32-64 length
+                // float or 16b coefficients
                 // note: > 64 length filters with 16b coefs can have quantization noise problems
                 useS32 = false;
                 stopBandAtten = 84.;
@@ -422,7 +445,15 @@
                 } else {
                     halfLength = 16;
                 }
-                if (inSampleRate <= mSampleRate) {
+
+                if (mSampleRate <= kVoiceDeviceSampleRate) {
+                    if (inSampleRate >= mSampleRate * 2) {
+                        halfLength += 16;
+                    } else {
+                        halfLength += 8;
+                    }
+                    tbwCheat = 1.05;
+                } else if (inSampleRate <= mSampleRate) {
                     tbwCheat = 1.03;
                 } else {
                     tbwCheat = 1.01;
@@ -430,6 +461,19 @@
             }
         }
 
+        if (fcr > 0.) {
+            ALOGV("%s: mFilterQuality:%d inSampleRate:%d mSampleRate:%d halfLength:%d "
+                    "stopBandAtten:%lf fcr:%lf",
+                    __func__, mFilterQuality, inSampleRate, mSampleRate, halfLength,
+                    stopBandAtten, fcr);
+        } else {
+            ALOGV("%s: mFilterQuality:%d inSampleRate:%d mSampleRate:%d halfLength:%d "
+                    "stopBandAtten:%lf tbwCheat:%lf",
+                    __func__, mFilterQuality, inSampleRate, mSampleRate, halfLength,
+                    stopBandAtten, tbwCheat);
+        }
+
+
         // determine the number of polyphases in the filterbank.
         // for 16b, it is desirable to have 2^(16/2) = 256 phases.
         // https://ccrma.stanford.edu/~jos/resample/Relation_Interpolation_Error_Quantization.html