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>
25 //#define _NEW_SOUND_MANAGER_API_
26 #define _SESSION_SOUND_MANAGER_API_
29 #define M_PI (3.14159265)
32 #define TABLE_SIZE (200)
35 float sine[TABLE_SIZE];
41 static int ch_table[3] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO };
43 void play_file(char *file, int length, int ch)
46 FILE* fp = fopen (file, "r");
48 printf ("fopen failed\n");
52 char * buf = malloc (length);
54 printf ("malloc failed\n");
59 printf ("start to play [%s][%d][%d]\n", file, length, ch);
60 //audio_out_create(44100, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output);
61 audio_out_create_new(44100, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, &output);
62 if (fread (buf, 1, length, fp) != length) {
63 printf ("error!!!!\n");
66 audio_out_prepare(output);
67 audio_out_write(output, buf, length);
68 audio_out_unprepare(output);
70 audio_out_destroy (output);
74 printf ("play done\n");
77 #define DUMP_FILE "/root/test.raw"
80 void play_file_sample(char *file, int frequency, int ch, int type)
88 if(ch < 0 || ch > 2) {
92 FILE* fp = fopen (file, "r");
94 printf("open failed\n");
98 fseek(fp, 0, SEEK_END);
99 file_size = ftell(fp);
100 fseek(fp, 0, SEEK_SET);
102 printf ("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
104 //audio_out_create(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output);
105 audio_out_create_new(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, &output);
107 //audio_out_create(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_U8, SOUND_TYPE_MEDIA, &output);
108 audio_out_create_new(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_U8, &output);
110 audio_out_prepare(output);
111 audio_out_get_buffer_size(output, &buffer_size);
113 buf = (char *) malloc(buffer_size);
115 printf ("malloc failed\n");
116 audio_out_unprepare(output);
117 audio_out_destroy(output);
121 //audio_out_prepare(output);
123 while (file_size > 0) {
124 read_bytes = fread (buf, 1, buffer_size, fp);
125 printf ("Read %d Requested - %d\n", read_bytes, buffer_size);
126 audio_out_write(output, buf, read_bytes);
127 file_size = file_size - read_bytes;
130 audio_out_unprepare(output);
131 audio_out_destroy (output);
135 printf ("play done\n");
138 int audio_io_test(int length, int num, int ch)
142 if ((ret = audio_in_create(44100, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, &input)) == AUDIO_IO_ERROR_NONE) {
143 ret = audio_in_ignore_session(input);
145 printf ("ERROR, set session mix\n");
146 audio_in_destroy(input);
150 ret = audio_in_prepare(input);
152 printf ("ERROR, prepare\n");
153 audio_in_destroy(input);
157 FILE* fp = fopen (DUMP_FILE, "wb+");
160 printf ("ERROR, file open failed\n");
161 audio_in_destroy(input);
165 if ((ret = audio_in_get_buffer_size(input, &size)) == AUDIO_IO_ERROR_NONE) {
167 char *buffer = alloca(size);
169 for (i=0; i<num; i++) {
170 printf ("### loop = %d ============== \n", i);
171 if ((ret = audio_in_read(input, (void*)buffer, size)) > AUDIO_IO_ERROR_NONE) {
172 fwrite (buffer, size, sizeof(char), fp);
173 printf ("PASS, size=%d, ret=%d\n", size, ret);
175 printf ("FAIL, size=%d, ret=%d\n", size, ret);
182 audio_in_destroy(input);
185 play_file (DUMP_FILE, length*num, ch);
190 int audio_io_loopback_in_test()
194 FILE* fp = fopen ("/tmp/dump_test.raw", "wb+");
197 printf("open failed \n");
201 if ((ret = audio_in_create(16000, AUDIO_CHANNEL_MONO , AUDIO_SAMPLE_TYPE_S16_LE, &input)) == AUDIO_IO_ERROR_NONE) {
202 ret = audio_in_ignore_session(input);
204 printf ("ERROR, set session mix\n");
208 ret = audio_in_prepare(input);
210 printf ("ERROR, prepare\n");
214 ret = audio_in_get_buffer_size(input, &size);
215 if(ret != AUDIO_IO_ERROR_NONE) {
216 printf ("audio_in_get_buffer_size failed.\n");
221 char *buffer = alloca(size);
222 if ((ret = audio_in_read(input, (void*)buffer, size)) > AUDIO_IO_ERROR_NONE) {
223 fwrite (buffer, size, sizeof(char), fp);
224 printf ("PASS, size=%d, ret=%d\n", size, ret);
226 printf ("FAIL, size=%d, ret=%d\n", size, ret);
232 audio_in_destroy(input);
240 int audio_io_loopback_test()
247 ret = audio_in_create(16000, AUDIO_CHANNEL_MONO , AUDIO_SAMPLE_TYPE_S16_LE, &input);
248 if(ret != AUDIO_IO_ERROR_NONE) {
249 printf ("audio_in_create_ex failed. \n");
253 //ret = audio_out_create(16000, AUDIO_CHANNEL_MONO , AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_CALL, &output);
254 ret = audio_out_create_new(16000, AUDIO_CHANNEL_MONO , AUDIO_SAMPLE_TYPE_S16_LE, &output);
255 if(ret != AUDIO_IO_ERROR_NONE) {
256 printf ("audio_out_create failed. \n");
260 ret = audio_in_prepare(input);
262 printf ("audio_in_prepare failed.\n");
263 audio_in_destroy(input);
266 ret = audio_in_get_buffer_size(input, &size);
267 if(ret != AUDIO_IO_ERROR_NONE) {
268 printf ("audio_in_get_buffer_size failed.\n");
271 printf("size(%d)\n", size);
272 buffer = alloca(size);
276 ret = audio_out_prepare(output);
278 printf ("audio_out_prepare failed.\n");
279 audio_out_destroy(output);
284 printf("buffer is null\n");
289 ret = audio_in_read(input, (void*)buffer, size);
290 if(ret > AUDIO_IO_ERROR_NONE) {
291 ret = audio_out_write(output, buffer, size);
292 if(ret > AUDIO_IO_ERROR_NONE) {
293 printf("audio read/write success. buffer(%p), size(%d)\n", buffer, size);
295 printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size);
298 printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size);
308 #ifdef _NEW_SOUND_MANAGER_API_
309 sound_stream_info_h g_stream_info_read_h = NULL;
310 sound_stream_info_h g_stream_info_write_h = NULL;
312 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) {
314 sound_stream_focus_state_e playback_focus_state;
315 sound_stream_focus_state_e recording_focus_state;
316 printf ("*** focus_callback_read is called, stream_info(%p) ***\n", stream_info);
317 printf (" - reason_for_change(%d), additional_info(%s), user_data(%p)\n", reason_for_change, additional_info, user_data);
318 ret = sound_manager_get_focus_state (stream_info, &playback_focus_state, &recording_focus_state);
320 printf (" - focus_state(playback_focus:%d, recording_focus:%d)\n", playback_focus_state, recording_focus_state);
321 if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
322 printf (" -- PLAYBACK_FOCUS acquired\n");
324 if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
325 printf (" -- FOCUS_RECORDING acquired\n");
327 printf ("*** focus_callback_read is ended, stream_info(%p) ****\n", stream_info);
331 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) {
333 sound_stream_focus_state_e playback_focus_state;
334 sound_stream_focus_state_e recording_focus_state;
335 printf ("*** focus_callback_write is called, stream_info(%p) ***\n", stream_info);
336 printf (" - reason_for_change(%d), additional_info(%s), user_data(%p)\n", reason_for_change, additional_info, user_data);
337 ret = sound_manager_get_focus_state (stream_info, &playback_focus_state, &recording_focus_state);
339 printf (" - focus_state(playback_focus:%d, recording_focus:%d)\n", playback_focus_state, recording_focus_state);
340 if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
341 printf (" -- PLAYBACK_FOCUS acquired\n");
343 if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
344 printf (" -- FOCUS_RECORDING acquired\n");
346 printf ("*** focus_callback_write is ended, stream_info(%p) ****\n", stream_info);
351 static void interrupted_callback_read(audio_io_interrupted_code_e code, void *user_data) {
352 printf ("*** interrupted_callback_read is called, code(%d), user_data(%p) ***\n", code, user_data);
355 static void interrupted_callback_write(audio_io_interrupted_code_e code, void *user_data) {
356 printf ("*** interrupted_callback_write is called, code(%d), user_data(%p) ***\n", code, user_data);
359 static void _audio_io_stream_read_cb (audio_in_h handle, size_t nbytes, void *user_data)
361 const void * buffer = NULL;
363 // printf("_audio_io_stream_read_cb : handle=%p, nbytes=%d, user_data=%p\n", handle, nbytes, user_data);
366 audio_in_peek (handle, &buffer, &nbytes);
368 fwrite(buffer, sizeof(char), nbytes, fp_w);
370 audio_in_drop (handle);
374 static void _audio_io_stream_write_cb (audio_out_h handle, size_t nbytes, void *user_data)
376 short* buffer = NULL;
380 // printf("_audio_io_stream_write_cb : handle=%p, nbytes=%d, user_data=%p\n", handle, nbytes, user_data);
383 buffer = (short *) malloc(nbytes);
384 if (buffer == NULL) {
385 printf ("malloc failed\n");
388 memset (buffer, 0, nbytes);
390 for(i=0; i<nbytes/2; i+=2)
392 buffer[i] = (short) 32768 * test_wav.sine[test_wav.left_channel]; /* left */
393 buffer[i+1] = (short) 32768 * test_wav.sine[test_wav.right_channel]; /* right */
394 test_wav.left_channel += 1;
395 if( test_wav.left_channel >= TABLE_SIZE ) test_wav.left_channel -= TABLE_SIZE;
396 test_wav.right_channel += 3;
397 if( test_wav.right_channel >= TABLE_SIZE ) test_wav.right_channel -= TABLE_SIZE;
400 ret = audio_out_write(handle, buffer, nbytes);
401 if(ret > AUDIO_IO_ERROR_NONE) {
402 // printf("audio write success. buffer(%p), nbytes(%d)\n", buffer, nbytes);
409 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)
411 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);
414 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)
416 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);
419 int _convert_cmd_and_run(char cmd, int mode) {
423 if(mode & 0x01) ret = audio_out_prepare(output);
424 if(mode & 0x02) ret = audio_in_prepare(input);
427 if(mode & 0x01) ret = audio_out_unprepare(output);
428 if(mode & 0x02) ret = audio_in_unprepare(input);
431 if(mode & 0x01) ret = audio_out_pause(output);
432 if(mode & 0x02) ret = audio_in_pause(input);
435 if(mode & 0x01) ret = audio_out_resume(output);
436 if(mode & 0x02) ret = audio_in_resume(input);
439 if(mode & 0x01) ret = audio_out_drain(output);
440 //if(mode & 0x02) ret = audio_in_drain(input);
443 if(mode & 0x01) ret = audio_out_flush(output);
444 if(mode & 0x02) ret = audio_in_flush(input);
447 #ifdef _NEW_SOUND_MANAGER_API_
448 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_write, NULL, &g_stream_info_write_h);
450 printf ("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
453 #ifdef _SESSION_SOUND_MANAGER_API_
454 ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
456 printf ("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
458 ret = sound_manager_set_media_session_option (SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
460 printf ("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
474 int audio_io_async_test(int mode)
483 int write_mode = (mode & 0x01);
484 int read_mode = (mode & 0x02);
486 if((write_mode == 0) && (read_mode == 0)) {
487 printf ("not vaild mode.\n");
493 #ifdef _SESSION_SOUND_MANAGER_API_
494 printf ("set session for capture.\n");
496 ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
498 printf ("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
501 ret = sound_manager_set_media_session_option (SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
503 printf ("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
507 printf ("audio_in_create\n");
508 ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO , AUDIO_SAMPLE_TYPE_S16_LE, &input);
509 if(ret != AUDIO_IO_ERROR_NONE) {
510 printf ("audio_in_create_ex failed. \n");
513 printf ("audio_in_create success!!! [%p]\n", input);
515 ret = audio_in_set_stream_cb(input, _audio_io_stream_read_cb, NULL);
516 if(ret != AUDIO_IO_ERROR_NONE) {
517 printf ("audio_in_set_stream_cb failed. \n");
520 printf ("audio_in_set_stream_cb success!!! [%p]\n", input);
522 ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
523 if(ret != AUDIO_IO_ERROR_NONE) {
524 printf ("audio_out_set_state_changed_cb failed. \n");
527 printf ("audio_out_set_state_changed_cb success!!! [%p]\n", input);
529 fp_w = fopen( "/tmp/pcm_w.raw", "w");
531 #ifdef _NEW_SOUND_MANAGER_API_
532 //set stream type as SOUND_STREAM_TYPE_MEDIA
533 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_read, NULL, &g_stream_info_read_h);
535 printf ("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
537 ret = audio_in_set_stream_info(input, g_stream_info_read_h);
540 ret = audio_in_set_interrupted_cb(input, interrupted_callback_read, NULL);
544 printf ("before audio_out_create\n");
547 #ifdef _SESSION_SOUND_MANAGER_API_
548 printf ("set session for playback.\n");
550 ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
552 printf ("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
555 ret = sound_manager_set_media_session_option (SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
557 printf ("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
561 printf ("audio_out_create\n");
562 //ret = audio_out_create(44100, AUDIO_CHANNEL_STEREO , AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output);
563 ret = audio_out_create_new(44100, AUDIO_CHANNEL_STEREO , AUDIO_SAMPLE_TYPE_S16_LE, &output);
564 if(ret != AUDIO_IO_ERROR_NONE) {
565 printf ("audio_out_create failed. \n");
568 printf ("audio_out_create success!!! [%p]\n", output);
570 ret = audio_out_set_stream_cb(output, _audio_io_stream_write_cb, NULL);
571 if(ret != AUDIO_IO_ERROR_NONE) {
572 printf ("audio_out_set_stream_cb failed. \n");
575 printf ("audio_out_set_stream_cb success!!! [%p]\n", output);
577 ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
578 if(ret != AUDIO_IO_ERROR_NONE) {
579 printf ("audio_out_set_state_changed_cb failed. \n");
582 printf ("audio_out_set_state_changed_cb success!!! [%p]\n", output);
584 #ifdef _NEW_SOUND_MANAGER_API_
585 //set stream type as SOUND_STREAM_TYPE_MEDIA
586 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_write, NULL, &g_stream_info_write_h);
588 printf ("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
590 ret = audio_out_set_stream_info(output, g_stream_info_write_h);
593 ret = audio_out_set_interrupted_cb(output, interrupted_callback_write, NULL);
596 for(i=0; i<TABLE_SIZE; i++) {
597 test_wav.sine[i] = 0.9 * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
599 test_wav.left_channel = test_wav.right_channel = 0;
603 printf ("before audio_in_prepare\n");
605 printf ("audio_in_prepare\n");
606 ret = audio_in_prepare(input);
608 printf ("audio_in_prepare failed.\n");
609 audio_in_destroy(input);
612 ret = audio_in_get_buffer_size(input, &size);
613 if(ret != AUDIO_IO_ERROR_NONE) {
614 printf ("audio_in_get_buffer_size failed.\n");
617 printf("size(%d)\n", size);
618 buffer = alloca(size);
623 printf("buffer is null\n");
629 printf ("before audio_out_prepare\n");
631 printf ("audio_out_prepare\n");
632 ret = audio_out_prepare(output);
634 printf ("audio_out_prepare failed.\n");
635 audio_out_destroy(output);
641 printf ("command(q:quit) : ");
642 cmd = (char) getchar();
643 if(cmd != '\n') getchar();
644 cmd_ret = _convert_cmd_and_run(cmd, mode);
645 printf (" - result code : %d\n", cmd_ret);
646 } while (cmd != 'q');
648 //printf ("loop start\n");
649 //for (i=0; i<10; i++) {
650 // printf ("-------- %d -------\n",i);
657 printf ("audio_in_unprepare\n");
658 audio_in_unprepare(input);
659 printf ("audio_in_destroy\n");
660 audio_in_destroy(input);
665 #ifdef _NEW_SOUND_MANAGER_API_
666 ret = sound_manager_destroy_stream_information(g_stream_info_read_h);
668 printf ("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
670 g_stream_info_read_h = NULL;
678 printf ("audio_out_unprepare\n");
679 audio_out_unprepare(output);
680 printf ("audio_out_destroy\n");
681 audio_out_destroy(output);
683 #ifdef _NEW_SOUND_MANAGER_API_
684 ret = sound_manager_destroy_stream_information(g_stream_info_write_h);
686 printf ("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
688 g_stream_info_write_h = NULL;
696 int main(int argc, char ** argv)
698 if ( argc == 2 && !strcmp(argv[1],"call-forwarding-loop")) {
699 audio_io_loopback_test();
700 } else if ( argc == 2 && !strcmp(argv[1],"call-forwarding-in")) {
701 audio_io_loopback_in_test();
702 } else if ( argc == 3 && !strcmp(argv[1],"async")) {
703 audio_io_async_test(atoi(argv[2]));
704 } else if (argc == 4) {
705 printf ("run with [%s][%s][%s]\n", argv[1],argv[2],argv[3]);
706 audio_io_test(atoi (argv[1]), atoi (argv[2]), atoi(argv[3]));
707 } else if (argc == 6) {
708 play_file_sample(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
710 printf ("1. usage : audio_io_test call-forwarding-loop\n");
711 printf ("2. usage : audio_io_test call-forwarding-in\n");
712 printf ("3. usage : audio_io_test [length to read] [number of iteration] [channels]\n");
713 printf ("4. usage : audio_io_test async [write(1) | read(2)]\n");
714 printf ("5. Uasge : audio_io_test play [filename] [sample rate] [channels] [type(0:U8)]\n");