]> code.delx.au - pulseaudio/blob - src/sconv.c
rename hashset to hashmap
[pulseaudio] / src / sconv.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "endianmacros.h"
5 #include "sconv.h"
6
7 #include "sconv-s16le.h"
8 #include "sconv-s16be.h"
9
10 static void u8_to_float32(unsigned n, const void *a, unsigned an, float *b) {
11 unsigned i;
12 const uint8_t *ca = a;
13 assert(n && a && an && b);
14
15 for (; n > 0; n--) {
16 float sum = 0;
17
18 for (i = 0; i < an; i++) {
19 uint8_t v = *(ca++);
20 sum += (((float) v)-127)/127;
21 }
22
23 if (sum > 1)
24 sum = 1;
25 if (sum < -1)
26 sum = -1;
27
28 *(b++) = sum;
29 }
30 }
31
32 static void u8_from_float32(unsigned n, const float *a, void *b, unsigned bn) {
33 unsigned i;
34 uint8_t *cb = b;
35
36 assert(n && a && b && bn);
37 for (; n > 0; n--) {
38 float v = *(a++);
39 uint8_t u;
40
41 if (v > 1)
42 v = 1;
43
44 if (v < -1)
45 v = -1;
46
47 u = (uint8_t) (v*127+127);
48
49 for (i = 0; i < bn; i++)
50 *(cb++) = u;
51 }
52 }
53
54 static void float32_to_float32(unsigned n, const void *a, unsigned an, float *b) {
55 unsigned i;
56 const float *ca = a;
57 assert(n && a && an && b);
58 for (; n > 0; n--) {
59 float sum = 0;
60
61 for (i = 0; i < an; i++)
62 sum += *(ca++);
63
64 if (sum > 1)
65 sum = 1;
66 if (sum < -1)
67 sum = -1;
68
69 *(b++) = sum;
70 }
71 }
72
73 static void float32_from_float32(unsigned n, const float *a, void *b, unsigned bn) {
74 unsigned i;
75 float *cb = b;
76 assert(n && a && b && bn);
77 for (; n > 0; n--) {
78 float v = *(a++);
79 for (i = 0; i < bn; i++)
80 *(cb++) = v;
81 }
82 }
83
84 pa_convert_to_float32_func_t pa_get_convert_to_float32_function(enum pa_sample_format f) {
85 switch(f) {
86 case PA_SAMPLE_U8:
87 return u8_to_float32;
88 case PA_SAMPLE_S16LE:
89 return pa_sconv_s16le_to_float32;
90 case PA_SAMPLE_S16BE:
91 return pa_sconv_s16be_to_float32;
92 case PA_SAMPLE_FLOAT32:
93 return float32_to_float32;
94 default:
95 return NULL;
96 }
97 }
98
99 pa_convert_from_float32_func_t pa_get_convert_from_float32_function(enum pa_sample_format f) {
100 switch(f) {
101 case PA_SAMPLE_U8:
102 return u8_from_float32;
103 case PA_SAMPLE_S16LE:
104 return pa_sconv_s16le_from_float32;
105 case PA_SAMPLE_S16BE:
106 return pa_sconv_s16be_from_float32;
107 case PA_SAMPLE_FLOAT32:
108 return float32_from_float32;
109 default:
110 return NULL;
111 }
112 }