]> code.delx.au - pulseaudio/blob - src/pulsecore/sconv-s16le.c
big resampler rework: support integer-only resampling, support speex resampler
[pulseaudio] / src / pulsecore / sconv-s16le.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <inttypes.h>
29
30 #include <liboil/liboilfuncs.h>
31
32 #include <pulsecore/sconv.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/log.h>
35
36 #include "endianmacros.h"
37
38 #include "sconv-s16le.h"
39
40 #ifndef INT16_FROM
41 #define INT16_FROM INT16_FROM_LE
42 #endif
43
44 #ifndef INT16_TO
45 #define INT16_TO INT16_TO_LE
46 #endif
47
48 #ifndef SWAP_WORDS
49 #ifdef WORDS_BIGENDIAN
50 #define SWAP_WORDS 1
51 #else
52 #define SWAP_WORDS 0
53 #endif
54 #endif
55
56 void pa_sconv_s16le_to_float32ne(unsigned n, const int16_t *a, float *b) {
57 pa_assert(a);
58 pa_assert(b);
59
60 #if SWAP_WORDS == 1
61
62 for (; n > 0; n--) {
63 int16_t s = *(a++);
64 *(b++) = ((float) INT16_FROM(s))/0x7FFF;
65 }
66
67 #else
68 {
69 static const double add = 0, factor = 1.0/0x7FFF;
70 oil_scaleconv_f32_s16(b, a, n, &add, &factor);
71 }
72 #endif
73 }
74
75 void pa_sconv_s16le_from_float32ne(unsigned n, const float *a, int16_t *b) {
76 pa_assert(a);
77 pa_assert(b);
78
79 #if SWAP_WORDS == 1
80
81 for (; n > 0; n--) {
82 int16_t s;
83 float v = *(a++);
84
85 if (v > 1)
86 v = 1;
87
88 if (v < -1)
89 v = -1;
90
91 s = (int16_t) (v * 0x7FFF);
92 *(b++) = INT16_TO(s);
93 }
94
95 #else
96 {
97 static const double add = 0, factor = 0x7FFF;
98 oil_scaleconv_s16_f32(b, a, n, &add, &factor);
99 }
100 #endif
101 }
102
103 void pa_sconv_s16le_to_float32re(unsigned n, const int16_t *a, float *b) {
104 pa_assert(a);
105 pa_assert(b);
106
107 #if SWAP_WORDS == 1
108
109 for (; n > 0; n--) {
110 int16_t s = *(a++);
111 float k = ((float) INT16_FROM(s))/0x7FFF;
112 uint32_t *j = (uint32_t*) &k;
113 *j = UINT32_SWAP(*j);
114 *(b++) = k;
115 }
116
117 #endif
118 }
119
120 void pa_sconv_s16le_from_float32re(unsigned n, const float *a, int16_t *b) {
121 pa_assert(a);
122 pa_assert(b);
123
124 #if SWAP_WORDS == 1
125
126 for (; n > 0; n--) {
127 int16_t s;
128 float v = *(a++);
129 uint32_t *j = (uint32_t*) &v;
130 *j = UINT32_SWAP(*j);
131
132 if (v > 1)
133 v = 1;
134
135 if (v < -1)
136 v = -1;
137
138 s = (int16_t) (v * 0x7FFF);
139 *(b++) = INT16_TO(s);
140 }
141
142 #endif
143 }