apply new version of audio-io for tizen 3.0
[platform/core/api/audio-io.git] / test / audio_io_test.c
1 /*
2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <math.h>
22 #include <audio_io.h>
23 #include <sound_manager.h>
24
25 //#define _NEW_SOUND_MANAGER_API_
26 #define _SESSION_SOUND_MANAGER_API_
27
28 #ifndef M_PI
29 #define M_PI  (3.14159265)
30 #endif
31
32 #define TABLE_SIZE   (200)
33 typedef struct
34 {
35     float sine[TABLE_SIZE];
36     int left_channel;
37     int right_channel;
38 } test_wav_t;
39 test_wav_t test_wav;
40
41 static int ch_table[3] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO };
42
43 void play_file(char *file, int length, int ch)
44 {
45     audio_out_h output;
46     FILE* fp = fopen (file, "r");
47     if (fp == NULL) {
48         printf ("fopen failed\n");
49         return;
50     }
51
52     char * buf = malloc (length);
53     if (buf == NULL) {
54         printf ("malloc failed\n");
55         fclose (fp);
56         return;
57     }
58
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");
64     }
65
66     audio_out_prepare(output);
67     audio_out_write(output, buf, length);
68     audio_out_unprepare(output);
69
70     audio_out_destroy (output);
71
72     fclose (fp);
73
74     printf ("play done\n");
75 }
76
77 #define DUMP_FILE "/root/test.raw"
78
79
80 void play_file_sample(char *file, int frequency, int ch, int type)
81 {
82     audio_out_h output;
83     int file_size = 0;
84     int read_bytes = 0;
85     int buffer_size = 0;
86     char * buf = NULL;
87
88     if(ch < 0 || ch > 2) {
89         ch = 0;
90     }
91
92     FILE* fp = fopen (file, "r");
93     if (fp == NULL) {
94         printf("open failed\n");
95         return;
96     }
97     /*Get the size*/
98     fseek(fp, 0, SEEK_END);
99     file_size = ftell(fp);
100     fseek(fp, 0, SEEK_SET);
101
102     printf ("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
103     if (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);
106     } else {
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);
109     }
110     audio_out_prepare(output);
111     audio_out_get_buffer_size(output, &buffer_size);
112
113     buf = (char *) malloc(buffer_size);
114     if (buf == NULL) {
115         printf ("malloc failed\n");
116         audio_out_unprepare(output);
117         audio_out_destroy(output);
118         fclose (fp);
119         return;
120     }
121     //audio_out_prepare(output);
122
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;
128     }
129
130     audio_out_unprepare(output);
131     audio_out_destroy (output);
132
133     free(buf);
134     fclose (fp);
135     printf ("play done\n");
136 }
137
138 int audio_io_test(int length, int num, int ch)
139 {
140     int ret, size, i;
141     audio_in_h input;
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);
144         if (ret != 0) {
145             printf ("ERROR, set session mix\n");
146             audio_in_destroy(input);
147             return 0;
148         }
149
150         ret = audio_in_prepare(input);
151         if (ret != 0) {
152             printf ("ERROR, prepare\n");
153             audio_in_destroy(input);
154             return 0;
155         }
156
157         FILE* fp = fopen (DUMP_FILE, "wb+");
158
159         if (fp == NULL) {
160             printf ("ERROR, file open failed\n");
161             audio_in_destroy(input);
162             return 0;
163         }
164
165         if ((ret = audio_in_get_buffer_size(input, &size)) == AUDIO_IO_ERROR_NONE) {
166             size = length;
167             char *buffer = alloca(size);
168
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);
174                 } else {
175                     printf ("FAIL, size=%d, ret=%d\n", size, ret);
176                 }
177             }
178         }
179
180         fclose (fp);
181
182         audio_in_destroy(input);
183     }
184
185     play_file (DUMP_FILE, length*num, ch);
186
187     return 1;
188 }
189
190 int audio_io_loopback_in_test()
191 {
192     int ret, size;
193     audio_in_h input;
194     FILE* fp = fopen ("/tmp/dump_test.raw", "wb+");
195
196     if(fp == NULL) {
197         printf("open failed \n");
198         return 0;
199     }
200
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);
203         if (ret != 0) {
204             printf ("ERROR, set session mix\n");
205             goto exit;
206         }
207
208         ret = audio_in_prepare(input);
209         if (ret != 0) {
210             printf ("ERROR, prepare\n");
211             goto exit;
212         }
213
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");
217             goto exit;
218         }
219
220         while(1) {
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);
225             } else {
226                 printf ("FAIL, size=%d, ret=%d\n", size, ret);
227             }
228         }
229     }
230
231 exit:
232     audio_in_destroy(input);
233
234     fclose (fp);
235
236     return ret;
237
238 }
239
240 int audio_io_loopback_test()
241 {
242     int ret, size;
243     audio_in_h input;
244     audio_out_h output;
245     char *buffer = NULL;
246
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");
250         return 0;
251     }
252
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");
257         return 0;
258     }
259
260     ret = audio_in_prepare(input);
261     if (ret != 0) {
262         printf ("audio_in_prepare failed.\n");
263         audio_in_destroy(input);
264         return 0;
265     } else {
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");
269             return 0;
270         } else {
271             printf("size(%d)\n", size);
272             buffer = alloca(size);
273         }
274     }
275
276     ret = audio_out_prepare(output);
277     if (ret != 0) {
278         printf ("audio_out_prepare failed.\n");
279         audio_out_destroy(output);
280         return 0;
281     }
282
283     if(buffer == NULL) {
284         printf("buffer is null\n");
285         return 0;
286     }
287
288     while(1) {
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);
294             } else {
295                 printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size);
296             }
297         } else
298             printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size);
299     }
300
301 }
302
303 audio_in_h input;
304 audio_out_h output;
305
306 FILE* fp_w = NULL;
307
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;
311
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) {
313     int ret = 0;
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);
319     if (!ret)
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");
323     }
324     if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
325         printf (" -- FOCUS_RECORDING acquired\n");
326     }
327     printf ("*** focus_callback_read is ended, stream_info(%p) ****\n", stream_info);
328     return;
329 }
330
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) {
332     int ret = 0;
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);
338     if (!ret)
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");
342     }
343     if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
344         printf (" -- FOCUS_RECORDING acquired\n");
345     }
346     printf ("*** focus_callback_write is ended, stream_info(%p) ****\n", stream_info);
347     return;
348 }
349 #endif
350
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);
353 }
354
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);
357 }
358
359 static void _audio_io_stream_read_cb (audio_in_h handle, size_t nbytes, void *user_data)
360 {
361     const void * buffer = NULL;
362
363 //    printf("_audio_io_stream_read_cb : handle=%p, nbytes=%d, user_data=%p\n", handle, nbytes, user_data);
364
365     if (nbytes > 0) {
366         audio_in_peek (handle, &buffer, &nbytes);
367         if (fp_w) {
368             fwrite(buffer, sizeof(char), nbytes, fp_w);
369         }
370         audio_in_drop (handle);
371     }
372 }
373
374 static void _audio_io_stream_write_cb (audio_out_h handle, size_t nbytes, void *user_data)
375 {
376     short* buffer = NULL;
377     int ret = 0;
378     int i = 0;
379
380 //    printf("_audio_io_stream_write_cb : handle=%p, nbytes=%d, user_data=%p\n", handle, nbytes, user_data);
381
382     if (nbytes > 0) {
383         buffer = (short *) malloc(nbytes);
384         if (buffer == NULL) {
385             printf ("malloc failed\n");
386             return;
387         }
388         memset (buffer, 0, nbytes);
389
390         for(i=0; i<nbytes/2; i+=2)
391         {
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;
398         }
399
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);
403         }
404
405         free (buffer);
406     }
407 }
408
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)
410 {
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);
412 }
413
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)
415 {
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);
417 }
418
419 int _convert_cmd_and_run(char cmd, int mode) {
420     int ret = 0;
421     switch (cmd) {
422         case 'P':
423             if(mode & 0x01)  ret = audio_out_prepare(output);
424             if(mode & 0x02)  ret = audio_in_prepare(input);
425             break;
426         case 'u':
427             if(mode & 0x01)  ret = audio_out_unprepare(output);
428             if(mode & 0x02)  ret = audio_in_unprepare(input);
429             break;
430         case 'p':
431             if(mode & 0x01)  ret = audio_out_pause(output);
432             if(mode & 0x02)  ret = audio_in_pause(input);
433             break;
434         case 'r':
435             if(mode & 0x01)  ret = audio_out_resume(output);
436             if(mode & 0x02)  ret = audio_in_resume(input);
437             break;
438         case 'd':
439             if(mode & 0x01)  ret = audio_out_drain(output);
440             //if(mode & 0x02)  ret = audio_in_drain(input);
441             break;
442         case 'f':
443             if(mode & 0x01)  ret = audio_out_flush(output);
444             if(mode & 0x02)  ret = audio_in_flush(input);
445             break;
446         case 'i':
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);
449             if (ret) {
450                 printf ("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
451             }
452 #endif
453 #ifdef _SESSION_SOUND_MANAGER_API_
454             ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
455             if (ret) {
456                 printf ("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
457             }
458             ret = sound_manager_set_media_session_option (SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
459             if (ret) {
460                 printf ("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
461             }
462 #endif
463             break;
464         case 'q':  /* quit */
465             ret = 1;
466             break;
467         default:
468             ret = 1;
469             break;
470     }
471     return ret;
472 }
473
474 int audio_io_async_test(int mode)
475 {
476     int ret, size;
477     char *buffer = NULL;
478     int i=0;
479
480     char cmd = 0;
481     int cmd_ret;
482
483     int write_mode = (mode & 0x01);
484     int read_mode = (mode & 0x02);
485
486     if((write_mode == 0) && (read_mode == 0)) {
487         printf ("not vaild mode.\n");
488         return 0;
489     }
490
491     if (read_mode) {
492
493 #ifdef _SESSION_SOUND_MANAGER_API_
494         printf ("set session for capture.\n");
495
496         ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
497         if (ret) {
498             printf ("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
499         }
500
501         ret = sound_manager_set_media_session_option (SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
502         if (ret) {
503             printf ("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
504         }
505 #endif
506
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");
511             return 0;
512         }
513         printf ("audio_in_create success!!! [%p]\n", input);
514
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");
518             return 0;
519         }
520         printf ("audio_in_set_stream_cb success!!! [%p]\n", input);
521
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");
525             return 0;
526         }
527         printf ("audio_out_set_state_changed_cb success!!! [%p]\n", input);
528
529         fp_w = fopen( "/tmp/pcm_w.raw", "w");
530
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);
534         if (ret) {
535             printf ("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
536         }
537         ret = audio_in_set_stream_info(input, g_stream_info_read_h);
538 #endif
539
540         ret = audio_in_set_interrupted_cb(input, interrupted_callback_read, NULL);
541     }
542
543     if (write_mode) {
544         printf ("before audio_out_create\n");
545         getchar();
546
547 #ifdef _SESSION_SOUND_MANAGER_API_
548         printf ("set session for playback.\n");
549
550         ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
551         if (ret) {
552             printf ("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
553         }
554
555         ret = sound_manager_set_media_session_option (SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
556         if (ret) {
557             printf ("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
558         }
559 #endif
560
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");
566             return 0;
567         }
568         printf ("audio_out_create success!!! [%p]\n", output);
569
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");
573             return 0;
574         }
575         printf ("audio_out_set_stream_cb success!!! [%p]\n", output);
576
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");
580             return 0;
581         }
582         printf ("audio_out_set_state_changed_cb success!!! [%p]\n", output);
583
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);
587         if (ret) {
588             printf ("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
589         }
590         ret = audio_out_set_stream_info(output, g_stream_info_write_h);
591 #endif
592
593         ret = audio_out_set_interrupted_cb(output, interrupted_callback_write, NULL);
594
595         //generate wave data
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. );
598         }
599         test_wav.left_channel = test_wav.right_channel = 0;
600     }
601
602     if (read_mode) {
603         printf ("before audio_in_prepare\n");
604         getchar();
605         printf ("audio_in_prepare\n");
606         ret = audio_in_prepare(input);
607         if (ret != 0) {
608             printf ("audio_in_prepare failed.\n");
609             audio_in_destroy(input);
610             return 0;
611         } else {
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");
615                 return 0;
616             } else {
617                 printf("size(%d)\n", size);
618                 buffer = alloca(size);
619             }
620         }
621
622         if(buffer == NULL) {
623             printf("buffer is null\n");
624             return 0;
625         }
626     }
627
628     if (write_mode) {
629         printf ("before audio_out_prepare\n");
630         getchar();
631         printf ("audio_out_prepare\n");
632         ret = audio_out_prepare(output);
633         if (ret != 0) {
634             printf ("audio_out_prepare failed.\n");
635             audio_out_destroy(output);
636             return 0;
637         }
638     }
639
640     do {
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');
647
648     //printf ("loop start\n");
649     //for (i=0; i<10; i++) {
650     //    printf ("-------- %d -------\n",i);
651     //    usleep (1000000);
652     //}
653
654     //getchar();
655
656     if (read_mode) {
657         printf ("audio_in_unprepare\n");
658         audio_in_unprepare(input);
659         printf ("audio_in_destroy\n");
660         audio_in_destroy(input);
661
662         fclose(fp_w);
663         fp_w = NULL;
664
665 #ifdef _NEW_SOUND_MANAGER_API_
666         ret = sound_manager_destroy_stream_information(g_stream_info_read_h);
667         if (ret) {
668             printf ("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
669         } else {
670             g_stream_info_read_h = NULL;
671         }
672 #endif
673     }
674
675     //getchar();
676
677     if (write_mode) {
678         printf ("audio_out_unprepare\n");
679         audio_out_unprepare(output);
680         printf ("audio_out_destroy\n");
681         audio_out_destroy(output);
682
683 #ifdef _NEW_SOUND_MANAGER_API_
684         ret = sound_manager_destroy_stream_information(g_stream_info_write_h);
685         if (ret) {
686             printf ("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
687         } else {
688             g_stream_info_write_h = NULL;
689         }
690 #endif
691     }
692
693     return 0;
694 }
695
696 int main(int argc, char ** argv)
697 {
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]));
709     } else {
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");
715     }
716     return 0;
717 }
718