]> code.delx.au - pulseaudio/blob - src/pulsecore/sconv-s16le.c
merge 'lennart' branch back into trunk.
[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 PA_INT16_FROM_LE
42 #endif
43
44 #ifndef INT16_TO
45 #define INT16_TO PA_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 v = CLAMP(v, -1, 1);
86 s = (int16_t) (v * 0x7FFF);
87 *(b++) = INT16_TO(s);
88 }
89
90 #else
91 {
92 static const double add = 0, factor = 0x7FFF;
93 oil_scaleconv_s16_f32(b, a, n, &add, &factor);
94 }
95 #endif
96 }
97
98 void pa_sconv_s16le_to_float32re(unsigned n, const int16_t *a, float *b) {
99 pa_assert(a);
100 pa_assert(b);
101
102 for (; n > 0; n--) {
103 int16_t s = *(a++);
104 float k = ((float) INT16_FROM(s))/0x7FFF;
105 uint32_t *j = (uint32_t*) &k;
106 *j = PA_UINT32_SWAP(*j);
107 *(b++) = k;
108 }
109 }
110
111 void pa_sconv_s16le_from_float32re(unsigned n, const float *a, int16_t *b) {
112 pa_assert(a);
113 pa_assert(b);
114
115 for (; n > 0; n--) {
116 int16_t s;
117 float v = *(a++);
118 uint32_t *j = (uint32_t*) &v;
119 *j = PA_UINT32_SWAP(*j);
120 v = CLAMP(v, -1, 1);
121 s = (int16_t) (v * 0x7FFF);
122 *(b++) = INT16_TO(s);
123 }
124 }