2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sound_manager.h>
22 #include <sound_manager_internal.h>
25 static bool arg_aec_is_set;
26 static int arg_aec_ref;
27 static sound_acoustic_echo_cancel_type_e arg_aec_type;
29 static bool arg_ns_is_set;
30 static sound_noise_suppression_type_e arg_ns_type;
32 static int arg_samplerate;
33 static audio_channel_e arg_channels;
34 static audio_sample_type_e arg_format;
36 static char arg_filename[256];
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 };
47 sound_stream_info_h stream_info;
48 audio_in_h input = NULL;
52 sound_device_list_h device_list = NULL;
53 sound_device_h device;
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);
63 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &stream_info);
65 printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
70 if (sound_manager_get_device_list(SOUND_DEVICE_ALL_MASK, &device_list) != SOUND_MANAGER_ERROR_NONE) {
71 printf("fail to get device list");
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)
79 if (arg_aec_ref == id) {
86 printf("Failed to find reference device");
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);
100 sound_acoustic_echo_cancel_type_e aec_type;
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);
108 if (sound_manager_get_device_id(device, &id)) {
109 printf("fail to sound_manager_device_id\n");
113 if (device_id != id || arg_aec_type != aec_type) {
114 printf("fail to verify aec args\n");
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);
130 sound_noise_suppression_type_e ns_type;
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);
138 if (arg_ns_is_set != ns_enabled || arg_ns_type != ns_type) {
139 printf("fail to verify ns args\n");
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);
151 ret = audio_in_prepare(input);
152 if (ret != AUDIO_IO_ERROR_NONE) {
153 printf("ERROR, prepare\n");
157 ret = audio_in_get_buffer_size(input, &size);
158 if (ret != AUDIO_IO_ERROR_NONE) {
159 printf("ERROR, get_buffer_size\n");
163 fp = fopen(arg_filename, "wb+");
165 printf("ERROR, file open failed\n");
169 buffer = alloca(size);
172 ret = audio_in_read(input, (void *)buffer, size);
174 printf("FAIL, size=%d, ret=0x%x\n", size, ret);
178 fwrite(buffer, size, sizeof(char), fp);
186 audio_in_unprepare(input);
187 audio_in_destroy(input);
191 if (sound_manager_free_device_list(device_list) != SOUND_MANAGER_ERROR_NONE)
192 printf("fail to free device list\n");
197 int main(int argc, char **argv)
199 setbuf(stdout, NULL);
200 const char *optstring = "r:c:f:a:n:o:d:";
203 arg_channels = AUDIO_CHANNEL_MONO;
204 arg_format = AUDIO_SAMPLE_TYPE_S16_LE;
206 printf("===== Arguments information =====\n");
207 while (-1 != (option = getopt(argc, argv, optstring))) {
210 arg_samplerate = atoi(optarg);
211 printf("Samplerate : %d\n", arg_samplerate);
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);
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);
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);
228 case 'o': /* output file */
229 snprintf(arg_filename, sizeof(arg_filename), "%s", optarg);
230 printf("Output filename : %s\n", arg_filename);
232 case 'c': /* channels */
233 arg_channels = atoi(optarg);
234 printf("Channels : %d\n", arg_channels);
236 case 'f': /* format */
237 arg_format = atoi(optarg);
238 printf("Format : %d\n", arg_format);
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");
247 printf("=================================\n");