Line 0
Link Here
|
|
|
1 |
From 5ffce9e91b2fde70ba532ea215e3e9e7eed3d41a Mon Sep 17 00:00:00 2001 |
2 |
From: Alexandre Ratchov <alex@caoua.org> |
3 |
Date: Thu, 2 Apr 2015 13:09:22 +1300 |
4 |
Subject: [PATCH] sndio: improve and clamp float->s16 conversion. |
5 |
|
6 |
--- |
7 |
src/cubeb_sndio.c | 14 +++++++++++--- |
8 |
1 file changed, 11 insertions(+), 3 deletions(-) |
9 |
|
10 |
diff --git a/src/cubeb_sndio.c b/src/cubeb_sndio.c |
11 |
index 01f96346..e6d531a4 100644 |
12 |
--- media/libcubeb/src/cubeb_sndio.c.orig |
13 |
+++ media/libcubeb/src/cubeb_sndio.c |
14 |
@@ -4,6 +4,7 @@ |
15 |
* This program is made available under an ISC-style license. See the |
16 |
* accompanying file LICENSE for details. |
17 |
*/ |
18 |
+#include <math.h> |
19 |
#include <poll.h> |
20 |
#include <pthread.h> |
21 |
#include <sndio.h> |
22 |
@@ -49,9 +50,16 @@ float_to_s16(void *ptr, long nsamp) |
23 |
{ |
24 |
int16_t *dst = ptr; |
25 |
float *src = ptr; |
26 |
- |
27 |
- while (nsamp-- > 0) |
28 |
- *(dst++) = *(src++) * 32767; |
29 |
+ int s; |
30 |
+ |
31 |
+ while (nsamp-- > 0) { |
32 |
+ s = lrintf(*(src++) * 32768); |
33 |
+ if (s < -32768) |
34 |
+ s = -32768; |
35 |
+ else if (s > 32767) |
36 |
+ s = 32767; |
37 |
+ *(dst++) = s; |
38 |
+ } |
39 |
} |
40 |
|
41 |
static void |