Fix float to i16 conversion in primitives.c
Updated memcpy_to_i16_from_float():
Made bit-accurate invertible with power of 2 scaling.
Also removed discontinuity around 0 (round versus truncate).
Added clamp16FromFloat() to clamp float audio range of
[-1.0, 1.0) to int16_t.
Change-Id: I45b23f32ae46a4d7f86c8e5482ee43538298ce89
Signed-off-by: Andy Hung <hunga@google.com>
diff --git a/audio_utils/primitives.c b/audio_utils/primitives.c
index 90ad75a..e2d4abc 100644
--- a/audio_utils/primitives.c
+++ b/audio_utils/primitives.c
@@ -56,17 +56,7 @@
void memcpy_to_i16_from_float(int16_t *dst, const float *src, size_t count)
{
while (count--) {
- float f = *src++;
- int16_t i;
- if (f > 1.0) {
- i = 32767;
- } else if (f < -1.0) {
- i = -32768;
- } else {
- // does not specifically handle NaN
- i = f * 32767.0;
- }
- *dst++ = i;
+ *dst++ = clamp16FromFloat(*src++);
}
}