]> code.delx.au - pulseaudio/blob - src/pulsecore/sconv.c
d15cec84765cf342b319c491df6612b2900bf2db
[pulseaudio] / src / pulsecore / sconv.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <assert.h>
32
33 #include <liboil/liboilfuncs.h>
34 #include <liboil/liboil.h>
35
36 #include <pulsecore/g711.h>
37
38 #include "endianmacros.h"
39 #include "sconv-s16le.h"
40 #include "sconv-s16be.h"
41
42 #include "sconv.h"
43
44 static void u8_to_float32ne(unsigned n, const void *a, float *b) {
45 const uint8_t *ca = a;
46 static const double add = -128.0/127.0, factor = 1.0/127.0;
47
48 assert(a);
49 assert(b);
50
51 oil_scaleconv_f32_u8(b, ca, n, &add, &factor);
52 }
53
54 static void u8_from_float32ne(unsigned n, const float *a, void *b) {
55 uint8_t *cb = b;
56 static const double add = 128.0, factor = 127.0;
57
58 assert(a);
59 assert(b);
60
61 oil_scaleconv_u8_f32(cb, a, n, &add, &factor);
62 }
63
64 static void float32ne_to_float32ne(unsigned n, const void *a, float *b) {
65 assert(a);
66 assert(b);
67
68 oil_memcpy(b, a, sizeof(float) * n);
69 }
70
71 static void float32ne_from_float32ne(unsigned n, const float *a, void *b) {
72 assert(a);
73 assert(b);
74
75 oil_memcpy(b, a, sizeof(float) * n);
76 }
77
78 static void float32re_to_float32ne(unsigned n, const void *a, float *b) {
79 assert(a);
80 assert(b);
81
82 while (n-- > 0)
83 ((uint32_t *)b)[n] = UINT32_SWAP (((uint32_t *)a)[n]);
84 }
85
86 static void float32re_from_float32ne(unsigned n, const float *a, void *b) {
87 assert(a);
88 assert(b);
89
90 while (n-- > 0)
91 ((uint32_t *)b)[n] = UINT32_SWAP (((uint32_t *)a)[n]);
92 }
93
94 static void ulaw_to_float32ne(unsigned n, const void *a, float *b) {
95 const uint8_t *ca = a;
96
97 assert(a);
98 assert(b);
99
100 for (; n > 0; n--)
101 *(b++) = st_ulaw2linear16(*(ca++)) * 1.0F / 0x7FFF;
102 }
103
104 static void ulaw_from_float32ne(unsigned n, const float *a, void *b) {
105 uint8_t *cb = b;
106
107 assert(a);
108 assert(b);
109
110 for (; n > 0; n--) {
111 float v = *(a++);
112
113 if (v > 1)
114 v = 1;
115
116 if (v < -1)
117 v = -1;
118
119 *(cb++) = st_14linear2ulaw((int16_t) (v * 0x1FFF));
120 }
121 }
122
123 static void alaw_to_float32ne(unsigned n, const void *a, float *b) {
124 const uint8_t *ca = a;
125
126 assert(a);
127 assert(b);
128
129 for (; n > 0; n--)
130 *(b++) = st_alaw2linear16(*(ca++)) * 1.0F / 0x7FFF;
131 }
132
133 static void alaw_from_float32ne(unsigned n, const float *a, void *b) {
134 uint8_t *cb = b;
135
136 assert(a);
137 assert(b);
138
139 for (; n > 0; n--) {
140 float v = *(a++);
141
142 if (v > 1)
143 v = 1;
144
145 if (v < -1)
146 v = -1;
147
148 *(cb++) = st_13linear2alaw((int16_t) (v * 0xFFF));
149 }
150 }
151
152 pa_convert_to_float32ne_func_t pa_get_convert_to_float32ne_function(pa_sample_format_t f) {
153 switch(f) {
154 case PA_SAMPLE_U8:
155 return u8_to_float32ne;
156 case PA_SAMPLE_S16LE:
157 return pa_sconv_s16le_to_float32ne;
158 case PA_SAMPLE_S16BE:
159 return pa_sconv_s16be_to_float32ne;
160 case PA_SAMPLE_FLOAT32NE:
161 return float32ne_to_float32ne;
162 case PA_SAMPLE_FLOAT32RE:
163 return float32re_to_float32ne;
164 case PA_SAMPLE_ALAW:
165 return alaw_to_float32ne;
166 case PA_SAMPLE_ULAW:
167 return ulaw_to_float32ne;
168 default:
169 return NULL;
170 }
171 }
172
173 pa_convert_from_float32ne_func_t pa_get_convert_from_float32ne_function(pa_sample_format_t f) {
174 switch(f) {
175 case PA_SAMPLE_U8:
176 return u8_from_float32ne;
177 case PA_SAMPLE_S16LE:
178 return pa_sconv_s16le_from_float32ne;
179 case PA_SAMPLE_S16BE:
180 return pa_sconv_s16be_from_float32ne;
181 case PA_SAMPLE_FLOAT32NE:
182 return float32ne_from_float32ne;
183 case PA_SAMPLE_FLOAT32RE:
184 return float32re_from_float32ne;
185 case PA_SAMPLE_ALAW:
186 return alaw_from_float32ne;
187 case PA_SAMPLE_ULAW:
188 return ulaw_from_float32ne;
189 default:
190 return NULL;
191 }
192 }