am 67c7a4ae: Merge "Fix off-by-two and other bugs in the visualization code. b/3137511" into gingerbread

This commit is contained in:
Marco Nelissen
2010-10-27 11:08:14 -07:00
committed by Android Git Automerger

View File

@ -243,19 +243,22 @@ extern "C" int Visualizer_process(
// derive capture scaling factor from peak value in current buffer // derive capture scaling factor from peak value in current buffer
// this gives more interesting captures for display. // this gives more interesting captures for display.
int32_t shift = 32; int32_t shift = 32;
for (size_t i = 0; i < inBuffer->frameCount; i++) { int len = inBuffer->frameCount * 2;
for (size_t i = 0; i < len; i++) {
int32_t smp = inBuffer->s16[i]; int32_t smp = inBuffer->s16[i];
if (smp < 0) smp = -smp; if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
int32_t clz = __builtin_clz(smp); int32_t clz = __builtin_clz(smp);
if (shift > clz) shift = clz; if (shift > clz) shift = clz;
} }
// never scale by less than 8 to avoid returning unaltered PCM signal. // A maximum amplitude signal will have 17 leading zeros, which we want to
// add one to combine the division by 2 needed after summing left and right channels below // translate to a shift of 8 (for converting 16 bit to 8 bit)
if (20 > shift) { shift = 25 - shift;
shift = (31 - 8 + 1) - shift; // Never scale by less than 8 to avoid returning unaltered PCM signal.
} else { if (shift < 3) {
shift = (3 + 1); shift = 3;
} }
// add one to combine the division by 2 needed after summing left and right channels below
shift++;
uint32_t captIdx; uint32_t captIdx;
uint32_t inIdx; uint32_t inIdx;
@ -264,7 +267,7 @@ extern "C" int Visualizer_process(
inIdx < inBuffer->frameCount && captIdx < pContext->mCaptureSize; inIdx < inBuffer->frameCount && captIdx < pContext->mCaptureSize;
inIdx++, captIdx++) { inIdx++, captIdx++) {
int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1]; int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1];
smp = (smp + (1 << (shift - 1))) >> shift; smp = smp >> shift;
buf[captIdx] = ((uint8_t)smp)^0x80; buf[captIdx] = ((uint8_t)smp)^0x80;
} }
pContext->mCaptureIdx = captIdx; pContext->mCaptureIdx = captIdx;