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