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.
23 #include <sound_manager.h>
28 #define M_PI (3.14159265)
31 #define TABLE_SIZE (200)
33 float sine[TABLE_SIZE];
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 static char *state_str[] = { "IDLE", "RUNNING", "PAUSED" };
44 static void _audio_in_state_cb(audio_in_h handle, audio_io_state_e previous, audio_io_state_e current,
45 bool by_policy, void *user_data)
47 printf(">>> _audio_in_state_cb() : handle(%p), (%d,%s) => (%d,%s), by_policy(%d), user_data(%p)\n",
48 handle, previous, state_str[previous], current, state_str[current], by_policy, user_data);
51 static void _audio_out_state_cb(audio_out_h handle, audio_io_state_e previous, audio_io_state_e current,
52 bool by_policy, void *user_data)
54 printf(">>> _audio_out_state_cb() : handle(%p), (%d,%s) => (%d,%s), by_policy(%d), user_data(%p)\n",
55 handle, previous, state_str[previous], current, state_str[current], by_policy, user_data);
58 static void _play_file(char *file, int length, int num, int ch)
62 int total_length = length * num;
64 FILE *fp = fopen(file, "r");
66 printf("fopen failed\n");
70 char *buf = malloc(total_length);
72 printf("malloc failed\n");
77 printf("# start to play [%s][%d][%d]\n", file, total_length, ch);
78 printf(" > create\n");
79 audio_out_create_new(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &output);
80 if (fread(buf, 1, total_length, fp) != total_length)
81 printf("error!!!!\n");
83 ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
84 if (ret != AUDIO_IO_ERROR_NONE) {
85 printf("audio_out_set_state_changed_cb failed. \n");
88 printf(" > prepare\n");
89 audio_out_prepare(output);
92 for (i = 0; i < num; i++) {
93 printf("### write = (%d/%d) ============== \n", i, num);
94 audio_out_write(output, pbuf, length);
98 printf(" < unprepare\n");
99 audio_out_drain(output);
100 audio_out_unprepare(output);
102 printf(" < destroy\n");
103 audio_out_destroy(output);
108 printf("# play done\n");
111 #define DUMP_FILE "/root/test.raw"
115 static void _audio_out_stream_cb(audio_out_h handle, size_t nbytes, void *user_data)
122 printf("FILE is NULL\n");
126 buf = (char *)malloc(nbytes);
128 printf("malloc(%zu) failed\n", nbytes);
132 read_bytes = fread(buf, 1, nbytes, fp);
134 written = audio_out_write(handle, buf, read_bytes);
135 printf("written : %6d/%6d (requested %zu)\n", written, read_bytes, nbytes);
137 if (read_bytes < nbytes) {
145 static void _play_file_sample_async(char *file, int frequency, int ch, int type)
150 if (ch < 0 || ch > 2)
153 fp = fopen(file, "r");
155 printf("open failed\n");
158 printf("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
159 audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output);
161 audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
162 audio_out_set_stream_cb(output, _audio_out_stream_cb, fp);
163 audio_out_prepare(output);
166 usleep(10000); /* 10ms */
169 printf("FINISHED!!!\n");
171 audio_out_unprepare(output);
172 audio_out_destroy(output);
174 printf("play done\n");
177 static void _play_file_sample_sync(char *file, int frequency, int ch, int type)
187 if (ch < 0 || ch > 2)
190 fp = fopen(file, "r");
192 printf("open failed\n");
196 fseek(fp, 0, SEEK_END);
197 file_size = ftell(fp);
198 fseek(fp, 0, SEEK_SET);
200 printf("Play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
201 audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output);
203 audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
204 audio_out_prepare(output);
206 audio_out_get_buffer_size(output, &buffer_size);
207 buf = (char *)malloc(buffer_size);
209 printf("malloc failed\n");
213 printf("Press any key to continue....\n");
215 printf("Start to read[%d] from file and write : \n", buffer_size);
217 while (file_size > 0) {
218 read_bytes = fread(buf, 1, buffer_size, fp);
227 audio_out_write(output, buf, read_bytes);
228 file_size = file_size - read_bytes;
233 audio_out_unprepare(output);
234 audio_out_destroy(output);
241 printf("\nEOS!!!! Play done\n");
244 static int _record_and_play(int length, int num, int ch)
247 audio_in_h input = NULL;
251 ret = audio_in_create(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &input);
252 if (ret != AUDIO_IO_ERROR_NONE) {
253 printf("audio in create error = 0x%x\n", ret);
257 ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
258 if (ret != AUDIO_IO_ERROR_NONE) {
259 printf("audio_in_set_state_changed_cb failed. \n");
263 ret = audio_in_prepare(input);
264 if (ret != AUDIO_IO_ERROR_NONE) {
265 printf("ERROR, prepare\n");
269 fp = fopen(DUMP_FILE, "wb+");
271 printf("ERROR, file open failed\n");
275 ret = audio_in_get_buffer_size(input, &size);
276 if (ret != AUDIO_IO_ERROR_NONE) {
277 printf("ERROR, prepare\n");
282 buffer = alloca(size);
284 for (i = 0; i < num; i++) {
285 printf("### read = (%d/%d) ============== ", i, num);
286 if ((ret = audio_in_read(input, (void *)buffer, size)) > AUDIO_IO_ERROR_NONE) {
287 fwrite(buffer, size, sizeof(char), fp);
288 printf("PASS, size=%d, ret=0x%x\n", size, ret);
290 printf("FAIL, size=%d, ret=0x%x\n", size, ret);
296 audio_in_unprepare(input);
297 audio_in_destroy(input);
299 _play_file(DUMP_FILE, length, num, ch);
304 audio_in_destroy(input);
310 static int _direct_loopback()
313 audio_in_h input = NULL;
314 audio_out_h output = NULL;
318 ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
319 if (ret != AUDIO_IO_ERROR_NONE) {
320 printf("audio_in_create_ex failed. \n");
323 ret = audio_out_create_new(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
324 if (ret != AUDIO_IO_ERROR_NONE) {
325 printf("audio_out_create_new failed. \n");
329 ret = audio_in_prepare(input);
331 printf("audio_in_prepare failed, err(0x%x)\n", ret);
335 ret = audio_in_get_buffer_size(input, &size);
336 if (ret != AUDIO_IO_ERROR_NONE) {
337 printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
341 printf("size(%d)\n", size);
342 buffer = alloca(size);
344 ret = audio_out_prepare(output);
346 printf("audio_out_prepare failed, err(0x%x)\n", ret);
350 if (buffer == NULL) {
351 printf("buffer is null\n");
356 printf("Start to loopback(read/write) with [16kHz/MONO/S16LE]!!!\n");
359 ret = audio_in_read(input, (void *)buffer, size);
360 if (ret > AUDIO_IO_ERROR_NONE) {
361 ret = audio_out_write(output, buffer, size);
362 if (ret > AUDIO_IO_ERROR_NONE) {
372 printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size);
375 printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size);
382 audio_in_destroy(input);
384 audio_out_destroy(output);
395 sound_stream_info_h g_stream_info_read_h = NULL;
396 sound_stream_info_h g_stream_info_write_h = NULL;
398 static void _focus_cb(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask,
399 sound_stream_focus_state_e focus_state,
400 sound_stream_focus_change_reason_e reason_for_change, int sound_behavior,
401 const char *additional_info, void *user_data)
403 printf("*** focus_callback_read is called, stream_info(%p, read(%p)/write(%p)) ***\n",
404 stream_info, g_stream_info_read_h, g_stream_info_write_h);
405 printf(" - reason_for_change(%d), additional_info(%s), user_data(%p)\n",
406 reason_for_change, additional_info, user_data);
407 printf(" - focus_state is :%d \n", focus_state);
412 static void _audio_io_stream_read_cb(audio_in_h handle, size_t nbytes, void *user_data)
414 const void *buffer = NULL;
415 unsigned int len = (unsigned int)nbytes;
420 audio_in_peek(handle, &buffer, &len);
422 fwrite(buffer, sizeof(char), len, fp_w);
423 audio_in_drop(handle);
426 static void _audio_io_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data)
428 short *buffer = NULL;
434 buffer = (short *)malloc(nbytes);
435 if (buffer == NULL) {
436 printf("malloc failed\n");
439 memset(buffer, 0, nbytes);
441 for (i = 0; i < nbytes / 2; i += 2) {
442 buffer[i] = (short)32768 *test_wav.sine[test_wav.left_channel]; /* left */
443 buffer[i + 1] = (short)32768 *test_wav.sine[test_wav.right_channel]; /* right */
444 test_wav.left_channel += 1;
445 if (test_wav.left_channel >= TABLE_SIZE)
446 test_wav.left_channel -= TABLE_SIZE;
447 test_wav.right_channel += 3;
448 if (test_wav.right_channel >= TABLE_SIZE)
449 test_wav.right_channel -= TABLE_SIZE;
452 audio_out_write(handle, buffer, nbytes);
457 int _convert_cmd_and_run(char cmd, int mode)
463 ret = audio_out_prepare(output);
465 ret = audio_in_prepare(input);
469 ret = audio_out_unprepare(output);
471 ret = audio_in_unprepare(input);
475 ret = audio_out_pause(output);
477 ret = audio_in_pause(input);
481 ret = audio_out_resume(output);
483 ret = audio_in_resume(input);
487 ret = audio_out_drain(output);
491 ret = audio_out_flush(output);
493 ret = audio_in_flush(input);
496 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h);
498 printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
510 int audio_io_async_test(int mode)
519 int write_mode = (mode & 0x01);
520 int read_mode = (mode & 0x02);
522 sound_stream_focus_state_e playback_focus_state;
523 sound_stream_focus_state_e recording_focus_state;
525 if ((write_mode == 0) && (read_mode == 0)) {
526 printf("not vaild mode.\n");
532 printf("audio_in_create\n");
533 ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
534 if (ret != AUDIO_IO_ERROR_NONE) {
535 printf("audio_in_create_ex failed. \n");
538 printf("audio_in_create success!!! [%p]\n", input);
540 ret = audio_in_set_stream_cb(input, _audio_io_stream_read_cb, NULL);
541 if (ret != AUDIO_IO_ERROR_NONE) {
542 printf("audio_in_set_stream_cb failed. \n");
545 printf("audio_in_set_stream_cb success!!! [%p]\n", input);
547 ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
548 if (ret != AUDIO_IO_ERROR_NONE) {
549 printf("audio_out_set_state_changed_cb failed. \n");
552 printf("audio_out_set_state_changed_cb success!!! [%p]\n", input);
554 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_read_h);
556 printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
559 ret = audio_in_set_sound_stream_info(input, g_stream_info_read_h);
561 printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret);
563 ret = sound_manager_acquire_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
565 printf("fail to sound_manager_acquire_focus() for RECORDING, ret(0x%x)\n", ret);
569 fp_w = fopen("/tmp/pcm_w.raw", "w");
573 printf("before audio_out_create_new\n");
576 printf("audio_out_create_new\n");
577 ret = audio_out_create_new(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
578 if (ret != AUDIO_IO_ERROR_NONE) {
579 printf("audio_out_create_new failed. \n");
582 printf("audio_out_create_new success!!! [%p]\n", output);
584 ret = audio_out_set_stream_cb(output, _audio_io_stream_write_cb, NULL);
585 if (ret != AUDIO_IO_ERROR_NONE) {
586 printf("audio_out_set_stream_cb failed. \n");
589 printf("audio_out_set_stream_cb success!!! [%p]\n", output);
591 ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
592 if (ret != AUDIO_IO_ERROR_NONE) {
593 printf("audio_out_set_state_changed_cb failed. \n");
596 printf("audio_out_set_state_changed_cb success!!! [%p]\n", output);
598 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h);
600 printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
604 ret = audio_out_set_sound_stream_info(output, g_stream_info_write_h);
606 printf("fail to audio_out_set_sound_stream_info(), ret(0x%x)\n", ret);
608 ret = sound_manager_acquire_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
610 printf("fail to sound_manager_acquire_focus() for PLAYBACK, ret(0x%x)\n", ret);
614 /* generate wave data */
615 for (i = 0; i < TABLE_SIZE; i++)
616 test_wav.sine[i] = 0.9 * (float)sin(((double)i / (double)TABLE_SIZE) * M_PI * 2.);
617 test_wav.left_channel = test_wav.right_channel = 0;
621 printf("before audio_in_prepare\n");
623 printf("audio_in_prepare\n");
624 ret = audio_in_prepare(input);
626 printf("audio_in_prepare failed, err(0x%x)\n", ret);
627 audio_in_destroy(input);
630 ret = audio_in_get_buffer_size(input, &size);
631 if (ret != AUDIO_IO_ERROR_NONE) {
632 printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
635 printf("size(%d)\n", size);
636 buffer = alloca(size);
640 if (buffer == NULL) {
641 printf("buffer is null\n");
647 printf("before audio_out_prepare\n");
649 printf("audio_out_prepare\n");
650 ret = audio_out_prepare(output);
652 printf("audio_out_prepare failed, err(0x%x)\n", ret);
653 audio_out_destroy(output);
659 printf("command(q:quit) : ");
666 cmd_ret = _convert_cmd_and_run(cmd, mode);
667 printf(" - result code : %d\n", cmd_ret);
668 } while (cmd != 'q');
673 printf("audio_in_unprepare\n");
674 audio_in_unprepare(input);
675 printf("audio_in_destroy\n");
676 audio_in_destroy(input);
685 if (g_stream_info_read_h) {
686 ret = sound_manager_get_focus_state(g_stream_info_read_h, NULL, &recording_focus_state);
687 if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
688 ret = sound_manager_release_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
690 printf("fail to sound_manager_release_focus() for recording, ret(0x%x)\n", ret);
692 ret = sound_manager_destroy_stream_information(g_stream_info_read_h);
694 printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
695 g_stream_info_read_h = NULL;
701 printf("audio_out_unprepare\n");
702 audio_out_unprepare(output);
703 printf("audio_out_destroy\n");
704 audio_out_destroy(output);
707 if (g_stream_info_write_h) {
708 ret = sound_manager_get_focus_state(g_stream_info_write_h, &playback_focus_state, NULL);
709 if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
710 ret = sound_manager_release_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
712 printf("fail to sound_manager_release_focus() for playback, ret(0x%x)\n", ret);
714 ret = sound_manager_destroy_stream_information(g_stream_info_write_h);
716 printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
717 g_stream_info_write_h = NULL;
724 #define THREAD_MAX 10
725 #define TEST_COUNT 500
726 static void *thread_stress_test_audio_in(void *data)
733 audio_sample_type_e type;
734 unsigned int seed = (unsigned int)time(NULL);
736 audio_in_h input = (audio_in_h)data;
738 audio_in_prepare(input);
739 audio_in_get_buffer_size(input, &buffer_size);
740 buf = (char *)malloc(buffer_size);
742 printf("malloc failed\n");
746 for (i=0; i<TEST_COUNT; i++) {
747 switch (rand_r(&seed) % 8) {
749 audio_in_prepare(input);
752 audio_in_read(input, buf, buffer_size);
755 audio_in_pause(input);
758 audio_in_resume(input);
761 audio_in_flush(input);
764 audio_in_unprepare(input);
767 audio_in_prepare(input);
768 for (j = 0; j < 10; j++)
769 audio_in_read(input, buf, buffer_size);
772 audio_in_get_buffer_size(input, &buffer_size);
773 audio_in_get_sample_rate(input, &rate);
774 audio_in_get_channel(input, &ch);
775 audio_in_get_sample_type(input, &type);
780 if ((i % (TEST_COUNT/10)) == 0)
781 printf("audio_in: %x thread. count(%d/%d)\n", (int)pthread_self(), i, TEST_COUNT);
783 audio_in_unprepare(input);
794 static void *thread_stress_test_audio_out(void *data)
801 audio_sample_type_e type;
802 unsigned int seed = (unsigned int)time(NULL);
804 audio_out_h output = (audio_out_h)data;
808 audio_out_get_buffer_size(output, &buffer_size);
810 buf = (char *)malloc(buffer_size);
812 printf("malloc failed\n");
816 audio_out_prepare(output);
817 for (i = 0; i < TEST_COUNT; i++) {
818 switch (rand_r(&seed) % 9) {
820 audio_out_prepare(output);
823 audio_out_write(output, buf, buffer_size);
826 audio_out_pause(output);
829 audio_out_resume(output);
832 audio_out_drain(output);
835 audio_out_flush(output);
838 audio_out_write(output, buf, buffer_size);
841 audio_out_prepare(output);
842 for (j = 0; j < 10; j++)
843 audio_out_write(output, buf, buffer_size);
846 audio_out_get_buffer_size(output, &buffer_size);
847 audio_out_get_sample_rate(output, &rate);
848 audio_out_get_channel(output, &ch);
849 audio_out_get_sample_type(output, &type);
854 if ((i % (TEST_COUNT/10)) == 0)
855 printf("audio_out: %x thread. count(%d/%d)\n", (int)pthread_self(), i, TEST_COUNT);
857 audio_out_unprepare(output);
866 #define OUT_HANDLE_MAX 1000
867 #define IN_HANDLE_MAX 10
868 void audio_io_test_handle_max()
870 audio_out_h output[OUT_HANDLE_MAX] = { 0, };
871 audio_in_h input[IN_HANDLE_MAX] = { 0, };
876 printf("==============================================\n");
877 printf("playback handle creation test. try to create %d handles\n", OUT_HANDLE_MAX);
878 for (i = 0; i < OUT_HANDLE_MAX; i++) {
879 ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[i]);
880 if (ret != AUDIO_IO_ERROR_NONE) {
881 printf("audio_out_create_new failed.\n");
885 ret = audio_out_prepare(output[i]);
886 if (ret == AUDIO_IO_ERROR_NONE)
889 printf("created handle (%d/%d)\n", success, OUT_HANDLE_MAX);
891 for (i = 0; i < OUT_HANDLE_MAX; i++)
892 audio_out_destroy(output[i]);
894 /* pulseaudio oom when IN_HANDLE_MAX is set over 50 */
895 printf("capture handle creation test. try to create %d handles\n", IN_HANDLE_MAX);
897 for (i = 0; i < IN_HANDLE_MAX; i++) {
898 ret = audio_in_create(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input[i]);
899 if (ret != AUDIO_IO_ERROR_NONE) {
900 printf("audio_in_create_new failed.\n");
904 ret = audio_in_prepare(input[i]);
905 if (ret == AUDIO_IO_ERROR_NONE)
908 printf("created handle (%d/%d)\n", success, IN_HANDLE_MAX);
910 for (i = 0; i < IN_HANDLE_MAX; i++)
911 audio_in_destroy(input[i]);
914 int audio_io_test_read_write()
918 audio_out_h output[2];
922 pthread_t t[THREAD_MAX];
924 void out_stream_cb(audio_out_h handle, size_t nbytes, void *user_data) {
925 char * buf = (char *)malloc(nbytes);
927 printf("malloc(%zu) failed\n", nbytes);
930 audio_out_write(handle, buf, nbytes);
935 void in_stream_cb(audio_in_h handle, size_t nbytes, void *user_data) {
937 unsigned int size = nbytes;
939 audio_in_peek(handle, (const void **)buf, &size);
940 audio_in_drop(handle);
944 void state_changed_cb(audio_out_h handle, audio_io_state_e previous, audio_io_state_e current, bool by_policy, void *user_data) {
949 ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[0]);
950 if (ret != AUDIO_IO_ERROR_NONE) {
951 printf("audio_out_create_new failed. \n");
954 audio_out_set_state_changed_cb(output[0], state_changed_cb, NULL);
956 /* audio_out async */
957 ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[1]);
958 if (ret != AUDIO_IO_ERROR_NONE) {
959 printf("audio_out_create_new failed. \n");
962 audio_out_set_stream_cb(output[1], out_stream_cb, NULL);
963 audio_out_set_state_changed_cb(output[1], state_changed_cb, NULL);
966 ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
967 if (ret != AUDIO_IO_ERROR_NONE) {
968 printf("audio in create error = 0x%x\n", ret);
971 audio_in_set_stream_cb(input, in_stream_cb, NULL);
973 printf("==============================================\n");
974 printf("audio out sync test\n");
975 for (i = 0; i < THREAD_MAX; i++)
976 pthread_create(&t[i], NULL, thread_stress_test_audio_out, output[0]);
978 for (i = 0; i < THREAD_MAX; i++) {
979 pthread_join(t[i], (void**)&status);
980 printf("thread %d finished\n", i);
983 printf("==============================================\n");
984 printf("audio out async test\n");
985 for (i = 0; i < THREAD_MAX; i++)
986 pthread_create(&t[i], NULL, thread_stress_test_audio_out, output[1]);
988 for (i = 0; i < THREAD_MAX; i++) {
989 pthread_join(t[i], (void**)&status);
990 printf("thread %d finished\n", i);
993 printf("==============================================\n");
994 printf("audio in test\n");
995 for (i = 0; i < THREAD_MAX; i++)
996 pthread_create(&t[i], NULL, thread_stress_test_audio_in, input);
998 for (i = 0; i < THREAD_MAX; i++) {
999 pthread_join(t[i], (void**)&status);
1000 printf("thread %d finished\n", i);
1003 audio_out_destroy(output[0]);
1004 audio_out_destroy(output[1]);
1005 audio_in_destroy(input);
1007 printf("stress test end\n");
1012 int main(int argc, char **argv)
1014 setbuf(stdout, NULL);
1016 if (argc == 2 && !strcmp(argv[1], "loopback")) {
1018 } else if (argc == 3 && !strcmp(argv[1], "async")) {
1019 audio_io_async_test(atoi(argv[2]));
1020 } else if (argc == 2 && !strcmp(argv[1], "stress")) {
1021 audio_io_test_handle_max();
1022 audio_io_test_read_write();
1023 } else if (argc == 4) {
1024 int channel_idx = atoi(argv[3]);
1025 if (channel_idx < 0 || channel_idx > 8) {
1026 printf("Invalid channel\n");
1029 printf("run with [%s][%s][%s]\n", argv[1], argv[2], argv[3]);
1030 _record_and_play(atoi(argv[1]), atoi(argv[2]), channel_idx);
1031 } else if (argc == 6) {
1032 if (strcmp(argv[1], "play") == 0)
1033 _play_file_sample_sync(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
1034 else if (strcmp(argv[1], "playasync") == 0)
1035 _play_file_sample_async(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
1037 printf("- Usages :\n");
1038 printf("- # audio_io_test loopback\n");
1039 printf("- # audio_io_test stress\n");
1040 printf("- # audio_io_test [length to read] [number of iteration] [channels]\n");
1041 printf("- # audio_io_test async [write(1) | read(2)]\n");
1042 printf("- # audio_io_test play [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n");
1043 printf("- # audio_io_test playasync [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n");