X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=test%2Faudio_io_test.c;h=efb54c4addda41da50a9f0beb9a0ada42e09daaa;hb=5bef189267474a190fcb31879cfbf245dbdac031;hp=ad97bdd0b214e58c4dc013236df3b184bb8e9a73;hpb=ad14291d88dd8b57a5fb063382cd8411d2bd9fb0;p=platform%2Fcore%2Fapi%2Faudio-io.git diff --git a/test/audio_io_test.c b/test/audio_io_test.c index ad97bdd..efb54c4 100644 --- a/test/audio_io_test.c +++ b/test/audio_io_test.c @@ -19,701 +19,1136 @@ #include #include #include +#include #include +#include #include - -//#define _NEW_SOUND_MANAGER_API_ -#define _SESSION_SOUND_MANAGER_API_ +#include #ifndef M_PI #define M_PI (3.14159265) #endif #define TABLE_SIZE (200) -typedef struct -{ - float sine[TABLE_SIZE]; - int left_channel; - int right_channel; +typedef struct { + float sine[TABLE_SIZE]; + int left_channel; + int right_channel; } test_wav_t; test_wav_t test_wav; -static int ch_table[3] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO }; +static int ch_table[] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO, + AUDIO_CHANNEL_MULTI_3, AUDIO_CHANNEL_MULTI_4, AUDIO_CHANNEL_MULTI_5, + AUDIO_CHANNEL_MULTI_6, AUDIO_CHANNEL_MULTI_7, AUDIO_CHANNEL_MULTI_8, + AUDIO_CHANNEL_MULTI_9, AUDIO_CHANNEL_MULTI_10, AUDIO_CHANNEL_MULTI_11, + AUDIO_CHANNEL_MULTI_12, AUDIO_CHANNEL_MULTI_13, AUDIO_CHANNEL_MULTI_14, + AUDIO_CHANNEL_MULTI_15, AUDIO_CHANNEL_MULTI_16 }; +static char *state_str[] = { "IDLE", "RUNNING", "PAUSED" }; -void play_file(char *file, int length, int ch) +static void _audio_in_state_cb(audio_in_h handle, audio_io_state_e previous, audio_io_state_e current, + bool by_policy, void *user_data) { - audio_out_h output; - FILE* fp = fopen (file, "r"); - if (fp == NULL) { - printf ("fopen failed\n"); - return; - } - - char * buf = malloc (length); - if (buf == NULL) { - printf ("malloc failed\n"); - fclose (fp); - return; - } - - printf ("start to play [%s][%d][%d]\n", file, length, ch); - //audio_out_create(44100, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output); - audio_out_create_new(44100, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, &output); - if (fread (buf, 1, length, fp) != length) { - printf ("error!!!!\n"); - } - - audio_out_prepare(output); - audio_out_write(output, buf, length); - audio_out_unprepare(output); - - audio_out_destroy (output); - - fclose (fp); - - printf ("play done\n"); + printf(">>> _audio_in_state_cb() : handle(%p), (%d,%s) => (%d,%s), by_policy(%d), user_data(%p)\n", + handle, previous, state_str[previous], current, state_str[current], by_policy, user_data); } -#define DUMP_FILE "/root/test.raw" - - -void play_file_sample(char *file, int frequency, int ch, int type) +static void _audio_out_state_cb(audio_out_h handle, audio_io_state_e previous, audio_io_state_e current, + bool by_policy, void *user_data) { - audio_out_h output; - int file_size = 0; - int read_bytes = 0; - int buffer_size = 0; - char * buf = NULL; - - if (ch < 0 || ch > 2) { - ch = 0; - } - - FILE* fp = fopen (file, "r"); - if (fp == NULL) { - printf("open failed\n"); - return; - } - /*Get the size*/ - fseek(fp, 0, SEEK_END); - file_size = ftell(fp); - fseek(fp, 0, SEEK_SET); - - printf ("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type); - if (type) { - //audio_out_create(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output); - audio_out_create_new(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, &output); - } else { - //audio_out_create(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_U8, SOUND_TYPE_MEDIA, &output); - audio_out_create_new(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_U8, &output); - } - audio_out_prepare(output); - audio_out_get_buffer_size(output, &buffer_size); - - buf = (char *) malloc(buffer_size); - if (buf == NULL) { - printf ("malloc failed\n"); - audio_out_unprepare(output); - audio_out_destroy(output); - fclose (fp); - return; - } - //audio_out_prepare(output); - - while (file_size > 0) { - read_bytes = fread (buf, 1, buffer_size, fp); - printf ("Read %d Requested - %d\n", read_bytes, buffer_size); - audio_out_write(output, buf, read_bytes); - file_size = file_size - read_bytes; - } - - audio_out_unprepare(output); - audio_out_destroy (output); - - free(buf); - fclose (fp); - printf ("play done\n"); + printf(">>> _audio_out_state_cb() : handle(%p), (%d,%s) => (%d,%s), by_policy(%d), user_data(%p)\n", + handle, previous, state_str[previous], current, state_str[current], by_policy, user_data); } -int audio_io_test(int length, int num, int ch) +static void _play_file(char *file, int length, int num, int ch) { - int ret, size, i; - audio_in_h input; - if ((ret = audio_in_create(44100, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, &input)) == AUDIO_IO_ERROR_NONE) { - ret = audio_in_ignore_session(input); - if (ret != 0) { - printf ("ERROR, set session mix\n"); - audio_in_destroy(input); - return 0; - } - - ret = audio_in_prepare(input); - if (ret != 0) { - printf ("ERROR, prepare\n"); - audio_in_destroy(input); - return 0; - } - - FILE* fp = fopen (DUMP_FILE, "wb+"); - - if (fp == NULL) { - printf ("ERROR, file open failed\n"); - audio_in_destroy(input); - return 0; - } - - if ((ret = audio_in_get_buffer_size(input, &size)) == AUDIO_IO_ERROR_NONE) { - size = length; - char *buffer = alloca(size); - - for (i=0; i AUDIO_IO_ERROR_NONE) { - fwrite (buffer, size, sizeof(char), fp); - printf ("PASS, size=%d, ret=%d\n", size, ret); - } else { - printf ("FAIL, size=%d, ret=%d\n", size, ret); - } - } - } - - fclose (fp); - - audio_in_destroy(input); - } - - play_file (DUMP_FILE, length*num, ch); - - return 1; + int i = 0; + int ret = 0; + int total_length = length * num; + audio_out_h output; + FILE *fp = fopen(file, "r"); + if (fp == NULL) { + printf("fopen failed\n"); + return; + } + + char *buf = malloc(total_length); + if (buf == NULL) { + printf("malloc failed\n"); + fclose(fp); + return; + } + + printf("# start to play [%s][%d][%d]\n", file, total_length, ch); + printf(" > create\n"); + audio_out_create_new(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &output); + if (fread(buf, 1, total_length, fp) != total_length) + printf("error!!!!\n"); + + ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_out_set_state_changed_cb failed. \n"); + } + + printf(" > prepare\n"); + audio_out_prepare(output); + + char *pbuf = buf; + for (i = 0; i < num; i++) { + printf("### write = (%d/%d) ============== \n", i, num); + audio_out_write(output, pbuf, length); + pbuf += length; + + } + printf(" < unprepare\n"); + audio_out_drain(output); + audio_out_unprepare(output); + + printf(" < destroy\n"); + audio_out_destroy(output); + + fclose(fp); + free(buf); + + printf("# play done\n"); } -int audio_io_loopback_in_test() -{ - int ret, size; - audio_in_h input; - FILE* fp = fopen ("/tmp/dump_test.raw", "wb+"); - - if (fp == NULL) { - printf("open failed \n"); - return 0; - } - - if ((ret = audio_in_create(16000, AUDIO_CHANNEL_MONO , AUDIO_SAMPLE_TYPE_S16_LE, &input)) == AUDIO_IO_ERROR_NONE) { - ret = audio_in_ignore_session(input); - if (ret != 0) { - printf ("ERROR, set session mix\n"); - goto exit; - } - - ret = audio_in_prepare(input); - if (ret != 0) { - printf ("ERROR, prepare\n"); - goto exit; - } - - ret = audio_in_get_buffer_size(input, &size); - if (ret != AUDIO_IO_ERROR_NONE) { - printf ("audio_in_get_buffer_size failed.\n"); - goto exit; - } - - while (1) { - char *buffer = alloca(size); - if ((ret = audio_in_read(input, (void*)buffer, size)) > AUDIO_IO_ERROR_NONE) { - fwrite (buffer, size, sizeof(char), fp); - printf ("PASS, size=%d, ret=%d\n", size, ret); - } else { - printf ("FAIL, size=%d, ret=%d\n", size, ret); - } - } - } - -exit: - audio_in_destroy(input); - - fclose (fp); +#define DUMP_FILE "/root/test.raw" - return ret; +FILE *fp = NULL; +static void _audio_out_stream_cb(audio_out_h handle, size_t nbytes, void *user_data) +{ + char *buf = NULL; + int read_bytes = 0; + int written = 0; + + if (fp == NULL) { + printf("FILE is NULL\n"); + return; + } + + buf = (char *)malloc(nbytes); + if (!buf) { + printf("malloc(%zu) failed\n", nbytes); + return; + } + + read_bytes = fread(buf, 1, nbytes, fp); + + written = audio_out_write(handle, buf, read_bytes); + printf("written : %6d/%6d (requested %zu)\n", written, read_bytes, nbytes); + + if (read_bytes < nbytes) { + printf("EOS!!!!\n"); + fclose(fp); + fp = NULL; + } + free(buf); } -int audio_io_loopback_test() +static void _play_file_sample_async(char *file, int frequency, int ch, int type) { - int ret, size; - audio_in_h input; - audio_out_h output; - char *buffer = NULL; - - ret = audio_in_create(16000, AUDIO_CHANNEL_MONO , AUDIO_SAMPLE_TYPE_S16_LE, &input); - if (ret != AUDIO_IO_ERROR_NONE) { - printf ("audio_in_create_ex failed. \n"); - return 0; - } - - //ret = audio_out_create(16000, AUDIO_CHANNEL_MONO , AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_CALL, &output); - ret = audio_out_create_new(16000, AUDIO_CHANNEL_MONO , AUDIO_SAMPLE_TYPE_S16_LE, &output); - if (ret != AUDIO_IO_ERROR_NONE) { - printf ("audio_out_create failed. \n"); - return 0; - } - - ret = audio_in_prepare(input); - if (ret != 0) { - printf ("audio_in_prepare failed.\n"); - audio_in_destroy(input); - return 0; - } else { - ret = audio_in_get_buffer_size(input, &size); - if (ret != AUDIO_IO_ERROR_NONE) { - printf ("audio_in_get_buffer_size failed.\n"); - return 0; - } else { - printf("size(%d)\n", size); - buffer = alloca(size); - } - } - - ret = audio_out_prepare(output); - if (ret != 0) { - printf ("audio_out_prepare failed.\n"); - audio_out_destroy(output); - return 0; - } - - if (buffer == NULL) { - printf("buffer is null\n"); - return 0; - } - - while (1) { - ret = audio_in_read(input, (void*)buffer, size); - if (ret > AUDIO_IO_ERROR_NONE) { - ret = audio_out_write(output, buffer, size); - if (ret > AUDIO_IO_ERROR_NONE) { - printf("audio read/write success. buffer(%p), size(%d)\n", buffer, size); - } else { - printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size); - } - } else - printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size); - } + audio_out_h output; + int file_size = 0; -} + if (ch < 0 || ch > 2) + ch = 0; -audio_in_h input; -audio_out_h output; + fp = fopen(file, "r"); + if (fp == NULL) { + printf("open failed\n"); + return; + } + printf("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type); + audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output); -FILE* fp_w = NULL; + audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL); + audio_out_set_stream_cb(output, _audio_out_stream_cb, fp); + audio_out_prepare(output); -#ifdef _NEW_SOUND_MANAGER_API_ -sound_stream_info_h g_stream_info_read_h = NULL; -sound_stream_info_h g_stream_info_write_h = NULL; + while (fp) { + usleep(10000); /* 10ms */ + } -static void focus_callback_read (sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason_for_change, const char *additional_info, void *user_data) { - int ret = 0; - sound_stream_focus_state_e playback_focus_state; - sound_stream_focus_state_e recording_focus_state; - printf ("*** focus_callback_read is called, stream_info(%p) ***\n", stream_info); - printf (" - reason_for_change(%d), additional_info(%s), user_data(%p)\n", reason_for_change, additional_info, user_data); - ret = sound_manager_get_focus_state (stream_info, &playback_focus_state, &recording_focus_state); - if (!ret) - printf (" - focus_state(playback_focus:%d, recording_focus:%d)\n", playback_focus_state, recording_focus_state); - if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) { - printf (" -- PLAYBACK_FOCUS acquired\n"); - } - if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) { - printf (" -- FOCUS_RECORDING acquired\n"); - } - printf ("*** focus_callback_read is ended, stream_info(%p) ****\n", stream_info); - return; -} - -static void focus_callback_write (sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason_for_change, const char *additional_info, void *user_data) { - int ret = 0; - sound_stream_focus_state_e playback_focus_state; - sound_stream_focus_state_e recording_focus_state; - printf ("*** focus_callback_write is called, stream_info(%p) ***\n", stream_info); - printf (" - reason_for_change(%d), additional_info(%s), user_data(%p)\n", reason_for_change, additional_info, user_data); - ret = sound_manager_get_focus_state (stream_info, &playback_focus_state, &recording_focus_state); - if (!ret) - printf (" - focus_state(playback_focus:%d, recording_focus:%d)\n", playback_focus_state, recording_focus_state); - if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) { - printf (" -- PLAYBACK_FOCUS acquired\n"); - } - if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) { - printf (" -- FOCUS_RECORDING acquired\n"); - } - printf ("*** focus_callback_write is ended, stream_info(%p) ****\n", stream_info); - return; -} -#endif + printf("FINISHED!!!\n"); -static void interrupted_callback_read(audio_io_interrupted_code_e code, void *user_data) { - printf ("*** interrupted_callback_read is called, code(%d), user_data(%p) ***\n", code, user_data); -} + audio_out_unprepare(output); + audio_out_destroy(output); -static void interrupted_callback_write(audio_io_interrupted_code_e code, void *user_data) { - printf ("*** interrupted_callback_write is called, code(%d), user_data(%p) ***\n", code, user_data); + printf("play done\n"); } -static void _audio_io_stream_read_cb (audio_in_h handle, size_t nbytes, void *user_data) +static void _play_file_sample_sync(char *file, int frequency, int ch, int type) { - const void *buffer = NULL; - unsigned int len = (unsigned int) nbytes; - - //printf("_audio_io_stream_read_cb : handle=%p, nbytes=%d, user_data=%p\n", handle, nbytes, user_data); - - if (len > 0) { - audio_in_peek (handle, &buffer, &len); - if (fp_w) { - fwrite(buffer, sizeof(char), len, fp_w); - } - audio_in_drop (handle); - } + audio_out_h output; + int file_size = 0; + int read_bytes = 0; + int buffer_size = 0; + char *buf = NULL; + FILE *fp = NULL; + int i = 0; + + if (ch < 0 || ch > 2) + ch = 0; + + fp = fopen(file, "r"); + if (fp == NULL) { + printf("open failed\n"); + return; + } + /* Get the size */ + fseek(fp, 0, SEEK_END); + file_size = ftell(fp); + fseek(fp, 0, SEEK_SET); + + printf("Play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type); + audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output); + + audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL); + audio_out_prepare(output); + + audio_out_get_buffer_size(output, &buffer_size); + buf = (char *)malloc(buffer_size); + if (buf == NULL) { + printf("malloc failed\n"); + goto EXIT; + } + + printf("Press any key to continue....\n"); + getchar(); + printf("Start to read[%d] from file and write : \n", buffer_size); + + while (file_size > 0) { + read_bytes = fread(buf, 1, buffer_size, fp); + printf("."); + i++; + if (i % 10 == 0) + printf("|"); + if (i % 100 == 0) { + printf("\n"); + i = 0; + } + audio_out_write(output, buf, read_bytes); + file_size = file_size - read_bytes; + usleep(10000); + } + +EXIT: + audio_out_unprepare(output); + audio_out_destroy(output); + + if (buf) + free(buf); + if (fp) + fclose(fp); + + printf("\nEOS!!!! Play done\n"); } -static void _audio_io_stream_write_cb (audio_out_h handle, size_t nbytes, void *user_data) +static sound_device_h *find_device_by_id(int id) { - short* buffer = NULL; - int ret = 0; - int i = 0; - - //printf("_audio_io_stream_write_cb : handle=%p, nbytes=%d, user_data=%p\n", handle, nbytes, user_data); - - if (nbytes > 0) { - buffer = (short *) malloc(nbytes); - if (buffer == NULL) { - printf ("malloc failed\n"); - return; - } - memset (buffer, 0, nbytes); - - for (i=0; i= TABLE_SIZE) test_wav.left_channel -= TABLE_SIZE; - test_wav.right_channel += 3; - if (test_wav.right_channel >= TABLE_SIZE) test_wav.right_channel -= TABLE_SIZE; - } - - ret = audio_out_write(handle, buffer, nbytes); - if (ret > AUDIO_IO_ERROR_NONE) { - //printf("audio write success. buffer(%p), nbytes(%d)\n", buffer, nbytes); - } - - free (buffer); - } -} + sound_device_list_h device_list; + sound_device_h device; + int _id; -static void _audio_in_state_cb (audio_in_h handle, audio_io_state_e previous, audio_io_state_e current, bool by_policy, void *user_data) -{ - printf(">>> _audio_in_state_cb() : handle(%p), current(%d), previous(%d), by_policy(%d), user_data(%p)\n", handle, current, previous, by_policy, user_data); + if (sound_manager_get_device_list(SOUND_DEVICE_ALL_MASK, &device_list) != 0) + return NULL; + + while (!sound_manager_get_next_device(device_list, &device)) { + if (sound_manager_get_device_id(device, &_id) != 0) { + device = NULL; + goto out; + } + + if (_id == id) + break; + } + +out: + sound_manager_free_device_list(device_list); + + return device; } -static void _audio_out_state_cb (audio_in_h handle, audio_io_state_e previous, audio_io_state_e current, bool by_policy, void *user_data) +static int _record_echocancel(char *filename, int rate, int channels, int ref_deviceid) { - printf(">>> _audio_out_state_cb() : handle(%p), current(%d), previous(%d), by_policy(%d), user_data(%p)\n", handle, current, previous, by_policy, user_data); + int ret, size; + sound_stream_info_h stream_info; + sound_device_h ref_dev = NULL; + audio_in_h input = NULL; + FILE *fp = NULL; + char *buffer = NULL; + + ret = audio_in_create(rate, ch_table[channels], AUDIO_SAMPLE_TYPE_S16_LE, &input); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio in create error = 0x%x\n", ret); + return -1; + } + + ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &stream_info); + if (ret) { + printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret); + goto out; + } + + if (!(ref_dev = find_device_by_id(ref_deviceid))) { + printf("fail to get ref device id\b"); + goto out; + } + + ret = sound_manager_set_echo_cancel_reference_device(stream_info, ref_dev); + if (ret) { + printf("fail to sound_manager_set_echo_cancel_reference_device(), ret(0x%x)\n", ret); + goto out; + } + + ret = audio_in_set_sound_stream_info(input, stream_info); + if (ret) { + printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret); + goto out; + } + + ret = audio_in_prepare(input); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("ERROR, prepare\n"); + goto out; + } + + ret = audio_in_get_buffer_size(input, &size); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("ERROR, get_buffer_size\n"); + goto out; + } + + fp = fopen(filename, "wb+"); + if (fp == NULL) { + printf("ERROR, file open failed\n"); + goto out; + } + + buffer = alloca(size); + + while (1) { + ret = audio_in_read(input, (void *)buffer, size); + if (ret <= 0) { + printf("FAIL, size=%d, ret=0x%x\n", size, ret); + goto out; + } + + fwrite(buffer, size, sizeof(char), fp); + } + +out: + fclose(fp); + audio_in_unprepare(input); + audio_in_destroy(input); + + return 0; } -int _convert_cmd_and_run(char cmd, int mode) { - int ret = 0; - switch (cmd) { - case 'P': - if (mode & 0x01) ret = audio_out_prepare(output); - if (mode & 0x02) ret = audio_in_prepare(input); - break; - case 'u': - if (mode & 0x01) ret = audio_out_unprepare(output); - if (mode & 0x02) ret = audio_in_unprepare(input); - break; - case 'p': - if (mode & 0x01) ret = audio_out_pause(output); - if (mode & 0x02) ret = audio_in_pause(input); - break; - case 'r': - if (mode & 0x01) ret = audio_out_resume(output); - if (mode & 0x02) ret = audio_in_resume(input); - break; - case 'd': - if (mode & 0x01) ret = audio_out_drain(output); - //if (mode & 0x02) ret = audio_in_drain(input); - break; - case 'f': - if (mode & 0x01) ret = audio_out_flush(output); - if (mode & 0x02) ret = audio_in_flush(input); - break; - case 'i': -#ifdef _NEW_SOUND_MANAGER_API_ - ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_write, NULL, &g_stream_info_write_h); - if (ret) { - printf ("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret); - } -#endif -#ifdef _SESSION_SOUND_MANAGER_API_ - ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA); - if (ret) { - printf ("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret); - } - ret = sound_manager_set_media_session_option (SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY); - if (ret) { - printf ("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret); - } -#endif - break; - case 'q': /* quit */ - ret = 1; - break; - default: - ret = 1; - break; - } - return ret; +static int _record_and_play(int length, int num, int ch) +{ + int ret, size, i; + audio_in_h input = NULL; + FILE *fp = NULL; + char *buffer = NULL; + + ret = audio_in_create(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &input); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio in create error = 0x%x\n", ret); + return -1; + } + + ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_in_set_state_changed_cb failed. \n"); + goto error; + } + + ret = audio_in_prepare(input); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("ERROR, prepare\n"); + goto error; + } + + fp = fopen(DUMP_FILE, "wb+"); + if (fp == NULL) { + printf("ERROR, file open failed\n"); + goto error; + } + + ret = audio_in_get_buffer_size(input, &size); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("ERROR, prepare\n"); + goto error; + } + + size = length; + buffer = alloca(size); + + for (i = 0; i < num; i++) { + printf("### read = (%d/%d) ============== ", i, num); + if ((ret = audio_in_read(input, (void *)buffer, size)) > AUDIO_IO_ERROR_NONE) { + fwrite(buffer, size, sizeof(char), fp); + printf("PASS, size=%d, ret=0x%x\n", size, ret); + } else { + printf("FAIL, size=%d, ret=0x%x\n", size, ret); + } + } + + fclose(fp); + + audio_in_unprepare(input); + audio_in_destroy(input); + + _play_file(DUMP_FILE, length, num, ch); + + return 0; + +error: + audio_in_destroy(input); + if (fp) + fclose(fp); + return -1; } -int audio_io_async_test(int mode) +static int _direct_loopback() { - int ret, size; - char *buffer = NULL; - int i=0; + int ret, size; + audio_in_h input = NULL; + audio_out_h output = NULL; + char *buffer = NULL; + int i = 0; + + ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_in_create_ex failed. \n"); + return ret; + } + ret = audio_out_create_new(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &output); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_out_create_new failed. \n"); + goto exit; + } + + ret = audio_in_prepare(input); + if (ret != 0) { + printf("audio_in_prepare failed, err(0x%x)\n", ret); + goto exit; + } + + ret = audio_in_get_buffer_size(input, &size); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret); + goto exit; + } + + printf("size(%d)\n", size); + buffer = alloca(size); + + ret = audio_out_prepare(output); + if (ret != 0) { + printf("audio_out_prepare failed, err(0x%x)\n", ret); + goto exit; + } + + if (buffer == NULL) { + printf("buffer is null\n"); + ret = -1; + goto exit; + } + + printf("Start to loopback(read/write) with [16kHz/MONO/S16LE]!!!\n"); + + while (1) { + ret = audio_in_read(input, (void *)buffer, size); + if (ret > AUDIO_IO_ERROR_NONE) { + ret = audio_out_write(output, buffer, size); + if (ret > AUDIO_IO_ERROR_NONE) { + printf("."); + i++; + if (i % 10 == 0) + printf("|"); + if (i % 100 == 0) { + printf("\n"); + i = 0; + } + } else { + printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size); + } + } else { + printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size); + } + usleep(10000); + } - char cmd = 0; - int cmd_ret; +exit: + if (input) + audio_in_destroy(input); + if (output) + audio_out_destroy(output); - int write_mode = (mode & 0x01); - int read_mode = (mode & 0x02); + return ret; - if ((write_mode == 0) && (read_mode == 0)) { - printf ("not vaild mode.\n"); - return 0; - } +} - if (read_mode) { +audio_in_h input; +audio_out_h output; -#ifdef _SESSION_SOUND_MANAGER_API_ - printf ("set session for capture.\n"); +FILE *fp_w = NULL; - ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA); - if (ret) { - printf ("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret); - } +sound_stream_info_h g_stream_info_read_h = NULL; +sound_stream_info_h g_stream_info_write_h = NULL; - ret = sound_manager_set_media_session_option (SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY); - if (ret) { - printf ("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret); - } -#endif +static void _focus_cb(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask, + sound_stream_focus_state_e focus_state, + sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, + const char *additional_info, void *user_data) +{ + printf("*** focus_callback_read is called, stream_info(%p, read(%p)/write(%p)) ***\n", + stream_info, g_stream_info_read_h, g_stream_info_write_h); + printf(" - reason_for_change(%d), additional_info(%s), user_data(%p)\n", + reason_for_change, additional_info, user_data); + printf(" - focus_state is :%d \n", focus_state); - printf ("audio_in_create\n"); - ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO , AUDIO_SAMPLE_TYPE_S16_LE, &input); - if (ret != AUDIO_IO_ERROR_NONE) { - printf ("audio_in_create_ex failed. \n"); - return 0; - } - printf ("audio_in_create success!!! [%p]\n", input); - - ret = audio_in_set_stream_cb(input, _audio_io_stream_read_cb, NULL); - if (ret != AUDIO_IO_ERROR_NONE) { - printf ("audio_in_set_stream_cb failed. \n"); - return 0; - } - printf ("audio_in_set_stream_cb success!!! [%p]\n", input); - - ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL); - if (ret != AUDIO_IO_ERROR_NONE) { - printf ("audio_out_set_state_changed_cb failed. \n"); - return 0; - } - printf ("audio_out_set_state_changed_cb success!!! [%p]\n", input); - - fp_w = fopen( "/tmp/pcm_w.raw", "w"); - -#ifdef _NEW_SOUND_MANAGER_API_ - //set stream type as SOUND_STREAM_TYPE_MEDIA - ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_read, NULL, &g_stream_info_read_h); - if (ret) { - printf ("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret); - } - ret = audio_in_set_stream_info(input, g_stream_info_read_h); -#endif + return; +} - ret = audio_in_set_interrupted_cb(input, interrupted_callback_read, NULL); - } +static void _audio_io_stream_read_cb(audio_in_h handle, size_t nbytes, void *user_data) +{ + const void *buffer = NULL; + unsigned int len = (unsigned int)nbytes; - if (write_mode) { - printf ("before audio_out_create\n"); - getchar(); + if (len == 0) + return; -#ifdef _SESSION_SOUND_MANAGER_API_ - printf ("set session for playback.\n"); + audio_in_peek(handle, &buffer, &len); + if (fp_w) + fwrite(buffer, sizeof(char), len, fp_w); + audio_in_drop(handle); +} - ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA); - if (ret) { - printf ("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret); - } +static void _audio_io_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data) +{ + short *buffer = NULL; + int i = 0; + + if (nbytes == 0) + return; + + buffer = (short *)malloc(nbytes); + if (buffer == NULL) { + printf("malloc failed\n"); + return; + } + memset(buffer, 0, nbytes); + + for (i = 0; i < nbytes / 2; i += 2) { + buffer[i] = (short)32768 *test_wav.sine[test_wav.left_channel]; /* left */ + buffer[i + 1] = (short)32768 *test_wav.sine[test_wav.right_channel]; /* right */ + test_wav.left_channel += 1; + if (test_wav.left_channel >= TABLE_SIZE) + test_wav.left_channel -= TABLE_SIZE; + test_wav.right_channel += 3; + if (test_wav.right_channel >= TABLE_SIZE) + test_wav.right_channel -= TABLE_SIZE; + } + + audio_out_write(handle, buffer, nbytes); + + free(buffer); +} - ret = sound_manager_set_media_session_option (SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY); - if (ret) { - printf ("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret); - } -#endif +int _convert_cmd_and_run(char cmd, int mode) +{ + int ret = 0; + switch (cmd) { + case 'P': + if (mode & 0x01) + ret = audio_out_prepare(output); + if (mode & 0x02) + ret = audio_in_prepare(input); + break; + case 'u': + if (mode & 0x01) + ret = audio_out_unprepare(output); + if (mode & 0x02) + ret = audio_in_unprepare(input); + break; + case 'p': + if (mode & 0x01) + ret = audio_out_pause(output); + if (mode & 0x02) + ret = audio_in_pause(input); + break; + case 'r': + if (mode & 0x01) + ret = audio_out_resume(output); + if (mode & 0x02) + ret = audio_in_resume(input); + break; + case 'd': + if (mode & 0x01) + ret = audio_out_drain(output); + break; + case 'f': + if (mode & 0x01) + ret = audio_out_flush(output); + if (mode & 0x02) + ret = audio_in_flush(input); + break; + case 'i': + ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h); + if (ret) + printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret); + break; + case 'q': /* quit */ + ret = 1; + break; + default: + ret = 1; + break; + } + return ret; +} - printf ("audio_out_create\n"); - //ret = audio_out_create(44100, AUDIO_CHANNEL_STEREO , AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output); - ret = audio_out_create_new(44100, AUDIO_CHANNEL_STEREO , AUDIO_SAMPLE_TYPE_S16_LE, &output); - if (ret != AUDIO_IO_ERROR_NONE) { - printf ("audio_out_create failed. \n"); - return 0; - } - printf ("audio_out_create success!!! [%p]\n", output); - - ret = audio_out_set_stream_cb(output, _audio_io_stream_write_cb, NULL); - if (ret != AUDIO_IO_ERROR_NONE) { - printf ("audio_out_set_stream_cb failed. \n"); - return 0; - } - printf ("audio_out_set_stream_cb success!!! [%p]\n", output); - - ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL); - if (ret != AUDIO_IO_ERROR_NONE) { - printf ("audio_out_set_state_changed_cb failed. \n"); - return 0; - } - printf ("audio_out_set_state_changed_cb success!!! [%p]\n", output); - -#ifdef _NEW_SOUND_MANAGER_API_ - //set stream type as SOUND_STREAM_TYPE_MEDIA - ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_write, NULL, &g_stream_info_write_h); - if (ret) { - printf ("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret); - } - ret = audio_out_set_stream_info(output, g_stream_info_write_h); -#endif +int audio_io_async_test(int mode) +{ + int ret, size; + char *buffer = NULL; + int i = 0; + + char cmd = 0; + int cmd_ret; + + int write_mode = (mode & 0x01); + int read_mode = (mode & 0x02); + + sound_stream_focus_state_e playback_focus_state; + sound_stream_focus_state_e recording_focus_state; + + if ((write_mode == 0) && (read_mode == 0)) { + printf("not vaild mode.\n"); + return 0; + } + + if (read_mode) { + + printf("audio_in_create\n"); + ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_in_create_ex failed. \n"); + return 0; + } + printf("audio_in_create success!!! [%p]\n", input); + + ret = audio_in_set_stream_cb(input, _audio_io_stream_read_cb, NULL); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_in_set_stream_cb failed. \n"); + goto EXIT; + } + printf("audio_in_set_stream_cb success!!! [%p]\n", input); + + ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_out_set_state_changed_cb failed. \n"); + goto EXIT; + } + printf("audio_out_set_state_changed_cb success!!! [%p]\n", input); + + ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_read_h); + if (ret) { + printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret); + goto EXIT; + } + ret = audio_in_set_sound_stream_info(input, g_stream_info_read_h); + if (ret) + printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret); + + ret = sound_manager_acquire_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL); + if (ret) { + printf("fail to sound_manager_acquire_focus() for RECORDING, ret(0x%x)\n", ret); + goto EXIT; + } + + fp_w = fopen("/tmp/pcm_w.raw", "w"); + } + + if (write_mode) { + printf("before audio_out_create_new\n"); + getchar(); + + printf("audio_out_create_new\n"); + ret = audio_out_create_new(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_out_create_new failed. \n"); + goto EXIT; + } + printf("audio_out_create_new success!!! [%p]\n", output); + + ret = audio_out_set_stream_cb(output, _audio_io_stream_write_cb, NULL); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_out_set_stream_cb failed. \n"); + goto EXIT; + } + printf("audio_out_set_stream_cb success!!! [%p]\n", output); + + ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_out_set_state_changed_cb failed. \n"); + goto EXIT; + } + printf("audio_out_set_state_changed_cb success!!! [%p]\n", output); + + ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h); + if (ret) { + printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret); + goto EXIT; + } + + ret = audio_out_set_sound_stream_info(output, g_stream_info_write_h); + if (ret) + printf("fail to audio_out_set_sound_stream_info(), ret(0x%x)\n", ret); + + ret = sound_manager_acquire_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL); + if (ret) { + printf("fail to sound_manager_acquire_focus() for PLAYBACK, ret(0x%x)\n", ret); + goto EXIT; + } + + /* generate wave data */ + for (i = 0; i < TABLE_SIZE; i++) + test_wav.sine[i] = 0.9 * (float)sin(((double)i / (double)TABLE_SIZE) * M_PI * 2.); + test_wav.left_channel = test_wav.right_channel = 0; + } + + if (read_mode) { + printf("before audio_in_prepare\n"); + getchar(); + printf("audio_in_prepare\n"); + ret = audio_in_prepare(input); + if (ret != 0) { + printf("audio_in_prepare failed, err(0x%x)\n", ret); + audio_in_destroy(input); + goto EXIT; + } else { + ret = audio_in_get_buffer_size(input, &size); + if (ret != AUDIO_IO_ERROR_NONE) { + printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret); + goto EXIT; + } else { + printf("size(%d)\n", size); + buffer = alloca(size); + } + } + + if (buffer == NULL) { + printf("buffer is null\n"); + goto EXIT; + } + } + + if (write_mode) { + printf("before audio_out_prepare\n"); + getchar(); + printf("audio_out_prepare\n"); + ret = audio_out_prepare(output); + if (ret != 0) { + printf("audio_out_prepare failed, err(0x%x)\n", ret); + audio_out_destroy(output); + goto EXIT; + } + } + + do { + printf("command(q:quit) : "); + ret = getchar(); + if (ret == EOF) + goto EXIT; + cmd = (char)ret; + if (cmd != '\n') + getchar(); + cmd_ret = _convert_cmd_and_run(cmd, mode); + printf(" - result code : %d\n", cmd_ret); + } while (cmd != 'q'); + +EXIT: + if (read_mode) { + if (input) { + printf("audio_in_unprepare\n"); + audio_in_unprepare(input); + printf("audio_in_destroy\n"); + audio_in_destroy(input); + input = NULL; + } + + if (fp_w) { + fclose(fp_w); + fp_w = NULL; + } + + if (g_stream_info_read_h) { + ret = sound_manager_get_focus_state(g_stream_info_read_h, NULL, &recording_focus_state); + if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) { + ret = sound_manager_release_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL); + if (ret) + printf("fail to sound_manager_release_focus() for recording, ret(0x%x)\n", ret); + } + ret = sound_manager_destroy_stream_information(g_stream_info_read_h); + if (ret) + printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret); + g_stream_info_read_h = NULL; + } + } + + if (write_mode) { + if (output) { + printf("audio_out_unprepare\n"); + audio_out_unprepare(output); + printf("audio_out_destroy\n"); + audio_out_destroy(output); + } + + if (g_stream_info_write_h) { + ret = sound_manager_get_focus_state(g_stream_info_write_h, &playback_focus_state, NULL); + if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) { + ret = sound_manager_release_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL); + if (ret) + printf("fail to sound_manager_release_focus() for playback, ret(0x%x)\n", ret); + } + ret = sound_manager_destroy_stream_information(g_stream_info_write_h); + if (ret) + printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret); + g_stream_info_write_h = NULL; + } + } + + return 0; +} - ret = audio_out_set_interrupted_cb(output, interrupted_callback_write, NULL); - - //generate wave data - for (i=0; i 16) { + printf("Invalid channel\n"); + return 0; + } + printf("run with [%s][%s][%s]\n", argv[1], argv[2], argv[3]); + _record_and_play(atoi(argv[1]), atoi(argv[2]), channel_idx); + } else if (argc == 6) { + if (strcmp(argv[1], "play") == 0) + _play_file_sample_sync(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5])); + else if (strcmp(argv[1], "playasync") == 0) + _play_file_sample_async(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5])); + else if (strcmp(argv[1], "echocancel") == 0) + _record_echocancel(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5])); + } else { + printf("- Usages :\n"); + printf("- # audio_io_test loopback\n"); + printf("- # audio_io_test stress\n"); + printf("- # audio_io_test [length to read] [number of iteration] [channels]\n"); + printf("- # audio_io_test async [write(1) | read(2)]\n"); + printf("- # audio_io_test play [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n"); + printf("- # audio_io_test playasync [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n"); + printf("- # audio_io_test echocancel [filename] [sample rate] [channels] [ref_dev]\n"); + } + return 0; +}