summaryrefslogtreecommitdiff
path: root/src/tests/ecore/ecore_test_ecore_audio.c
diff options
context:
space:
mode:
authorDaniel Willmann <d.willmann@samsung.com>2012-12-14 23:38:40 +0000
committerDaniel Willmann <daniel@totalueberwachung.de>2012-12-14 23:38:40 +0000
commit6c054fc7ec40b83e9f8294cf1db489ac1ad29d0a (patch)
treeb4dcd91e57623b090ddef0c7b38cc9d74ebd1548 /src/tests/ecore/ecore_test_ecore_audio.c
parent1c68e42f251f93137b9abad151d6375f991c333a (diff)
ecore_audio: Add tests case
The sounds used are in the public domain and were taken from freesound.org Signed-off-by: Daniel Willmann <d.willmann@samsung.com> SVN revision: 81004
Diffstat (limited to 'src/tests/ecore/ecore_test_ecore_audio.c')
-rw-r--r--src/tests/ecore/ecore_test_ecore_audio.c378
1 files changed, 378 insertions, 0 deletions
diff --git a/src/tests/ecore/ecore_test_ecore_audio.c b/src/tests/ecore/ecore_test_ecore_audio.c
new file mode 100644
index 0000000..71e0d9f
--- /dev/null
+++ b/src/tests/ecore/ecore_test_ecore_audio.c
@@ -0,0 +1,378 @@
1#ifdef HAVE_CONFIG_H
2#include <config.h>
3#endif
4
5#include <Ecore_Audio.h>
6
7#include "ecore_suite.h"
8
9#include <stdio.h>
10#include <Ecore.h>
11#include <Ecore_Audio.h>
12
13#define SF_FORMAT_RAW 0x040000
14#define SF_FORMAT_PCM_U8 0x0005
15
16#define SOUNDS_DIR TESTS_SRC_DIR"/src/tests/ecore/"
17
18Ecore_Audio_Object *out;
19Ecore_Audio_Object *in;
20
21Eina_Bool
22seek(void *data)
23{
24 double offs;
25 fail_if(ecore_audio_input_seek(in, 0, SEEK_SET) != 0);
26 fail_if(ecore_audio_input_seek(in, 1, SEEK_CUR) != 1);
27 offs = ecore_audio_input_seek(in, -1, SEEK_END);
28 fail_if((offs > 1.001) || (offs < 0.999));
29
30 fail_if(ecore_audio_input_seek(in, SEEK_CUR, 10) != -1);
31 fail_if(ecore_audio_input_seek(in, 5, 0) != -1);
32
33 fail_if(ecore_audio_input_seek(in, 0, SEEK_SET) != 0);
34}
35
36Eina_Bool
37input_del(void *data)
38{
39 printf("Deleting input\n");
40
41 ecore_audio_input_del(in);
42
43 return EINA_FALSE;
44}
45
46Eina_Bool
47output_add(void *data)
48{
49 out = ecore_audio_output_add(ECORE_AUDIO_TYPE_SNDFILE);
50 fail_if(!out);
51 ecore_audio_output_sndfile_filename_set(out, SOUNDS_DIR"tmp.ogg");
52
53 printf("Adding input\n");
54 ecore_audio_output_input_add(out, in);
55
56 return EINA_FALSE;
57}
58
59Eina_Bool
60_quit(void *data)
61{
62 printf("Deleting output\n");
63 ecore_audio_output_del(out);
64
65 printf("Quitting\n");
66 ecore_main_loop_quit();
67}
68
69START_TEST(ecore_test_ecore_audio_cleanup)
70{
71 in = ecore_audio_input_add(ECORE_AUDIO_TYPE_TONE);
72 fail_if(!in);
73 ecore_audio_input_tone_frequency_set(in, 1000);
74 ecore_audio_input_tone_duration_set(in, 2);
75
76 ecore_timer_add(1, output_add, NULL);
77 ecore_timer_add(2, seek, NULL);
78 ecore_timer_add(3.3, input_del, NULL);
79 ecore_timer_add(4, _quit, NULL);
80
81 ecore_main_loop_begin();
82
83 ecore_file_remove(SOUNDS_DIR"tmp.ogg");
84}
85END_TEST
86
87
88Eina_Bool
89output_add_default(void *data)
90{
91 out = ecore_audio_output_add(ECORE_AUDIO_TYPE_PULSE);
92 fail_if(!out);
93
94 printf("Adding input\n");
95 ecore_audio_output_input_add(out, in);
96
97 return EINA_FALSE;
98}
99
100Eina_Bool
101input_resume(void *data)
102{
103 double pos;
104
105 fail_if(ecore_audio_input_paused_get(in) != EINA_TRUE);
106 fail_if(ecore_audio_input_seek(in, 0, SEEK_CUR) != 0);
107 ecore_audio_input_paused_set(in, EINA_FALSE);
108
109 ecore_audio_input_looped_set(in, EINA_TRUE);
110
111 return EINA_FALSE;
112}
113
114Eina_Bool
115input_pause_seek(void *data)
116{
117 double pos;
118
119 fail_if(ecore_audio_input_paused_get(in) != EINA_FALSE);
120 ecore_audio_input_paused_set(in, EINA_TRUE);
121 pos = ecore_audio_input_seek(in, 0, SEEK_CUR);
122
123 fail_if(ecore_audio_input_seek(in, 0, SEEK_SET) != 0);
124 pos = ecore_audio_input_seek(in, 0, SEEK_CUR);
125 fail_if(pos > 0.01);
126
127 ecore_timer_add(0.2, input_resume, NULL);
128 return EINA_FALSE;
129}
130
131Eina_Bool looped(void *data, int type, void *event)
132{
133 Ecore_Audio_Object *in = (Ecore_Audio_Object *)event;
134 fail_if(!in);
135
136 fail_if(!ecore_audio_input_looped_get(in));
137 ecore_audio_input_looped_set(in, EINA_FALSE);
138 ecore_audio_input_seek(in, -0.5, SEEK_END);
139}
140
141Eina_Bool play_done(void *data, int type, void *event)
142{
143 Ecore_Audio_Object *in = (Ecore_Audio_Object *)event;
144 Eina_List *ins;
145 fail_if(!in);
146
147 ins = ecore_audio_output_inputs_get(out);
148 fail_if(eina_list_count(ins) != 1);
149
150 fail_if(eina_list_data_get(ins) != in);
151 ecore_audio_output_input_del(ecore_audio_input_output_get(in), in);
152 ecore_audio_input_del(in);
153 ecore_audio_output_del(out);
154 ecore_main_loop_quit();
155}
156
157START_TEST(ecore_test_ecore_audio_default)
158{
159 in = ecore_audio_input_add(ECORE_AUDIO_TYPE_SNDFILE);
160 ecore_audio_input_name_set(in, "modem.wav");
161 ecore_audio_input_sndfile_filename_set(in, SOUNDS_DIR"modem.wav");
162
163 ecore_timer_add(1, output_add_default, NULL);
164 ecore_timer_add(1.2, input_pause_seek, NULL);
165 ecore_event_handler_add(ECORE_AUDIO_INPUT_LOOPED, looped, NULL);
166 ecore_event_handler_add(ECORE_AUDIO_INPUT_ENDED, play_done, NULL);
167
168 ecore_main_loop_begin();
169
170}
171END_TEST
172
173START_TEST(ecore_test_ecore_audio_sndfile)
174{
175 double len;
176
177 in = ecore_audio_input_add(ECORE_AUDIO_TYPE_SNDFILE);
178 fail_if(!in);
179
180 ecore_audio_input_userdata_set(in, &len);
181 ecore_audio_input_name_set(in, "sms.ogg");
182 ecore_audio_input_sndfile_filename_set(in, SOUNDS_DIR"sms.ogg");
183 fail_if(ecore_audio_input_channels_get(in) != 2);
184 fail_if(ecore_audio_input_samplerate_get(in) != 44100);
185 len = ecore_audio_input_length_get(in);
186// fail_if(len == 0);
187// fail_if(len != ecore_audio_input_remaining_get(in));
188 fail_if(strcmp("sms.ogg", ecore_audio_input_name_get(in)));
189
190 fail_if(ecore_audio_input_userdata_get(in) != &len);
191 ecore_audio_input_del(in);
192}
193END_TEST
194
195struct buffer {
196 int offset;
197 int length;
198 unsigned char data[1024];
199};
200
201int _get_length(Ecore_Audio_Object *in)
202{
203 struct buffer *buf = ecore_audio_input_userdata_get(in);
204 return buf->length;
205}
206
207int _seek(Ecore_Audio_Object *in, int offs, int whence)
208{
209 struct buffer *buf = ecore_audio_input_userdata_get(in);
210
211 switch (whence) {
212 case SEEK_SET:
213 buf->offset = offs;
214 break;
215 case SEEK_CUR:
216 buf->offset += offs;
217 break;
218 case SEEK_END:
219 buf->offset = buf->length + offs;
220 break;
221 }
222 return buf->offset;
223}
224
225int _tell(Ecore_Audio_Object *in)
226{
227 struct buffer *buf = ecore_audio_input_userdata_get(in);
228 return buf->offset;
229}
230
231int _read(Ecore_Audio_Object *in, void *buffer, int length)
232{
233 struct buffer *buf = ecore_audio_input_userdata_get(in);
234
235 if ((buf->offset + length) > buf->length)
236 length = buf->length - buf->offset;
237
238 memcpy(buffer, buf->data + buf->offset, length);
239 buf->offset += length;
240
241 return length;
242}
243
244int _write(Ecore_Audio_Object *out, const void *buffer, int length)
245{
246 struct buffer *buf = ecore_audio_output_userdata_get(out);
247
248 if ((buf->offset + length) > buf->length)
249 length = buf->length - buf->offset;
250
251 memcpy(buf->data + buf->offset, buffer, length);
252
253 buf->offset += length;
254
255 return length;
256}
257
258Ecore_Audio_Vio vio = {
259 .get_length = _get_length,
260 .seek = _seek,
261 .tell = _tell,
262 .read = _read,
263 .write = _write,
264};
265
266Eina_Bool sndfile_done(void *data, int type, void *event)
267{
268 Ecore_Audio_Object *in = (Ecore_Audio_Object *)event;
269
270 ecore_audio_input_seek(in, 0, SEEK_SET);
271 ecore_audio_input_del(in);
272 ecore_audio_output_del(out);
273 ecore_main_loop_quit();
274}
275
276START_TEST(ecore_test_ecore_audio_sndfile_vio)
277{
278 struct buffer indata = {
279 .length = 1000,
280 .offset = 0,
281 };
282 for (int i=0; i < 1000; i++) {
283 indata.data[i] = i%256;
284 }
285
286 struct buffer outdata = {
287 .length = 1000,
288 .offset = 0,
289 };
290 for (int i=0; i < 1000; i++) {
291 outdata.data[i] = 0;
292 }
293
294 in = ecore_audio_input_add(ECORE_AUDIO_TYPE_SNDFILE);
295 fail_if(!in);
296
297 ecore_audio_input_name_set(in, "tmp");
298 ecore_audio_input_channels_set(in, 1);
299 ecore_audio_input_samplerate_set(in, 44100);
300 ecore_audio_input_userdata_set(in, &indata);
301 ecore_audio_input_sndfile_format_set(in, SF_FORMAT_RAW | SF_FORMAT_PCM_U8);
302
303 ecore_audio_input_sndfile_vio_set(in, &vio);
304
305 out = ecore_audio_output_add(ECORE_AUDIO_TYPE_SNDFILE);
306 fail_if(!out);
307 ecore_audio_output_name_set(out, "tmp");
308 ecore_audio_output_sndfile_format_set(out, SF_FORMAT_RAW | SF_FORMAT_PCM_U8);
309 ecore_audio_output_userdata_set(out, &outdata);
310
311 ecore_audio_output_sndfile_vio_set(out, &vio);
312
313 ecore_audio_output_input_add(out, in);
314 ecore_event_handler_add(ECORE_AUDIO_INPUT_ENDED, sndfile_done, NULL);
315
316 ecore_main_loop_begin();
317
318 // Off-by-one...must be libsndfile float conversion error?!
319 for (int i = 0; i<1000; i++) {
320 fail_if(indata.data[i] - outdata.data[i] > 1);
321 fail_if(indata.data[i] - outdata.data[i] < -1);
322 }
323}
324END_TEST
325
326START_TEST(ecore_test_ecore_audio_custom)
327{
328 in = ecore_audio_input_add(ECORE_AUDIO_TYPE_CUSTOM);
329 ecore_audio_input_del(in);
330}
331END_TEST
332
333START_TEST(ecore_test_ecore_audio_init)
334{
335 int ret;
336
337 ret = ecore_audio_init();
338 fail_if(ret != 2);
339
340 ret = ecore_audio_shutdown();
341 fail_if(ret != 1);
342
343}
344END_TEST
345
346void setup(void)
347{
348 int ret;
349
350 ret = eina_init();
351 ret = ecore_init();
352 ret = ecore_audio_init();
353}
354
355void teardown(void)
356{
357 int ret;
358
359 ret = ecore_audio_shutdown();
360 ret = ecore_shutdown();
361 ret = eina_shutdown();
362}
363
364void
365ecore_test_ecore_audio(TCase *tc)
366{
367 tcase_add_checked_fixture (tc, setup, teardown);
368
369 tcase_add_test(tc, ecore_test_ecore_audio_init);
370
371 tcase_add_test(tc, ecore_test_ecore_audio_default);
372 tcase_add_test(tc, ecore_test_ecore_audio_sndfile);
373 tcase_add_test(tc, ecore_test_ecore_audio_sndfile_vio);
374 tcase_add_test(tc, ecore_test_ecore_audio_custom);
375
376 tcase_add_test(tc, ecore_test_ecore_audio_cleanup);
377}
378