alsa: when creating alsa sinks/sources include mapping name in device name to allow...
[pulseaudio-mirror.git] / src / tests / mix-test.c
blobac4b57b5b0c43fd2e91583db274c17b2bfe9780e
1 /***
2 This file is part of PulseAudio.
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.
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.
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 ***/
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <stdio.h>
26 #include <pulse/sample.h>
27 #include <pulse/volume.h>
29 #include <pulsecore/resampler.h>
30 #include <pulsecore/macro.h>
31 #include <pulsecore/endianmacros.h>
32 #include <pulsecore/memblock.h>
33 #include <pulsecore/sample-util.h>
35 #include <liboil/liboil.h>
37 static float swap_float(float a) {
38 uint32_t *b = (uint32_t*) &a;
39 *b = PA_UINT32_SWAP(*b);
40 return a;
43 static void dump_block(const pa_sample_spec *ss, const pa_memchunk *chunk) {
44 void *d;
45 unsigned i;
47 d = pa_memblock_acquire(chunk->memblock);
49 switch (ss->format) {
51 case PA_SAMPLE_U8:
52 case PA_SAMPLE_ULAW:
53 case PA_SAMPLE_ALAW: {
54 uint8_t *u = d;
56 for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
57 printf("0x%02x ", *(u++));
59 break;
62 case PA_SAMPLE_S16NE:
63 case PA_SAMPLE_S16RE: {
64 uint16_t *u = d;
66 for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
67 printf("0x%04x ", *(u++));
69 break;
72 case PA_SAMPLE_S32NE:
73 case PA_SAMPLE_S32RE: {
74 uint32_t *u = d;
76 for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
77 printf("0x%08x ", *(u++));
79 break;
82 case PA_SAMPLE_FLOAT32NE:
83 case PA_SAMPLE_FLOAT32RE: {
84 float *u = d;
86 for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
87 printf("%1.5f ", ss->format == PA_SAMPLE_FLOAT32NE ? *u : swap_float(*u));
88 u++;
91 break;
94 default:
95 pa_assert_not_reached();
98 printf("\n");
100 pa_memblock_release(chunk->memblock);
103 static pa_memblock* generate_block(pa_mempool *pool, const pa_sample_spec *ss) {
104 pa_memblock *r;
105 void *d;
106 unsigned i;
108 pa_assert_se(r = pa_memblock_new(pool, pa_frame_size(ss) * 10));
109 d = pa_memblock_acquire(r);
111 switch (ss->format) {
113 case PA_SAMPLE_U8:
114 case PA_SAMPLE_ULAW:
115 case PA_SAMPLE_ALAW: {
116 uint8_t *u = d;
118 u[0] = 0x00;
119 u[1] = 0xFF;
120 u[2] = 0x7F;
121 u[3] = 0x80;
122 u[4] = 0x9f;
123 u[5] = 0x3f;
124 u[6] = 0x1;
125 u[7] = 0xF0;
126 u[8] = 0x20;
127 u[9] = 0x21;
128 break;
131 case PA_SAMPLE_S16NE:
132 case PA_SAMPLE_S16RE: {
133 uint16_t *u = d;
135 u[0] = 0x0000;
136 u[1] = 0xFFFF;
137 u[2] = 0x7FFF;
138 u[3] = 0x8000;
139 u[4] = 0x9fff;
140 u[5] = 0x3fff;
141 u[6] = 0x1;
142 u[7] = 0xF000;
143 u[8] = 0x20;
144 u[9] = 0x21;
145 break;
148 case PA_SAMPLE_S32NE:
149 case PA_SAMPLE_S32RE: {
150 uint32_t *u = d;
152 u[0] = 0x00000001;
153 u[1] = 0xFFFF0002;
154 u[2] = 0x7FFF0003;
155 u[3] = 0x80000004;
156 u[4] = 0x9fff0005;
157 u[5] = 0x3fff0006;
158 u[6] = 0x10007;
159 u[7] = 0xF0000008;
160 u[8] = 0x200009;
161 u[9] = 0x21000A;
162 break;
165 case PA_SAMPLE_FLOAT32NE:
166 case PA_SAMPLE_FLOAT32RE: {
167 float *u = d;
169 u[0] = 0.0f;
170 u[1] = -1.0f;
171 u[2] = 1.0f;
172 u[3] = 4711.0f;
173 u[4] = 0.222f;
174 u[5] = 0.33f;
175 u[6] = -.3f;
176 u[7] = 99.0f;
177 u[8] = -0.555f;
178 u[9] = -.123f;
180 if (ss->format == PA_SAMPLE_FLOAT32RE)
181 for (i = 0; i < 10; i++)
182 u[i] = swap_float(u[i]);
184 break;
187 default:
188 pa_assert_not_reached();
191 pa_memblock_release(r);
193 return r;
196 int main(int argc, char *argv[]) {
197 pa_mempool *pool;
198 pa_sample_spec a;
199 pa_cvolume v;
201 oil_init();
202 pa_log_set_level(PA_LOG_DEBUG);
204 pa_assert_se(pool = pa_mempool_new(FALSE, 0));
206 a.channels = 1;
207 a.rate = 44100;
209 v.channels = a.channels;
210 v.values[0] = pa_sw_volume_from_linear(0.9);
212 for (a.format = 0; a.format < PA_SAMPLE_MAX; a.format ++) {
213 pa_memchunk i, j, k;
214 pa_mix_info m[2];
215 void *ptr;
217 printf("=== mixing: %s\n", pa_sample_format_to_string(a.format));
219 /* Generate block */
220 i.memblock = generate_block(pool, &a);
221 i.length = pa_memblock_get_length(i.memblock);
222 i.index = 0;
224 dump_block(&a, &i);
226 /* Make a copy */
227 j = i;
228 pa_memblock_ref(j.memblock);
229 pa_memchunk_make_writable(&j, 0);
231 /* Adjust volume of the copy */
232 pa_volume_memchunk(&j, &a, &v);
234 dump_block(&a, &j);
236 m[0].chunk = i;
237 m[0].volume.values[0] = PA_VOLUME_NORM;
238 m[0].volume.channels = a.channels;
239 m[1].chunk = j;
240 m[1].volume.values[0] = PA_VOLUME_NORM;
241 m[1].volume.channels = a.channels;
243 k.memblock = pa_memblock_new(pool, i.length);
244 k.length = i.length;
245 k.index = 0;
247 ptr = (uint8_t*) pa_memblock_acquire(k.memblock) + k.index;
248 pa_mix(m, 2, ptr, k.length, &a, NULL, FALSE);
249 pa_memblock_release(k.memblock);
251 dump_block(&a, &k);
253 pa_memblock_unref(i.memblock);
254 pa_memblock_unref(j.memblock);
255 pa_memblock_unref(k.memblock);
258 pa_mempool_free(pool);
260 return 0;