Modify noise suppression functionality
[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 <sound_manager_internal.h>
23 #include <audio_io.h>
24
25 static bool arg_aec_is_set;
26 static int arg_aec_ref;
27 static sound_acoustic_echo_cancel_type_e arg_aec_type;
28
29 static bool arg_ns_is_set;
30 static sound_noise_suppression_type_e arg_ns_type;
31
32 static int arg_samplerate;
33 static audio_channel_e arg_channels;
34 static audio_sample_type_e arg_format;
35
36 static char arg_filename[256];
37
38 static int ch_table[] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO,
39                           AUDIO_CHANNEL_MULTI_3, AUDIO_CHANNEL_MULTI_4, AUDIO_CHANNEL_MULTI_5,
40                           AUDIO_CHANNEL_MULTI_6, AUDIO_CHANNEL_MULTI_7, AUDIO_CHANNEL_MULTI_8,
41                           AUDIO_CHANNEL_MULTI_9, AUDIO_CHANNEL_MULTI_10, AUDIO_CHANNEL_MULTI_11,
42                           AUDIO_CHANNEL_MULTI_12, AUDIO_CHANNEL_MULTI_13, AUDIO_CHANNEL_MULTI_14,
43                           AUDIO_CHANNEL_MULTI_15, AUDIO_CHANNEL_MULTI_16 };
44 static void record()
45 {
46         int ret, size;
47         sound_stream_info_h stream_info;
48         audio_in_h input = NULL;
49         FILE *fp = NULL;
50         char *buffer = NULL;
51
52         sound_device_list_h device_list = NULL;
53         sound_device_h device;
54         bool found = false;
55         int id;
56
57         ret = audio_in_create(arg_samplerate, ch_table[arg_channels], AUDIO_SAMPLE_TYPE_S16_LE, &input);
58         if (ret != AUDIO_IO_ERROR_NONE) {
59                 printf("audio in create error = 0x%x\n", ret);
60                 goto out;
61         }
62
63         ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &stream_info);
64         if (ret) {
65                 printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
66                 goto out;
67         }
68
69         if (arg_aec_is_set) {
70                 if (sound_manager_get_device_list(SOUND_DEVICE_ALL_MASK, &device_list) != SOUND_MANAGER_ERROR_NONE) {
71                         printf("fail to get device list");
72                         goto out;
73                 }
74
75                 while (sound_manager_get_next_device(device_list, &device) == SOUND_MANAGER_ERROR_NONE) {
76                         if (sound_manager_get_device_id(device, &id) != SOUND_MANAGER_ERROR_NONE)
77                                 goto out;
78
79                         if (arg_aec_ref == id) {
80                                 found = true;
81                                 break;
82                         }
83                 }
84
85                 if (!found) {
86                         printf("Failed to find reference device");
87                         goto out;
88                 }
89
90                 ret = sound_manager_set_echo_cancel_reference_device(stream_info, device, arg_aec_type);
91                 if (ret != SOUND_MANAGER_ERROR_NONE) {
92                         printf("fail to sound_manager_set_echo_cancel_reference_device(), ret(0x%x)\n", ret);
93                         goto out;
94                 }
95
96                 /* verify */
97                 {
98                         int device_id;
99                         int id;
100                         sound_acoustic_echo_cancel_type_e aec_type;
101
102                         ret = sound_manager_get_echo_cancel_reference_device(stream_info, &device_id, &aec_type);
103                         if (ret != SOUND_MANAGER_ERROR_NONE) {
104                                 printf("fail to sound_manager_get_echo_cancel_reference_device(), ret(0x%x)\n", ret);
105                                 goto out;
106                         }
107
108                         if (sound_manager_get_device_id(device, &id)) {
109                                 printf("fail to sound_manager_device_id\n");
110                                 goto out;
111                         }
112
113                         if (device_id != id || arg_aec_type != aec_type) {
114                                 printf("fail to verify aec args\n");
115                                 goto out;
116                         }
117                 }
118         }
119
120         if (arg_ns_is_set) {
121                 ret = sound_manager_set_noise_suppression(stream_info, arg_ns_is_set, arg_ns_type);
122                 if (ret != SOUND_MANAGER_ERROR_NONE) {
123                         printf("failed to sound_manager_set_noise_suppression(), ret(0x%x)\n", ret);
124                         goto out;
125                 }
126
127                 /* verify */
128                 {
129                         bool ns_enabled;
130                         sound_noise_suppression_type_e ns_type;
131
132                         ret = sound_manager_get_noise_suppression(stream_info, &ns_enabled, &ns_type);
133                         if (ret != SOUND_MANAGER_ERROR_NONE) {
134                                 printf("fail to sound_manager_get_echo_cancel_reference_device(), ret(0x%x)\n", ret);
135                                 goto out;
136                         }
137
138                         if (arg_ns_is_set != ns_enabled || arg_ns_type != ns_type) {
139                                 printf("fail to verify ns 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 int main(int argc, char **argv)
198 {
199         setbuf(stdout, NULL);
200         const char *optstring = "r:c:f:a:n:o:d:";
201         int option;
202
203         arg_channels = AUDIO_CHANNEL_MONO;
204         arg_format = AUDIO_SAMPLE_TYPE_S16_LE;
205
206         printf("===== Arguments information =====\n");
207         while (-1 != (option = getopt(argc, argv, optstring))) {
208                 switch (option) {
209                         case 'r': /* rate */
210                                 arg_samplerate = atoi(optarg);
211                                 printf("Samplerate : %d\n", arg_samplerate);
212                                 break;
213                         case 'd': /* reference device */
214                                 arg_aec_ref = atoi(optarg);
215                                 arg_aec_is_set = true;
216                                 printf("AEC reference device : %d\n", arg_aec_ref);
217                                 break;
218                         case 'a': /* AEC method */
219                                 arg_aec_type = atoi(optarg);
220                                 arg_aec_is_set = true;
221                                 printf("AEC reference type: %d\n", arg_aec_type);
222                                 break;
223                         case 'n': /* Noise suppression method */
224                                 arg_ns_type = atoi(optarg);
225                                 arg_ns_is_set = true;
226                                 printf("Noise Suppression Type : %d\n", arg_ns_type);
227                                 break;
228                         case 'o': /* output file */
229                                 snprintf(arg_filename, sizeof(arg_filename), "%s", optarg);
230                                 printf("Output filename : %s\n", arg_filename);
231                                 break;
232                         case 'c': /* channels */
233                                 arg_channels = atoi(optarg);
234                                 printf("Channels : %d\n", arg_channels);
235                                 break;
236                         case 'f': /* format */
237                                 arg_format = atoi(optarg);
238                                 printf("Format : %d\n", arg_format);
239                                 break;
240                         case 'h':
241                         default:
242                                 printf("audio_io_process_test -r [sample] [-d:reference_device_id] [-a:AEC type] [-n:NS type] -o [output filename]\n");
243                                 printf("audio_io_process_test -r 16000 -d 1 -a 0 -n 0 -o filename\n");
244                                 return 0;
245                 }
246         }
247         printf("=================================\n");
248
249         record();
250         printf("END\n");
251
252         return 0;
253 }