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.
22 #include <sound_manager.h>
25 #define DEFAULT_CHANNELS 1
27 static int arg_samplerate;
28 static audio_channel_e arg_channels;
29 static audio_sample_type_e arg_format;
31 static bool arg_ref_dev_is_set;
32 static int arg_ref_dev;
33 static bool arg_method_is_set;
34 static int arg_method;
35 static bool arg_reference_method_is_set;
36 static int arg_reference_method;
38 static char arg_filename[256];
40 static int ch_table[] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO,
41 AUDIO_CHANNEL_MULTI_3, AUDIO_CHANNEL_MULTI_4, AUDIO_CHANNEL_MULTI_5,
42 AUDIO_CHANNEL_MULTI_6, AUDIO_CHANNEL_MULTI_7, AUDIO_CHANNEL_MULTI_8,
43 AUDIO_CHANNEL_MULTI_9, AUDIO_CHANNEL_MULTI_10, AUDIO_CHANNEL_MULTI_11,
44 AUDIO_CHANNEL_MULTI_12, AUDIO_CHANNEL_MULTI_13, AUDIO_CHANNEL_MULTI_14,
45 AUDIO_CHANNEL_MULTI_15, AUDIO_CHANNEL_MULTI_16 };
50 sound_stream_info_h stream_info;
51 audio_in_h input = NULL;
55 sound_device_list_h device_list = NULL;
56 sound_device_h device;
60 ret = audio_in_create(arg_samplerate, ch_table[arg_channels], AUDIO_SAMPLE_TYPE_S16_LE, &input);
61 if (ret != AUDIO_IO_ERROR_NONE) {
62 fprintf(stderr, "audio in create error = 0x%x\n", ret);
66 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &stream_info);
68 fprintf(stderr, "fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
72 if (arg_reference_method_is_set) {
73 if (sound_manager_get_device_list(SOUND_DEVICE_ALL_MASK, &device_list) != SOUND_MANAGER_ERROR_NONE) {
74 fprintf(stderr, "fail to get device list");
78 while (sound_manager_get_next_device(device_list, &device) == SOUND_MANAGER_ERROR_NONE) {
79 if (sound_manager_get_device_id(device, &id) != SOUND_MANAGER_ERROR_NONE)
82 if (arg_ref_dev == id) {
89 fprintf(stderr, "Failed to find reference device");
93 ret = sound_manager_set_effect_method_with_reference(stream_info, arg_reference_method, device);
94 if (ret != SOUND_MANAGER_ERROR_NONE) {
95 fprintf(stderr, "fail to sound_manager_set_audio_effect_reference_device(), ret(0x%x)\n", ret);
103 sound_effect_method_with_reference_e method;
105 ret = sound_manager_get_effect_method_with_reference(stream_info, &method, &device_id);
106 if (ret != SOUND_MANAGER_ERROR_NONE) {
107 fprintf(stderr, "fail to sound_manager_get_audio_effect_reference_device(), ret(0x%x)\n", ret);
111 if (sound_manager_get_device_id(device, &id)) {
112 fprintf(stderr, "fail to sound_manager_device_id\n");
116 if (device_id != id || method != arg_reference_method) {
117 fprintf(stderr, "fail to verify aec args\n");
123 if (arg_method_is_set) {
124 ret = sound_manager_set_effect_method(stream_info, arg_method);
125 if (ret != SOUND_MANAGER_ERROR_NONE) {
126 fprintf(stderr, "failed to sound_manager_set_audio_effect_method(), ret(0x%x)\n", ret);
134 ret = sound_manager_get_effect_method(stream_info, &method);
135 if (ret != SOUND_MANAGER_ERROR_NONE) {
136 fprintf(stderr, "fail to sound_manager_get_audio_effect_reference_device(), ret(0x%x)\n", ret);
140 if (method != arg_method) {
141 fprintf(stderr, "fail to verify method args\n");
147 ret = audio_in_set_sound_stream_info(input, stream_info);
148 if (ret != AUDIO_IO_ERROR_NONE) {
149 fprintf(stderr, "fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret);
153 ret = audio_in_prepare(input);
154 if (ret != AUDIO_IO_ERROR_NONE) {
155 fprintf(stderr, "ERROR, prepare\n");
159 ret = audio_in_get_buffer_size(input, &size);
160 if (ret != AUDIO_IO_ERROR_NONE) {
161 fprintf(stderr, "ERROR, get_buffer_size\n");
165 if (strcmp(arg_filename, ""))
166 fp = fopen(arg_filename, "wb");
168 fp = fdopen(dup(STDOUT_FILENO), "wb");
171 fprintf(stderr, "failed to open file(%s)", arg_filename);
175 buffer = alloca(size);
178 ret = audio_in_read(input, (void *)buffer, size);
180 fprintf(stderr, "FAIL, size=%d, ret=0x%x\n", size, ret);
185 fprintf(stderr, "Failed to read, size=%d, ret=%d\n", size, ret);
189 fwrite(buffer, sizeof(short), size/sizeof(short), fp);
197 audio_in_unprepare(input);
198 audio_in_destroy(input);
202 if (sound_manager_free_device_list(device_list) != SOUND_MANAGER_ERROR_NONE)
203 fprintf(stderr, "fail to free device list\n");
208 static void playback_stdin()
215 ret = audio_out_create_new(arg_samplerate, ch_table[arg_channels], AUDIO_SAMPLE_TYPE_S16_LE, &output);
216 assert(ret == AUDIO_IO_ERROR_NONE);
218 fp = fdopen(dup(STDIN_FILENO), "r");
221 ret = audio_out_get_buffer_size(output, &size);
222 assert(ret == AUDIO_IO_ERROR_NONE);
224 buffer = (char *)malloc(size);
227 ret = audio_out_prepare(output);
228 assert(ret == AUDIO_IO_ERROR_NONE);
231 if (fread(buffer, size, 1, fp) <= 0)
234 ret = audio_out_write(output, (void *)buffer, size);
240 audio_out_destroy(output);
243 static void print_usage()
245 fprintf(stderr, "Usage\n\n");
246 fprintf(stderr, "audio_io_process_test -r [samplerate] -c [channels] -a [reference_method] -d [reference_device_id] -m [methods] -o [output filename]\n");
247 fprintf(stderr, "e.g) audio_io_process_test -r 48000 -d 1 -m 0x0001 -a 0x0004 -d 1 -o filename (method:webrtc and voicecall)\n");
249 fprintf(stderr, "\n");
250 fprintf(stderr, "Please refer to the optional arguments as following.\n\n");
251 fprintf(stderr, "Options\n");
252 fprintf(stderr, "-a\t\taudio-effects with reference device_id like AEC\n");
253 fprintf(stderr, "\t\te.g) refcopy:0x0001, aec-speex:0x0002, aec-webrtc:0x0004\n");
255 fprintf(stderr, "-m\t\taudio-effects like noise-suppression\n");
256 fprintf(stderr, "\t\te.g) rnnoise:0x0001, agc-speex:0x0002, pse:0x0200, doorbel:0x0100\n");
259 int main(int argc, char **argv)
261 setbuf(stdout, NULL);
262 const char *optstring = "r:c:f:m:o:d:a:p";
264 bool playback = false;
266 arg_channels = DEFAULT_CHANNELS;
267 arg_format = AUDIO_SAMPLE_TYPE_S16_LE;
274 fprintf(stderr, "===== Arguments information =====\n");
275 while (-1 != (option = getopt(argc, argv, optstring))) {
278 arg_samplerate = atoi(optarg);
279 fprintf(stderr, "Samplerate : %d\n", arg_samplerate);
281 case 'a': /* sound_effect_method_with_reference_e */
282 arg_reference_method_is_set = true;
283 arg_reference_method = (int)strtol(optarg, NULL, 16);
284 fprintf(stderr, "reference method : 0x%x, optarg(%s)\n",
285 arg_reference_method, optarg);
287 case 'd': /* reference device */
288 arg_ref_dev_is_set = true;
289 arg_ref_dev = atoi(optarg);
290 fprintf(stderr, "reference device : %d\n", arg_ref_dev);
292 case 'm': /* method */
293 arg_method_is_set = true;
294 arg_method = (int)strtol(optarg, NULL, 16);
295 fprintf(stderr, "method : 0x%x, optarg(%s)\n", arg_method, optarg);
297 case 'o': /* output file */
298 snprintf(arg_filename, sizeof(arg_filename), "%s", optarg);
299 fprintf(stderr, "Output filename : %s\n", arg_filename);
301 case 'c': /* channels */
302 arg_channels = atoi(optarg);
303 fprintf(stderr, "Channels : %d\n", arg_channels);
304 if (arg_channels < 1 || arg_channels > 16) {
305 fprintf(stderr, "Invalid channels : %d\n", arg_channels);
309 case 'f': /* format */
310 arg_format = atoi(optarg);
311 fprintf(stderr, "Format : %d\n", arg_format);
313 case 'p': /* playback for loopback test */
314 fprintf(stderr, "Playback : Yes\n");
323 fprintf(stderr, "=================================\n");
329 if ((arg_reference_method_is_set && !arg_ref_dev_is_set) ||
330 (!arg_reference_method_is_set && arg_ref_dev_is_set)) {
331 fprintf(stderr, "need to set reference devices and method both\n");
336 fprintf(stderr, "END\n");