rename configuration file
[profile/ivi/pulseaudio-panda.git] / src / 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 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 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 #include "endianmacros.h"
30 #include "sconv.h"
31
32 #include "sconv-s16le.h"
33 #include "sconv-s16be.h"
34
35 static void u8_to_float32(unsigned n, const void *a, unsigned an, float *b) {
36     unsigned i;
37     const uint8_t *ca = a;
38     assert(n && a && an && b);
39
40     for (; n > 0; n--) {
41         float sum = 0;
42
43         for (i = 0; i < an; i++) {
44             uint8_t v = *(ca++);
45             sum += (((float) v)-127)/127;
46         }
47
48         if (sum > 1)
49             sum = 1;
50         if (sum < -1)
51             sum = -1;
52
53         *(b++) = sum;
54     }
55 }    
56
57 static void u8_from_float32(unsigned n, const float *a, void *b, unsigned bn) {
58     unsigned i;
59     uint8_t *cb = b;
60
61     assert(n && a && b && bn);
62     for (; n > 0; n--) {
63         float v = *(a++);
64         uint8_t u;
65
66         if (v > 1)
67             v = 1;
68
69         if (v < -1)
70             v = -1;
71
72         u = (uint8_t) (v*127+127);
73         
74         for (i = 0; i < bn; i++)
75             *(cb++) = u;
76     }
77 }
78
79 static void float32_to_float32(unsigned n, const void *a, unsigned an, float *b) {
80     unsigned i;
81     const float *ca = a;
82     assert(n && a && an && b);
83     for (; n > 0; n--) {
84         float sum = 0;
85
86         for (i = 0; i < an; i++)
87             sum += *(ca++);
88
89         if (sum > 1)
90             sum = 1;
91         if (sum < -1)
92             sum = -1;
93
94         *(b++) = sum;
95     }
96 }
97
98 static void float32_from_float32(unsigned n, const float *a, void *b, unsigned bn) {
99     unsigned i;
100     float *cb = b;
101     assert(n && a && b && bn);
102     for (; n > 0; n--) {
103         float v = *(a++);
104         for (i = 0; i < bn; i++)
105             *(cb++) = v;
106     }
107 }
108
109 pa_convert_to_float32_func_t pa_get_convert_to_float32_function(enum pa_sample_format f) {
110     switch(f) {
111         case PA_SAMPLE_U8:
112             return u8_to_float32;
113         case PA_SAMPLE_S16LE:
114             return pa_sconv_s16le_to_float32;
115         case PA_SAMPLE_S16BE:
116             return pa_sconv_s16be_to_float32;
117         case PA_SAMPLE_FLOAT32:
118             return float32_to_float32;
119         default:
120             return NULL;
121     }
122 }
123
124 pa_convert_from_float32_func_t pa_get_convert_from_float32_function(enum pa_sample_format f) {
125     switch(f) {
126         case PA_SAMPLE_U8:
127             return u8_from_float32;
128         case PA_SAMPLE_S16LE:
129             return pa_sconv_s16le_from_float32;
130         case PA_SAMPLE_S16BE:
131             return pa_sconv_s16be_from_float32;
132         case PA_SAMPLE_FLOAT32:
133             return float32_from_float32;
134         default:
135             return NULL;
136     }
137 }