a5a7775f36740b28d38fe882d518bac0af0cc454
[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 <sound_manager.h>
23 #include <audio_io.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         float sine[TABLE_SIZE];
35         int left_channel;
36         int right_channel;
37 } test_wav_t;
38 test_wav_t test_wav;
39
40 static int ch_table[3] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO };
41
42 void play_file(char *file, int length, int ch)
43 {
44         audio_out_h output;
45         FILE *fp = fopen(file, "r");
46         if (fp == NULL) {
47                 printf("fopen failed\n");
48                 return;
49         }
50
51         char *buf = malloc(length);
52         if (buf == NULL) {
53                 printf("malloc failed\n");
54                 fclose(fp);
55                 return;
56         }
57
58         printf("start to play [%s][%d][%d]\n", file, length, ch);
59         //audio_out_create(44100, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output);
60         audio_out_create_new(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &output);
61         if (fread(buf, 1, length, fp) != length) {
62                 printf("error!!!!\n");
63         }
64
65         audio_out_prepare(output);
66         audio_out_write(output, buf, length);
67         audio_out_unprepare(output);
68
69         audio_out_destroy(output);
70
71         fclose(fp);
72
73         printf("play done\n");
74 }
75
76 #define DUMP_FILE "/root/test.raw"
77
78 void play_file_sample(char *file, int frequency, int ch, int type)
79 {
80         audio_out_h output;
81         int file_size = 0;
82         int read_bytes = 0;
83         int buffer_size = 0;
84         char *buf = NULL;
85
86         if (ch < 0 || ch > 2) {
87                 ch = 0;
88         }
89
90         FILE *fp = fopen(file, "r");
91         if (fp == NULL) {
92                 printf("open failed\n");
93                 return;
94         }
95         /*Get the size */
96         fseek(fp, 0, SEEK_END);
97         file_size = ftell(fp);
98         fseek(fp, 0, SEEK_SET);
99
100         printf("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
101         if (type) {
102                 //audio_out_create(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output);
103                 audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &output);
104         } else {
105                 //audio_out_create(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_U8, SOUND_TYPE_MEDIA, &output);
106                 audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8, &output);
107         }
108         audio_out_prepare(output);
109         audio_out_get_buffer_size(output, &buffer_size);
110
111         buf = (char *)malloc(buffer_size);
112         if (buf == NULL) {
113                 printf("malloc failed\n");
114                 audio_out_unprepare(output);
115                 audio_out_destroy(output);
116                 fclose(fp);
117                 return;
118         }
119         //audio_out_prepare(output);
120
121         while (file_size > 0) {
122                 read_bytes = fread(buf, 1, buffer_size, fp);
123                 printf("Read %d Requested - %d\n", read_bytes, buffer_size);
124                 audio_out_write(output, buf, read_bytes);
125                 file_size = file_size - read_bytes;
126         }
127
128         audio_out_unprepare(output);
129         audio_out_destroy(output);
130
131         free(buf);
132         fclose(fp);
133         printf("play done\n");
134 }
135
136 int audio_io_test(int length, int num, int ch)
137 {
138         int ret, size, i;
139         audio_in_h input;
140         if ((ret = audio_in_create(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &input)) == AUDIO_IO_ERROR_NONE) {
141                 ret = audio_in_ignore_session(input);
142                 if (ret != 0) {
143                         printf("ERROR, set session mix\n");
144                         audio_in_destroy(input);
145                         return 0;
146                 }
147
148                 ret = audio_in_prepare(input);
149                 if (ret != 0) {
150                         printf("ERROR, prepare\n");
151                         audio_in_destroy(input);
152                         return 0;
153                 }
154
155                 FILE *fp = fopen(DUMP_FILE, "wb+");
156
157                 if (fp == NULL) {
158                         printf("ERROR, file open failed\n");
159                         audio_in_destroy(input);
160                         return 0;
161                 }
162
163                 if ((ret = audio_in_get_buffer_size(input, &size)) == AUDIO_IO_ERROR_NONE) {
164                         size = length;
165                         char *buffer = alloca(size);
166
167                         for (i = 0; i < num; i++) {
168                                 printf("### loop = %d ============== \n", i);
169                                 if ((ret = audio_in_read(input, (void *)buffer, size)) > AUDIO_IO_ERROR_NONE) {
170                                         fwrite(buffer, size, sizeof(char), fp);
171                                         printf("PASS, size=%d, ret=%d\n", size, ret);
172                                 } else {
173                                         printf("FAIL, size=%d, ret=%d\n", size, ret);
174                                 }
175                         }
176                 }
177
178                 fclose(fp);
179
180                 audio_in_destroy(input);
181         }
182
183         play_file(DUMP_FILE, length * num, ch);
184
185         return 1;
186 }
187
188 int audio_io_loopback_in_test()
189 {
190         int ret, size;
191         audio_in_h input;
192         FILE *fp = fopen("/tmp/dump_test.raw", "wb+");
193
194         if (fp == NULL) {
195                 printf("open failed \n");
196                 return 0;
197         }
198
199         if ((ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input)) == AUDIO_IO_ERROR_NONE) {
200                 ret = audio_in_ignore_session(input);
201                 if (ret != 0) {
202                         printf("ERROR, set session mix\n");
203                         goto exit;
204                 }
205
206                 ret = audio_in_prepare(input);
207                 if (ret != 0) {
208                         printf("ERROR, prepare\n");
209                         goto exit;
210                 }
211
212                 ret = audio_in_get_buffer_size(input, &size);
213                 if (ret != AUDIO_IO_ERROR_NONE) {
214                         printf("audio_in_get_buffer_size failed.\n");
215                         goto exit;
216                 }
217
218                 while (1) {
219                         char *buffer = alloca(size);
220                         if ((ret = audio_in_read(input, (void *)buffer, size)) > AUDIO_IO_ERROR_NONE) {
221                                 fwrite(buffer, size, sizeof(char), fp);
222                                 printf("PASS, size=%d, ret=%d\n", size, ret);
223                         } else {
224                                 printf("FAIL, size=%d, ret=%d\n", size, ret);
225                         }
226                 }
227         }
228
229  exit:
230         audio_in_destroy(input);
231
232         fclose(fp);
233
234         return ret;
235
236 }
237
238 int audio_io_loopback_test()
239 {
240         int ret, size;
241         audio_in_h input;
242         audio_out_h output;
243         char *buffer = NULL;
244
245         ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
246         if (ret != AUDIO_IO_ERROR_NONE) {
247                 printf("audio_in_create_ex failed. \n");
248                 return 0;
249         }
250         //ret = audio_out_create(16000, AUDIO_CHANNEL_MONO , AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_CALL, &output);
251         ret = audio_out_create_new(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
252         if (ret != AUDIO_IO_ERROR_NONE) {
253                 printf("audio_out_create failed. \n");
254                 return 0;
255         }
256
257         ret = audio_in_prepare(input);
258         if (ret != 0) {
259                 printf("audio_in_prepare failed.\n");
260                 audio_in_destroy(input);
261                 return 0;
262         } else {
263                 ret = audio_in_get_buffer_size(input, &size);
264                 if (ret != AUDIO_IO_ERROR_NONE) {
265                         printf("audio_in_get_buffer_size failed.\n");
266                         return 0;
267                 } else {
268                         printf("size(%d)\n", size);
269                         buffer = alloca(size);
270                 }
271         }
272
273         ret = audio_out_prepare(output);
274         if (ret != 0) {
275                 printf("audio_out_prepare failed.\n");
276                 audio_out_destroy(output);
277                 return 0;
278         }
279
280         if (buffer == NULL) {
281                 printf("buffer is null\n");
282                 return 0;
283         }
284
285         while (1) {
286                 ret = audio_in_read(input, (void *)buffer, size);
287                 if (ret > AUDIO_IO_ERROR_NONE) {
288                         ret = audio_out_write(output, buffer, size);
289                         if (ret > AUDIO_IO_ERROR_NONE) {
290                                 printf("audio read/write success. buffer(%p), size(%d)\n", buffer, size);
291                         } else {
292                                 printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size);
293                         }
294                 } else
295                         printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size);
296         }
297
298 }
299
300 audio_in_h input;
301 audio_out_h output;
302
303 FILE *fp_w = NULL;
304
305 #ifdef _NEW_SOUND_MANAGER_API_
306 sound_stream_info_h g_stream_info_read_h = NULL;
307 sound_stream_info_h g_stream_info_write_h = NULL;
308
309 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)
310 {
311         int ret = 0;
312         sound_stream_focus_state_e playback_focus_state;
313         sound_stream_focus_state_e recording_focus_state;
314         printf("*** focus_callback_read is called, stream_info(%p) ***\n", stream_info);
315         printf(" - reason_for_change(%d), additional_info(%s), user_data(%p)\n", reason_for_change, additional_info, user_data);
316         ret = sound_manager_get_focus_state(stream_info, &playback_focus_state, &recording_focus_state);
317         if (!ret)
318                 printf(" - focus_state(playback_focus:%d, recording_focus:%d)\n", playback_focus_state, recording_focus_state);
319         if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
320                 printf(" -- PLAYBACK_FOCUS acquired\n");
321         }
322         if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
323                 printf(" -- FOCUS_RECORDING acquired\n");
324         }
325         printf("*** focus_callback_read is ended, stream_info(%p) ****\n", stream_info);
326         return;
327 }
328
329 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)
330 {
331         int ret = 0;
332         sound_stream_focus_state_e playback_focus_state;
333         sound_stream_focus_state_e recording_focus_state;
334         printf("*** focus_callback_write is called, stream_info(%p) ***\n", stream_info);
335         printf(" - reason_for_change(%d), additional_info(%s), user_data(%p)\n", reason_for_change, additional_info, user_data);
336         ret = sound_manager_get_focus_state(stream_info, &playback_focus_state, &recording_focus_state);
337         if (!ret)
338                 printf(" - focus_state(playback_focus:%d, recording_focus:%d)\n", playback_focus_state, recording_focus_state);
339         if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
340                 printf(" -- PLAYBACK_FOCUS acquired\n");
341         }
342         if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
343                 printf(" -- FOCUS_RECORDING acquired\n");
344         }
345         printf("*** focus_callback_write is ended, stream_info(%p) ****\n", stream_info);
346         return;
347 }
348 #endif
349
350 static void interrupted_callback_read(audio_io_interrupted_code_e code, void *user_data)
351 {
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 {
357         printf("*** interrupted_callback_write is called, code(%d), user_data(%p) ***\n", code, user_data);
358 }
359
360 static void _audio_io_stream_read_cb(audio_in_h handle, size_t nbytes, void *user_data)
361 {
362         const void *buffer = NULL;
363         unsigned int len = (unsigned int)nbytes;
364
365         //printf("_audio_io_stream_read_cb : handle=%p, nbytes=%d, user_data=%p\n", handle, nbytes, user_data);
366
367         if (len > 0) {
368                 audio_in_peek(handle, &buffer, &len);
369                 if (fp_w) {
370                         fwrite(buffer, sizeof(char), len, fp_w);
371                 }
372                 audio_in_drop(handle);
373         }
374 }
375
376 static void _audio_io_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data)
377 {
378         short *buffer = NULL;
379         int ret = 0;
380         int i = 0;
381
382         //printf("_audio_io_stream_write_cb : handle=%p, nbytes=%d, user_data=%p\n", handle, nbytes, user_data);
383
384         if (nbytes > 0) {
385                 buffer = (short *)malloc(nbytes);
386                 if (buffer == NULL) {
387                         printf("malloc failed\n");
388                         return;
389                 }
390                 memset(buffer, 0, nbytes);
391
392                 for (i = 0; i < nbytes / 2; i += 2) {
393                         buffer[i] = (short)32768 *test_wav.sine[test_wav.left_channel]; /* left */
394                         buffer[i + 1] = (short)32768 *test_wav.sine[test_wav.right_channel];    /* right */
395                         test_wav.left_channel += 1;
396                         if (test_wav.left_channel >= TABLE_SIZE)
397                                 test_wav.left_channel -= TABLE_SIZE;
398                         test_wav.right_channel += 3;
399                         if (test_wav.right_channel >= TABLE_SIZE)
400                                 test_wav.right_channel -= TABLE_SIZE;
401                 }
402
403                 ret = audio_out_write(handle, buffer, nbytes);
404                 if (ret > AUDIO_IO_ERROR_NONE) {
405                         //printf("audio write success. buffer(%p), nbytes(%d)\n", buffer, nbytes);
406                 }
407
408                 free(buffer);
409         }
410 }
411
412 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)
413 {
414         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);
415 }
416
417 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)
418 {
419         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);
420 }
421
422 int _convert_cmd_and_run(char cmd, int mode)
423 {
424         int ret = 0;
425         switch (cmd) {
426         case 'P':
427                 if (mode & 0x01)
428                         ret = audio_out_prepare(output);
429                 if (mode & 0x02)
430                         ret = audio_in_prepare(input);
431                 break;
432         case 'u':
433                 if (mode & 0x01)
434                         ret = audio_out_unprepare(output);
435                 if (mode & 0x02)
436                         ret = audio_in_unprepare(input);
437                 break;
438         case 'p':
439                 if (mode & 0x01)
440                         ret = audio_out_pause(output);
441                 if (mode & 0x02)
442                         ret = audio_in_pause(input);
443                 break;
444         case 'r':
445                 if (mode & 0x01)
446                         ret = audio_out_resume(output);
447                 if (mode & 0x02)
448                         ret = audio_in_resume(input);
449                 break;
450         case 'd':
451                 if (mode & 0x01)
452                         ret = audio_out_drain(output);
453                 //if (mode & 0x02)  ret = audio_in_drain(input);
454                 break;
455         case 'f':
456                 if (mode & 0x01)
457                         ret = audio_out_flush(output);
458                 if (mode & 0x02)
459                         ret = audio_in_flush(input);
460                 break;
461         case 'i':
462 #ifdef _NEW_SOUND_MANAGER_API_
463                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_write, NULL, &g_stream_info_write_h);
464                 if (ret) {
465                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
466                 }
467 #endif
468 #ifdef _SESSION_SOUND_MANAGER_API_
469                 ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
470                 if (ret) {
471                         printf("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
472                 }
473                 ret = sound_manager_set_media_session_option(SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
474                 if (ret) {
475                         printf("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
476                 }
477 #endif
478                 break;
479         case 'q':                                       /* quit */
480                 ret = 1;
481                 break;
482         default:
483                 ret = 1;
484                 break;
485         }
486         return ret;
487 }
488
489 int audio_io_async_test(int mode)
490 {
491         int ret, size;
492         char *buffer = NULL;
493         int i = 0;
494
495         char cmd = 0;
496         int cmd_ret;
497
498         int write_mode = (mode & 0x01);
499         int read_mode = (mode & 0x02);
500
501         if ((write_mode == 0) && (read_mode == 0)) {
502                 printf("not vaild mode.\n");
503                 return 0;
504         }
505
506         if (read_mode) {
507
508 #ifdef _SESSION_SOUND_MANAGER_API_
509                 printf("set session for capture.\n");
510
511                 ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
512                 if (ret) {
513                         printf("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
514                 }
515
516                 ret = sound_manager_set_media_session_option(SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
517                 if (ret) {
518                         printf("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
519                 }
520 #endif
521
522                 printf("audio_in_create\n");
523                 ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
524                 if (ret != AUDIO_IO_ERROR_NONE) {
525                         printf("audio_in_create_ex failed. \n");
526                         return 0;
527                 }
528                 printf("audio_in_create success!!! [%p]\n", input);
529
530                 ret = audio_in_set_stream_cb(input, _audio_io_stream_read_cb, NULL);
531                 if (ret != AUDIO_IO_ERROR_NONE) {
532                         printf("audio_in_set_stream_cb failed. \n");
533                         return 0;
534                 }
535                 printf("audio_in_set_stream_cb success!!! [%p]\n", input);
536
537                 ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
538                 if (ret != AUDIO_IO_ERROR_NONE) {
539                         printf("audio_out_set_state_changed_cb failed. \n");
540                         return 0;
541                 }
542                 printf("audio_out_set_state_changed_cb success!!! [%p]\n", input);
543
544                 fp_w = fopen("/tmp/pcm_w.raw", "w");
545
546 #ifdef _NEW_SOUND_MANAGER_API_
547                 //set stream type as SOUND_STREAM_TYPE_MEDIA
548                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_read, NULL, &g_stream_info_read_h);
549                 if (ret) {
550                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
551                 }
552                 ret = audio_in_set_stream_info(input, g_stream_info_read_h);
553 #endif
554
555                 ret = audio_in_set_interrupted_cb(input, interrupted_callback_read, NULL);
556         }
557
558         if (write_mode) {
559                 printf("before audio_out_create\n");
560                 getchar();
561
562 #ifdef _SESSION_SOUND_MANAGER_API_
563                 printf("set session for playback.\n");
564
565                 ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
566                 if (ret) {
567                         printf("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
568                 }
569
570                 ret = sound_manager_set_media_session_option(SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
571                 if (ret) {
572                         printf("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
573                 }
574 #endif
575
576                 printf("audio_out_create\n");
577                 //ret = audio_out_create(44100, AUDIO_CHANNEL_STEREO , AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output);
578                 ret = audio_out_create_new(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
579                 if (ret != AUDIO_IO_ERROR_NONE) {
580                         printf("audio_out_create failed. \n");
581                         return 0;
582                 }
583                 printf("audio_out_create success!!! [%p]\n", output);
584
585                 ret = audio_out_set_stream_cb(output, _audio_io_stream_write_cb, NULL);
586                 if (ret != AUDIO_IO_ERROR_NONE) {
587                         printf("audio_out_set_stream_cb failed. \n");
588                         return 0;
589                 }
590                 printf("audio_out_set_stream_cb success!!! [%p]\n", output);
591
592                 ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
593                 if (ret != AUDIO_IO_ERROR_NONE) {
594                         printf("audio_out_set_state_changed_cb failed. \n");
595                         return 0;
596                 }
597                 printf("audio_out_set_state_changed_cb success!!! [%p]\n", output);
598
599 #ifdef _NEW_SOUND_MANAGER_API_
600                 //set stream type as SOUND_STREAM_TYPE_MEDIA
601                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_write, NULL, &g_stream_info_write_h);
602                 if (ret) {
603                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
604                 }
605                 ret = audio_out_set_stream_info(output, g_stream_info_write_h);
606 #endif
607
608                 ret = audio_out_set_interrupted_cb(output, interrupted_callback_write, NULL);
609
610                 //generate wave data
611                 for (i = 0; i < TABLE_SIZE; i++) {
612                         test_wav.sine[i] = 0.9 * (float)sin(((double)i / (double)TABLE_SIZE) * M_PI * 2.);
613                 }
614                 test_wav.left_channel = test_wav.right_channel = 0;
615         }
616
617         if (read_mode) {
618                 printf("before audio_in_prepare\n");
619                 getchar();
620                 printf("audio_in_prepare\n");
621                 ret = audio_in_prepare(input);
622                 if (ret != 0) {
623                         printf("audio_in_prepare failed.\n");
624                         audio_in_destroy(input);
625                         return 0;
626                 } else {
627                         ret = audio_in_get_buffer_size(input, &size);
628                         if (ret != AUDIO_IO_ERROR_NONE) {
629                                 printf("audio_in_get_buffer_size failed.\n");
630                                 return 0;
631                         } else {
632                                 printf("size(%d)\n", size);
633                                 buffer = alloca(size);
634                         }
635                 }
636
637                 if (buffer == NULL) {
638                         printf("buffer is null\n");
639                         return 0;
640                 }
641         }
642
643         if (write_mode) {
644                 printf("before audio_out_prepare\n");
645                 getchar();
646                 printf("audio_out_prepare\n");
647                 ret = audio_out_prepare(output);
648                 if (ret != 0) {
649                         printf("audio_out_prepare failed.\n");
650                         audio_out_destroy(output);
651                         return 0;
652                 }
653         }
654
655         do {
656                 printf("command(q:quit) : ");
657                 cmd = (char)getchar();
658                 if (cmd != '\n')
659                         getchar();
660                 cmd_ret = _convert_cmd_and_run(cmd, mode);
661                 printf("  - result code : %d\n", cmd_ret);
662         } while (cmd != 'q');
663
664         //printf ("loop start\n");
665         //for (i=0; i<10; i++) {
666         //    printf ("-------- %d -------\n",i);
667         //    usleep (1000000);
668         //}
669
670         //getchar();
671
672         if (read_mode) {
673                 printf("audio_in_unprepare\n");
674                 audio_in_unprepare(input);
675                 printf("audio_in_destroy\n");
676                 audio_in_destroy(input);
677
678                 fclose(fp_w);
679                 fp_w = NULL;
680
681 #ifdef _NEW_SOUND_MANAGER_API_
682                 ret = sound_manager_destroy_stream_information(g_stream_info_read_h);
683                 if (ret) {
684                         printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
685                 } else {
686                         g_stream_info_read_h = NULL;
687                 }
688 #endif
689         }
690         //getchar();
691
692         if (write_mode) {
693                 printf("audio_out_unprepare\n");
694                 audio_out_unprepare(output);
695                 printf("audio_out_destroy\n");
696                 audio_out_destroy(output);
697
698 #ifdef _NEW_SOUND_MANAGER_API_
699                 ret = sound_manager_destroy_stream_information(g_stream_info_write_h);
700                 if (ret) {
701                         printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
702                 } else {
703                         g_stream_info_write_h = NULL;
704                 }
705 #endif
706         }
707
708         return 0;
709 }
710
711 int main(int argc, char **argv)
712 {
713         if (argc == 2 && !strcmp(argv[1], "call-forwarding-loop")) {
714                 audio_io_loopback_test();
715         } else if (argc == 2 && !strcmp(argv[1], "call-forwarding-in")) {
716                 audio_io_loopback_in_test();
717         } else if (argc == 3 && !strcmp(argv[1], "async")) {
718                 audio_io_async_test(atoi(argv[2]));
719         } else if (argc == 4) {
720                 printf("run with [%s][%s][%s]\n", argv[1], argv[2], argv[3]);
721                 audio_io_test(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
722         } else if (argc == 6) {
723                 play_file_sample(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
724         } else {
725                 printf("1. usage : audio_io_test call-forwarding-loop\n");
726                 printf("2. usage : audio_io_test call-forwarding-in\n");
727                 printf("3. usage : audio_io_test [length to read] [number of iteration] [channels]\n");
728                 printf("4. usage : audio_io_test async [write(1) | read(2)]\n");
729                 printf("5. Uasge : audio_io_test play [filename] [sample rate] [channels] [type(0:U8)]\n");
730         }
731         return 0;
732 }