1559cb337037e2c72efda894d14a8bafc6846582
[platform/core/api/audio-io.git] / test / audio_io_test_ec.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <time.h>
8 #include <stdbool.h>
9
10 #include <audio_io.h>
11 #include <sound_manager.h>
12 #include <sound_manager_internal.h>
13
14 #ifndef TIZEN_FEATURE_TV_PROD
15 static double _get_rand_double_range(double min, double max)
16 {
17         double ret;
18         int r;
19
20         r = rand();
21         ret = (max / RAND_MAX) * r * (r & 0x1 ? 1 : -1);
22
23         return ret;
24 }
25
26 static void _white_noise_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data)
27 {
28         int i;
29         short *ptr;
30         char *buffer;
31         double amp = 0.3 * 32767.0;
32
33         buffer = (char *)malloc(nbytes);
34         if (!buffer)
35                 return;
36
37         ptr = (short *)buffer;
38         for (i = 0; i < nbytes; i += 2, ptr++)
39                 ptr[0] = (short)(amp * _get_rand_double_range(-1.0, 1.0));
40
41         audio_out_write(handle, buffer, nbytes);
42
43         free(buffer);
44 }
45
46 int play_white_noise_async(audio_out_h *output)
47 {
48         audio_out_h _output = NULL;
49         int ret;
50
51         srand(time(NULL));
52
53         ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &_output);
54         if (ret != AUDIO_IO_ERROR_NONE) {
55                 printf("failed to create audio out\n");
56                 return -1;
57         }
58
59         ret = audio_out_set_stream_cb(_output, _white_noise_stream_write_cb, NULL);
60         if (ret != AUDIO_IO_ERROR_NONE) {
61                 printf("failed to set stream callback\n");
62                 goto fail;
63         }
64
65         ret = audio_out_prepare(_output);
66         if (ret != AUDIO_IO_ERROR_NONE) {
67                 printf("failed to prepare \n");
68                 goto fail;
69         }
70
71         *output = _output;
72
73         return 0;
74
75 fail:
76         audio_out_destroy(_output);
77
78         return -1;
79 }
80
81 int stop_white_noise_async(audio_out_h output)
82 {
83         int ret;
84
85         ret = audio_out_unprepare(output);
86         if (ret != AUDIO_IO_ERROR_NONE)
87                 printf("failed to prepare\n");
88
89         ret = audio_out_destroy(output);
90         if (ret != AUDIO_IO_ERROR_NONE) {
91                 printf("failed to destroy output\n");
92                 return -1;
93         }
94
95         return 0;
96 }
97
98 int capture_sound(char **buffer, int *length, int sec)
99 {
100         int ret;
101         char *_buffer = NULL;
102         audio_in_h input = NULL;
103         int _length = 16000 * 1 * 2 * sec; // 16Khz, mono, S16LE
104
105         if (!buffer) {
106                 printf("buffer is null\n");
107                 return -1;
108         }
109
110         ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
111         if (ret != AUDIO_IO_ERROR_NONE) {
112                 printf("failed to create audio_input\n");
113                 return -1;
114         }
115
116         ret = audio_in_prepare(input);
117         if (ret != AUDIO_IO_ERROR_NONE) {
118                 printf("failed to prepare %x0x\n", ret);
119                 goto fail;
120         }
121
122         _buffer = malloc(_length);
123         if (!_buffer) {
124                 printf("_buffer is null\n");
125                 goto fail;
126         }
127
128         ret = audio_in_read(input, _buffer, _length);
129         if (ret <= 0) {
130                 printf("failed to read ret(0x%x), length(%d)\n", ret, _length);
131                 goto fail;
132         }
133         ret = audio_in_unprepare(input);
134         if (ret != AUDIO_IO_ERROR_NONE) {
135                 printf("failed to unprepare 0x%x", ret);
136                 goto fail;
137         }
138
139         *buffer = _buffer;
140         *length = _length;
141
142         return 0;
143
144 fail:
145         if (input)
146                 audio_in_destroy(input);
147         if (buffer)
148                 free(buffer);
149
150         return -1;
151 }
152
153 int enable_echo_cancellation(bool enable)
154 {
155         int ret;
156
157         if (enable)
158                 ret = sound_manager_start_aec();
159         else
160                 ret = sound_manager_stop_aec();
161
162         if (ret != 0) {
163                 printf("failed to %s echo cancellation. ret(0x%x)\n",
164                                 enable ? "start" : "stop", ret);
165                 return -1;
166         }
167
168         return 0;
169 }
170
171 int test_echo_cancellation(char **buffer, bool enable_ec, bool enable_ref, int time_sec)
172 {
173         audio_out_h output = NULL;
174         char *_buffer = NULL;
175         int length;
176
177         if (enable_ec && enable_echo_cancellation(true))
178                 goto exit;
179
180         if (enable_ref && play_white_noise_async(&output))
181                 goto exit;
182
183         printf("Say something for %d seconds..  enable_ec(%s) enable_ref(%s)\n",
184                         time_sec,
185                         enable_ec ? "on" : "off",
186                         enable_ref? "on" : "off");
187
188         if (capture_sound(&_buffer, &length, time_sec))
189                 goto exit;
190
191         if (output && stop_white_noise_async(output))
192                 goto exit;
193
194         output = NULL;
195
196         if (enable_ec && enable_echo_cancellation(false))
197                 goto exit;
198
199         *buffer = _buffer;
200
201         return length;
202
203 exit:
204         if (output)
205                 stop_white_noise_async(output);
206
207         if (enable_ec)
208                 enable_echo_cancellation(false);
209
210         if (_buffer)
211                 free(_buffer);
212
213         return -1;
214 }
215
216 int dump_to_file(const char *filename, char *buffer, int length)
217 {
218         int fd, ret;
219
220         fd = open(filename, O_CREAT|O_TRUNC|O_RDWR, 0644);
221         if (fd < 0) {
222                 printf("failed to open file\n");
223                 return -1;
224         }
225
226         ret = write(fd, buffer, length);
227         if (ret < 0)
228                 printf("failed to write\n");
229
230         close(fd);
231
232         return 0;
233 }
234 #endif
235
236 int main(int argc, char **argv)
237 {
238 #ifndef TIZEN_FEATURE_TV_PROD
239         bool enable_ec;
240         bool enable_ref;
241         int time_sec;
242         char *filename;
243
244         char *buffer;
245         int size;
246
247         if (argc != 5) {
248                 printf("- Usages :\n");
249                 printf("- # audio_io_test_ec [filename] [on/off 0:1] [noise 0:1] [seconds]\n");
250                 return -1;
251         }
252
253         filename  = argv[1];
254         enable_ec = !!atoi(argv[2]);
255         enable_ref = !!atoi(argv[3]);
256         time_sec = atoi(argv[4]);
257
258         size = test_echo_cancellation(&buffer, enable_ec, enable_ref, time_sec);
259         if (size <= 0) {
260                 printf("failed to test aec\n");
261                 return -1;
262         }
263
264         if (dump_to_file(filename, buffer, size))
265                 printf("failed to save file %s\n", filename);
266
267         if (buffer)
268                 free(buffer);
269 #else
270         printf("not supported yet\n");
271 #endif
272
273         return 0;
274 }
275