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>
24 #define DEFAULT_CHANNELS 1
26 static int arg_samplerate;
27 static audio_channel_e arg_channels;
28 static audio_sample_type_e arg_format;
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;
37 static char arg_filename[256];
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 };
48 sound_stream_info_h stream_info;
49 audio_in_h input = NULL;
53 sound_device_list_h device_list = NULL;
54 sound_device_h device;
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);
64 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &stream_info);
66 printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
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");
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)
80 if (arg_ref_dev == id) {
87 printf("Failed to find reference device");
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);
101 sound_effect_method_with_reference_e method;
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);
109 if (sound_manager_get_device_id(device, &id)) {
110 printf("fail to sound_manager_device_id\n");
114 if (device_id != id || method != arg_reference_method) {
115 printf("fail to verify aec args\n");
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);
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);
138 if (method != arg_method) {
139 printf("fail to verify method 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 static void print_usage()
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");
204 printf("Please refer to the optional arguments as following.\n\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");
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");
213 int main(int argc, char **argv)
215 setbuf(stdout, NULL);
216 const char *optstring = "r:c:f:m:o:d:a:";
219 arg_channels = DEFAULT_CHANNELS;
220 arg_format = AUDIO_SAMPLE_TYPE_S16_LE;
227 printf("===== Arguments information =====\n");
228 while (-1 != (option = getopt(argc, argv, optstring))) {
231 arg_samplerate = atoi(optarg);
232 printf("Samplerate : %d\n", arg_samplerate);
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);
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);
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);
250 case 'o': /* output file */
251 snprintf(arg_filename, sizeof(arg_filename), "%s", optarg);
252 printf("Output filename : %s\n", arg_filename);
254 case 'c': /* channels */
255 arg_channels = atoi(optarg);
256 printf("Channels : %d\n", arg_channels);
257 if (arg_channels < 1 || arg_channels > 16) {
258 printf("Invalid channels : %d\n", arg_channels);
262 case 'f': /* format */
263 arg_format = atoi(optarg);
264 printf("Format : %d\n", arg_format);
272 printf("=================================\n");
274 if ((arg_reference_method_is_set && !arg_ref_dev_is_set) ||
275 (!arg_reference_method_is_set && arg_ref_dev_is_set)) {
276 printf("need to set reference devices and method both\n");