Use static constants to keep the generated sample blocks.
[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/resampler.h>
30 #include <pulsecore/macro.h>
31 #include <pulsecore/endianmacros.h>
32 #include <pulsecore/memblock.h>
33 #include <pulsecore/sample-util.h>
34
35 #include <liboil/liboil.h>
36
37 static float swap_float(float a) {
38     uint32_t *b = (uint32_t*) &a;
39     *b = PA_UINT32_SWAP(*b);
40     return a;
41 }
42
43 static void dump_block(const pa_sample_spec *ss, const pa_memchunk *chunk) {
44     void *d;
45     unsigned i;
46
47     d = pa_memblock_acquire(chunk->memblock);
48
49     switch (ss->format) {
50
51         case PA_SAMPLE_U8:
52         case PA_SAMPLE_ULAW:
53         case PA_SAMPLE_ALAW: {
54             uint8_t *u = d;
55
56             for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
57                 printf("0x%02x ", *(u++));
58
59             break;
60         }
61
62         case PA_SAMPLE_S16NE:
63         case PA_SAMPLE_S16RE: {
64             uint16_t *u = d;
65
66             for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
67                 printf("0x%04x ", *(u++));
68
69             break;
70         }
71
72         case PA_SAMPLE_S32NE:
73         case PA_SAMPLE_S32RE: {
74             uint32_t *u = d;
75
76             for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
77                 printf("0x%08x ", *(u++));
78
79             break;
80         }
81
82         case PA_SAMPLE_FLOAT32NE:
83         case PA_SAMPLE_FLOAT32RE: {
84             float *u = d;
85
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++;
89             }
90
91             break;
92         }
93
94         default:
95             pa_assert_not_reached();
96     }
97
98     printf("\n");
99
100     pa_memblock_release(chunk->memblock);
101 }
102
103 static pa_memblock* generate_block(pa_mempool *pool, const pa_sample_spec *ss) {
104     pa_memblock *r;
105     void *d;
106     unsigned i;
107
108     pa_assert_se(r = pa_memblock_new(pool, pa_frame_size(ss) * 10));
109     d = pa_memblock_acquire(r);
110
111     switch (ss->format) {
112
113         case PA_SAMPLE_U8:
114         case PA_SAMPLE_ULAW:
115         case PA_SAMPLE_ALAW: {
116             static const uint8_t u8_samples[] =
117               { 0x00, 0xFF, 0x7F, 0x80, 0x9f,
118                 0x3f, 0x01, 0xF0, 0x20, 0x21 };
119
120             memcpy(d, &u8_samples[0], sizeof(u8_samples));
121             break;
122         }
123
124         case PA_SAMPLE_S16NE:
125         case PA_SAMPLE_S16RE: {
126             static const uint16_t u16_samples[] =
127               { 0x0000, 0xFFFF, 0x7FFF, 0x8000, 0x9fff,
128                 0x3fff, 0x0001, 0xF000, 0x0020, 0x0021 };
129
130             memcpy(d, &u16_samples[0], sizeof(u16_samples));
131             break;
132         }
133
134         case PA_SAMPLE_S32NE:
135         case PA_SAMPLE_S32RE: {
136             static const uint32_t u32_samples[] =
137               { 0x00000001, 0xFFFF0002, 0x7FFF0003, 0x80000004, 0x9fff0005,
138                 0x3fff0006, 0x00010007, 0xF0000008, 0x00200009, 0x0021000A };
139
140             memcpy(d, &u32_samples[0], sizeof(u32_samples));
141             break;
142         }
143
144         case PA_SAMPLE_FLOAT32NE:
145         case PA_SAMPLE_FLOAT32RE: {
146             float *u = d;
147             static const float float_samples[] =
148               { 0.0f, -1.0f, 1.0f, 4711.0f, 0.222f,
149                 0.33f, -.3f, 99.0f, -0.555f, -.123f };
150
151             if (ss->format == PA_SAMPLE_FLOAT32RE) {
152                 for (i = 0; i < 10; i++)
153                     u[i] = swap_float(float_samples[i]);
154             } else {
155               memcpy(d, &float_samples[0], sizeof(float_samples));
156             }
157
158             break;
159         }
160
161         default:
162             pa_assert_not_reached();
163     }
164
165     pa_memblock_release(r);
166
167     return r;
168 }
169
170 int main(int argc, char *argv[]) {
171     pa_mempool *pool;
172     pa_sample_spec a;
173     pa_cvolume v;
174
175     oil_init();
176     pa_log_set_level(PA_LOG_DEBUG);
177
178     pa_assert_se(pool = pa_mempool_new(FALSE, 0));
179
180     a.channels = 1;
181     a.rate = 44100;
182
183     v.channels = a.channels;
184     v.values[0] = pa_sw_volume_from_linear(0.9);
185
186     for (a.format = 0; a.format < PA_SAMPLE_MAX; a.format ++) {
187         pa_memchunk i, j, k;
188         pa_mix_info m[2];
189         void *ptr;
190
191         printf("=== mixing: %s\n", pa_sample_format_to_string(a.format));
192
193         /* Generate block */
194         i.memblock = generate_block(pool, &a);
195         i.length = pa_memblock_get_length(i.memblock);
196         i.index = 0;
197
198         dump_block(&a, &i);
199
200         /* Make a copy */
201         j = i;
202         pa_memblock_ref(j.memblock);
203         pa_memchunk_make_writable(&j, 0);
204
205         /* Adjust volume of the copy */
206         pa_volume_memchunk(&j, &a, &v);
207
208         dump_block(&a, &j);
209
210         m[0].chunk = i;
211         m[0].volume.values[0] = PA_VOLUME_NORM;
212         m[0].volume.channels = a.channels;
213         m[1].chunk = j;
214         m[1].volume.values[0] = PA_VOLUME_NORM;
215         m[1].volume.channels = a.channels;
216
217         k.memblock = pa_memblock_new(pool, i.length);
218         k.length = i.length;
219         k.index = 0;
220
221         ptr = (uint8_t*) pa_memblock_acquire(k.memblock) + k.index;
222         pa_mix(m, 2, ptr, k.length, &a, NULL, FALSE);
223         pa_memblock_release(k.memblock);
224
225         dump_block(&a, &k);
226
227         pa_memblock_unref(i.memblock);
228         pa_memblock_unref(j.memblock);
229         pa_memblock_unref(k.memblock);
230     }
231
232     pa_mempool_free(pool);
233
234     return 0;
235 }