4276fb7dca8ebbf6467a482f83ad104891c4dbce
[platform/core/api/audio-io.git] / test / audio_io_process_test.c
1 /*
2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sound_manager.h>
22 #include <audio_io.h>
23
24 #define DEFAULT_CHANNELS        1
25
26 static int arg_samplerate;
27 static audio_channel_e arg_channels;
28 static audio_sample_type_e arg_format;
29
30 static bool arg_ref_dev_is_set;
31 static int arg_ref_dev;
32 static bool arg_method_is_set;
33 static int arg_method;
34 static bool arg_reference_method_is_set;
35 static int arg_reference_method;
36
37 static char arg_filename[256];
38
39 static int ch_table[] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO,
40                           AUDIO_CHANNEL_MULTI_3, AUDIO_CHANNEL_MULTI_4, AUDIO_CHANNEL_MULTI_5,
41                           AUDIO_CHANNEL_MULTI_6, AUDIO_CHANNEL_MULTI_7, AUDIO_CHANNEL_MULTI_8,
42                           AUDIO_CHANNEL_MULTI_9, AUDIO_CHANNEL_MULTI_10, AUDIO_CHANNEL_MULTI_11,
43                           AUDIO_CHANNEL_MULTI_12, AUDIO_CHANNEL_MULTI_13, AUDIO_CHANNEL_MULTI_14,
44                           AUDIO_CHANNEL_MULTI_15, AUDIO_CHANNEL_MULTI_16 };
45 static void record()
46 {
47         int ret, size;
48         sound_stream_info_h stream_info;
49         audio_in_h input = NULL;
50         FILE *fp = NULL;
51         char *buffer = NULL;
52
53         sound_device_list_h device_list = NULL;
54         sound_device_h device;
55         bool found = false;
56         int id;
57
58         ret = audio_in_create(arg_samplerate, ch_table[arg_channels], AUDIO_SAMPLE_TYPE_S16_LE, &input);
59         if (ret != AUDIO_IO_ERROR_NONE) {
60                 printf("audio in create error = 0x%x\n", ret);
61                 goto out;
62         }
63
64         ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &stream_info);
65         if (ret) {
66                 printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
67                 goto out;
68         }
69
70         if (arg_reference_method_is_set) {
71                 if (sound_manager_get_device_list(SOUND_DEVICE_ALL_MASK, &device_list) != SOUND_MANAGER_ERROR_NONE) {
72                         printf("fail to get device list");
73                         goto out;
74                 }
75
76                 while (sound_manager_get_next_device(device_list, &device) == SOUND_MANAGER_ERROR_NONE) {
77                         if (sound_manager_get_device_id(device, &id) != SOUND_MANAGER_ERROR_NONE)
78                                 goto out;
79
80                         if (arg_ref_dev == id) {
81                                 found = true;
82                                 break;
83                         }
84                 }
85
86                 if (!found) {
87                         printf("Failed to find reference device");
88                         goto out;
89                 }
90
91                 ret = sound_manager_set_effect_method_with_reference(stream_info, arg_reference_method, device);
92                 if (ret != SOUND_MANAGER_ERROR_NONE) {
93                         printf("fail to sound_manager_set_audio_effect_reference_device(), ret(0x%x)\n", ret);
94                         goto out;
95                 }
96
97                 /* verify */
98                 {
99                         int device_id;
100                         int id;
101                         sound_effect_method_with_reference_e method;
102
103                         ret = sound_manager_get_effect_method_with_reference(stream_info, &method, &device_id);
104                         if (ret != SOUND_MANAGER_ERROR_NONE) {
105                                 printf("fail to sound_manager_get_audio_effect_reference_device(), ret(0x%x)\n", ret);
106                                 goto out;
107                         }
108
109                         if (sound_manager_get_device_id(device, &id)) {
110                                 printf("fail to sound_manager_device_id\n");
111                                 goto out;
112                         }
113
114                         if (device_id != id || method != arg_reference_method) {
115                                 printf("fail to verify aec args\n");
116                                 goto out;
117                         }
118                 }
119         }
120
121         if (arg_method_is_set) {
122                 ret = sound_manager_set_effect_method(stream_info, arg_method);
123                 if (ret != SOUND_MANAGER_ERROR_NONE) {
124                         printf("failed to sound_manager_set_audio_effect_method(), ret(0x%x)\n", ret);
125                         goto out;
126                 }
127
128                 /* verify */
129                 {
130                         int method;
131
132                         ret = sound_manager_get_effect_method(stream_info, &method);
133                         if (ret != SOUND_MANAGER_ERROR_NONE) {
134                                 printf("fail to sound_manager_get_audio_effect_reference_device(), ret(0x%x)\n", ret);
135                                 goto out;
136                         }
137
138                         if (method != arg_method) {
139                                 printf("fail to verify method args\n");
140                                 goto out;
141                         }
142                 }
143         }
144
145         ret = audio_in_set_sound_stream_info(input, stream_info);
146         if (ret != AUDIO_IO_ERROR_NONE) {
147                 printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret);
148                 goto out;
149         }
150
151         ret = audio_in_prepare(input);
152         if (ret != AUDIO_IO_ERROR_NONE) {
153                 printf("ERROR, prepare\n");
154                 goto out;
155         }
156
157         ret = audio_in_get_buffer_size(input, &size);
158         if (ret != AUDIO_IO_ERROR_NONE) {
159                 printf("ERROR, get_buffer_size\n");
160                 goto out;
161         }
162
163         fp = fopen(arg_filename, "wb+");
164         if (fp == NULL) {
165                 printf("ERROR, file open failed\n");
166                 goto out;
167         }
168
169         buffer = alloca(size);
170
171         while (1) {
172                 ret = audio_in_read(input, (void *)buffer, size);
173                 if (ret <= 0) {
174                         printf("FAIL, size=%d, ret=0x%x\n", size, ret);
175                         goto out;
176                 }
177
178                 fwrite(buffer, size, sizeof(char), fp);
179         }
180
181 out:
182         if (fp)
183                 fclose(fp);
184
185         if (input) {
186                 audio_in_unprepare(input);
187                 audio_in_destroy(input);
188         }
189
190         if (device_list)
191                 if (sound_manager_free_device_list(device_list) != SOUND_MANAGER_ERROR_NONE)
192                         printf("fail to free device list\n");
193
194         return;
195 }
196
197 static void print_usage()
198 {
199         printf("Usage\n\n");
200         printf("audio_io_process_test -r [samplerate] -c [channels] -a [reference_method] -d [reference_device_id] -m [methods] -o [output filename]\n");
201         printf("e.g) audio_io_process_test -r 48000 -d 1 -m 0x0001 -a 0x0004 -d 1 -o filename (method:webrtc and voicecall)\n");
202
203         printf("\n");
204         printf("Please refer to the optional arguments as following.\n\n");
205         printf("Options\n");
206         printf("-a\t\taudio-effects with reference device_id like AEC\n");
207         printf("\t\te.g) refcopy:0x0001, aec-speex:0x0002, aec-webrtc:0x0004\n");
208
209         printf("-m\t\taudio-effects like noise-suppression\n");
210         printf("\t\te.g) rnnoise:0x0001, agc-speex:0x0002, pse:0x0200, doorbel:0x0100\n");
211 }
212
213 int main(int argc, char **argv)
214 {
215         setbuf(stdout, NULL);
216         const char *optstring = "r:c:f:m:o:d:a:";
217         int option;
218
219         arg_channels = DEFAULT_CHANNELS;
220         arg_format = AUDIO_SAMPLE_TYPE_S16_LE;
221
222         if (argc == 1) {
223                 print_usage();
224                 return 0;
225         }
226
227         printf("===== Arguments information =====\n");
228         while (-1 != (option = getopt(argc, argv, optstring))) {
229                 switch (option) {
230                         case 'r': /* rate */
231                                 arg_samplerate = atoi(optarg);
232                                 printf("Samplerate : %d\n", arg_samplerate);
233                                 break;
234                         case 'a': /* sound_effect_method_with_reference_e */
235                                 arg_reference_method_is_set = true;
236                                 arg_reference_method = (int)strtol(optarg, NULL, 16);
237                                 printf("reference method : 0x%x, optarg(%s)\n",
238                                                 arg_reference_method, optarg);
239                                 break;
240                         case 'd': /* reference device */
241                                 arg_ref_dev_is_set = true;
242                                 arg_ref_dev = atoi(optarg);
243                                 printf("reference device : %d\n", arg_ref_dev);
244                                 break;
245                         case 'm': /* method */
246                                 arg_method_is_set = true;
247                                 arg_method = (int)strtol(optarg, NULL, 16);
248                                 printf("method : 0x%x, optarg(%s)\n", arg_method, optarg);
249                                 break;
250                         case 'o': /* output file */
251                                 snprintf(arg_filename, sizeof(arg_filename), "%s", optarg);
252                                 printf("Output filename : %s\n", arg_filename);
253                                 break;
254                         case 'c': /* channels */
255                                 arg_channels = atoi(optarg);
256                                 printf("Channels : %d\n", arg_channels);
257                                 break;
258                         case 'f': /* format */
259                                 arg_format = atoi(optarg);
260                                 printf("Format : %d\n", arg_format);
261                                 break;
262                         case 'h':
263                         default:
264                                 print_usage();
265                                 return 0;
266                 }
267         }
268         printf("=================================\n");
269
270         if ((arg_reference_method_is_set && !arg_ref_dev_is_set) ||
271                         (!arg_reference_method_is_set && arg_ref_dev_is_set)) {
272                 printf("need to set reference devices and method both\n");
273                 return 0;
274         }
275
276         record();
277         printf("END\n");
278
279         return 0;
280 }