Remove unnecessary #includes
[platform/upstream/pulseaudio.git] / src / tests / mix-test.c
1 /***
2   This file is part of PulseAudio.
3
4   PulseAudio is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as published
6   by the Free Software Foundation; either version 2.1 of the License,
7   or (at your option) any later version.
8
9   PulseAudio is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with PulseAudio; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25
26 #include <pulse/sample.h>
27 #include <pulse/volume.h>
28
29 #include <pulsecore/macro.h>
30 #include <pulsecore/endianmacros.h>
31 #include <pulsecore/memblock.h>
32 #include <pulsecore/sample-util.h>
33
34 static void dump_block(const pa_sample_spec *ss, const pa_memchunk *chunk) {
35     void *d;
36     unsigned i;
37
38     d = pa_memblock_acquire(chunk->memblock);
39
40     switch (ss->format) {
41
42         case PA_SAMPLE_U8:
43         case PA_SAMPLE_ULAW:
44         case PA_SAMPLE_ALAW: {
45             uint8_t *u = d;
46
47             for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
48                 printf("0x%02x ", *(u++));
49
50             break;
51         }
52
53         case PA_SAMPLE_S16NE:
54         case PA_SAMPLE_S16RE: {
55             uint16_t *u = d;
56
57             for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
58                 printf("0x%04x ", *(u++));
59
60             break;
61         }
62
63         case PA_SAMPLE_S24_32NE:
64         case PA_SAMPLE_S24_32RE:
65         case PA_SAMPLE_S32NE:
66         case PA_SAMPLE_S32RE: {
67             uint32_t *u = d;
68
69             for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
70                 printf("0x%08x ", *(u++));
71
72             break;
73         }
74
75         case PA_SAMPLE_S24NE:
76         case PA_SAMPLE_S24RE: {
77             uint8_t *u = d;
78
79             for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
80                 printf("0x%02x%02x%02xx ", *u, *(u+1), *(u+2));
81                 u += 3;
82             }
83
84             break;
85         }
86
87         case PA_SAMPLE_FLOAT32NE:
88         case PA_SAMPLE_FLOAT32RE: {
89             float *u = d;
90
91             for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
92                 printf("%1.5f ",  ss->format == PA_SAMPLE_FLOAT32NE ? *u : PA_FLOAT32_SWAP(*u));
93                 u++;
94             }
95
96             break;
97         }
98
99         default:
100             pa_assert_not_reached();
101     }
102
103     printf("\n");
104
105     pa_memblock_release(chunk->memblock);
106 }
107
108 static pa_memblock* generate_block(pa_mempool *pool, const pa_sample_spec *ss) {
109     pa_memblock *r;
110     void *d;
111     unsigned i;
112
113     pa_assert_se(r = pa_memblock_new(pool, pa_frame_size(ss) * 10));
114     d = pa_memblock_acquire(r);
115
116     switch (ss->format) {
117
118         case PA_SAMPLE_U8:
119         case PA_SAMPLE_ULAW:
120         case PA_SAMPLE_ALAW: {
121             static const uint8_t u8_samples[] = {
122                 0x00, 0xFF, 0x7F, 0x80, 0x9f,
123                 0x3f, 0x01, 0xF0, 0x20, 0x21
124             };
125
126             memcpy(d, u8_samples, sizeof(u8_samples));
127             break;
128         }
129
130         case PA_SAMPLE_S16NE:
131         case PA_SAMPLE_S16RE: {
132             static const uint16_t u16_samples[] = {
133                 0x0000, 0xFFFF, 0x7FFF, 0x8000, 0x9fff,
134                 0x3fff, 0x0001, 0xF000, 0x0020, 0x0021
135             };
136
137             memcpy(d, u16_samples, sizeof(u16_samples));
138             break;
139         }
140
141         case PA_SAMPLE_S24_32NE:
142         case PA_SAMPLE_S24_32RE:
143         case PA_SAMPLE_S32NE:
144         case PA_SAMPLE_S32RE: {
145             static const uint32_t u32_samples[] = {
146                 0x00000001, 0xFFFF0002, 0x7FFF0003, 0x80000004, 0x9fff0005,
147                 0x3fff0006, 0x00010007, 0xF0000008, 0x00200009, 0x0021000A
148             };
149
150             memcpy(d, u32_samples, sizeof(u32_samples));
151             break;
152         }
153
154         case PA_SAMPLE_S24NE:
155         case PA_SAMPLE_S24RE: {
156             /* Need to be on a byte array because they are not aligned */
157             static const uint8_t u24_samples[] = {
158                 0x00, 0x00, 0x01,
159                 0xFF, 0xFF, 0x02,
160                 0x7F, 0xFF, 0x03,
161                 0x80, 0x00, 0x04,
162                 0x9f, 0xff, 0x05,
163                 0x3f, 0xff, 0x06,
164                 0x01, 0x00, 0x07,
165                 0xF0, 0x00, 0x08,
166                 0x20, 0x00, 0x09,
167                 0x21, 0x00, 0x0A
168             };
169
170             memcpy(d, u24_samples, sizeof(u24_samples));
171             break;
172         }
173
174         case PA_SAMPLE_FLOAT32NE:
175         case PA_SAMPLE_FLOAT32RE: {
176             float *u = d;
177             static const float float_samples[] = {
178                 0.0f, -1.0f, 1.0f, 4711.0f, 0.222f,
179                 0.33f, -.3f, 99.0f, -0.555f, -.123f
180             };
181
182             if (ss->format == PA_SAMPLE_FLOAT32RE) {
183                 for (i = 0; i < 10; i++)
184                     u[i] = PA_FLOAT32_SWAP(float_samples[i]);
185             } else
186               memcpy(d, float_samples, sizeof(float_samples));
187
188             break;
189         }
190
191         default:
192             pa_assert_not_reached();
193     }
194
195     pa_memblock_release(r);
196
197     return r;
198 }
199
200 int main(int argc, char *argv[]) {
201     pa_mempool *pool;
202     pa_sample_spec a;
203     pa_cvolume v;
204
205     pa_log_set_level(PA_LOG_DEBUG);
206
207     pa_assert_se(pool = pa_mempool_new(FALSE, 0));
208
209     a.channels = 1;
210     a.rate = 44100;
211
212     v.channels = a.channels;
213     v.values[0] = pa_sw_volume_from_linear(0.9);
214
215     for (a.format = 0; a.format < PA_SAMPLE_MAX; a.format ++) {
216         pa_memchunk i, j, k;
217         pa_mix_info m[2];
218         void *ptr;
219
220         printf("=== mixing: %s\n", pa_sample_format_to_string(a.format));
221
222         /* Generate block */
223         i.memblock = generate_block(pool, &a);
224         i.length = pa_memblock_get_length(i.memblock);
225         i.index = 0;
226
227         dump_block(&a, &i);
228
229         /* Make a copy */
230         j = i;
231         pa_memblock_ref(j.memblock);
232         pa_memchunk_make_writable(&j, 0);
233
234         /* Adjust volume of the copy */
235         pa_volume_memchunk(&j, &a, &v);
236
237         dump_block(&a, &j);
238
239         m[0].chunk = i;
240         m[0].volume.values[0] = PA_VOLUME_NORM;
241         m[0].volume.channels = a.channels;
242         m[1].chunk = j;
243         m[1].volume.values[0] = PA_VOLUME_NORM;
244         m[1].volume.channels = a.channels;
245
246         k.memblock = pa_memblock_new(pool, i.length);
247         k.length = i.length;
248         k.index = 0;
249
250         ptr = (uint8_t*) pa_memblock_acquire(k.memblock) + k.index;
251         pa_mix(m, 2, ptr, k.length, &a, NULL, FALSE);
252         pa_memblock_release(k.memblock);
253
254         dump_block(&a, &k);
255
256         pa_memblock_unref(i.memblock);
257         pa_memblock_unref(j.memblock);
258         pa_memblock_unref(k.memblock);
259     }
260
261     pa_mempool_free(pool);
262
263     return 0;
264 }