--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <time.h>
+#include <stdbool.h>
+
+#include <audio_io.h>
+#include <sound_manager.h>
+#include <sound_manager_internal.h>
+
+#ifndef TIZEN_FEATURE_TV_PROD
+static double _get_rand_double_range(double min, double max)
+{
+ double ret;
+ int r;
+
+ r = rand();
+ ret = (max / RAND_MAX) * r * (r & 0x1 ? 1 : -1);
+
+ return ret;
+}
+
+static void _white_noise_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data)
+{
+ int i;
+ short *ptr;
+ char *buffer;
+ double amp = 0.3 * 32767.0;
+
+ buffer = (char *)malloc(nbytes);
+ if (!buffer)
+ return;
+
+ ptr = (short *)buffer;
+ for (i = 0; i < nbytes; i += 2, ptr++)
+ ptr[0] = (short)(amp * _get_rand_double_range(-1.0, 1.0));
+
+ audio_out_write(handle, buffer, nbytes);
+
+ free(buffer);
+}
+
+int play_white_noise_async(audio_out_h *output)
+{
+ audio_out_h _output = NULL;
+ int ret;
+
+ srand(time(NULL));
+
+ ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &_output);
+ if (ret != AUDIO_IO_ERROR_NONE) {
+ printf("failed to create audio out\n");
+ return -1;
+ }
+
+ ret = audio_out_set_stream_cb(_output, _white_noise_stream_write_cb, NULL);
+ if (ret != AUDIO_IO_ERROR_NONE) {
+ printf("failed to set stream callback\n");
+ goto fail;
+ }
+
+ ret = audio_out_prepare(_output);
+ if (ret != AUDIO_IO_ERROR_NONE) {
+ printf("failed to prepare \n");
+ goto fail;
+ }
+
+ *output = _output;
+
+ return 0;
+
+fail:
+ audio_out_destroy(_output);
+
+ return -1;
+}
+
+int stop_white_noise_async(audio_out_h output)
+{
+ int ret;
+
+ ret = audio_out_unprepare(output);
+ if (ret != AUDIO_IO_ERROR_NONE)
+ printf("failed to prepare\n");
+
+ ret = audio_out_destroy(output);
+ if (ret != AUDIO_IO_ERROR_NONE) {
+ printf("failed to destroy output\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+int capture_sound(char **buffer, int *length, int sec)
+{
+ int ret;
+ char *_buffer = NULL;
+ audio_in_h input = NULL;
+ int _length = 16000 * 1 * 2 * sec; // 16Khz, mono, S16LE
+
+ if (!buffer) {
+ printf("buffer is null\n");
+ return -1;
+ }
+
+ ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
+ if (ret != AUDIO_IO_ERROR_NONE) {
+ printf("failed to create audio_input\n");
+ return -1;
+ }
+
+ ret = audio_in_prepare(input);
+ if (ret != AUDIO_IO_ERROR_NONE) {
+ printf("failed to prepare %x0x\n", ret);
+ goto fail;
+ }
+
+ _buffer = malloc(_length);
+ if (!_buffer) {
+ printf("_buffer is null\n");
+ goto fail;
+ }
+
+ ret = audio_in_read(input, _buffer, _length);
+ if (ret <= 0) {
+ printf("failed to read ret(0x%x), length(%d)\n", ret, _length);
+ goto fail;
+ }
+ ret = audio_in_unprepare(input);
+ if (ret != AUDIO_IO_ERROR_NONE) {
+ printf("failed to unprepare 0x%x", ret);
+ goto fail;
+ }
+
+ *buffer = _buffer;
+ *length = _length;
+
+ return 0;
+
+fail:
+ if (input)
+ audio_in_destroy(input);
+ if (buffer)
+ free(buffer);
+
+ return -1;
+}
+
+int enable_echo_cancellation(bool enable)
+{
+ int ret;
+
+ if (enable)
+ ret = sound_manager_start_aec();
+ else
+ ret = sound_manager_stop_aec();
+
+ if (ret != 0) {
+ printf("failed to %s echo cancellation. ret(0x%x)\n",
+ enable ? "start" : "stop", ret);
+ return -1;
+ }
+
+ return 0;
+}
+
+int test_echo_cancellation(char **buffer, bool enable_ec, bool enable_ref, int time_sec)
+{
+ audio_out_h output = NULL;
+ char *_buffer = NULL;
+ int length;
+
+ if (enable_ec && enable_echo_cancellation(true))
+ goto exit;
+
+ if (enable_ref && play_white_noise_async(&output))
+ goto exit;
+
+ printf("Say something for %d seconds.. enable_ec(%s) enable_ref(%s)\n",
+ time_sec,
+ enable_ec ? "on" : "off",
+ enable_ref? "on" : "off");
+
+ if (capture_sound(&_buffer, &length, time_sec))
+ goto exit;
+
+ if (output && stop_white_noise_async(output))
+ goto exit;
+
+ output = NULL;
+
+ if (enable_ec && enable_echo_cancellation(false))
+ goto exit;
+
+ *buffer = _buffer;
+
+ return length;
+
+exit:
+ if (output)
+ stop_white_noise_async(output);
+
+ if (enable_ec)
+ enable_echo_cancellation(false);
+
+ if (_buffer)
+ free(_buffer);
+
+ return -1;
+}
+
+int dump_to_file(const char *filename, char *buffer, int length)
+{
+ int fd, ret;
+
+ fd = open(filename, O_CREAT|O_TRUNC|O_RDWR, 0644);
+ if (fd < 0) {
+ printf("failed to open file\n");
+ return -1;
+ }
+
+ ret = write(fd, buffer, length);
+ if (ret < 0)
+ printf("failed to write\n");
+
+ close(fd);
+
+ return 0;
+}
+#endif
+
+int main(int argc, char **argv)
+{
+#ifndef TIZEN_FEATURE_TV_PROD
+ bool enable_ec;
+ bool enable_ref;
+ int time_sec;
+ char *filename;
+
+ char *buffer;
+ int size;
+
+ if (argc != 5) {
+ printf("- Usages :\n");
+ printf("- # audio_io_test_ec [filename] [on/off 0:1] [noise 0:1] [seconds]\n");
+ return -1;
+ }
+
+ filename = argv[1];
+ enable_ec = !!atoi(argv[2]);
+ enable_ref = !!atoi(argv[3]);
+ time_sec = atoi(argv[4]);
+
+ size = test_echo_cancellation(&buffer, enable_ec, enable_ref, time_sec);
+ if (size <= 0) {
+ printf("failed to test aec\n");
+ return -1;
+ }
+
+ if (dump_to_file(filename, buffer, size))
+ printf("failed to save file %s\n", filename);
+
+ if (buffer)
+ free(buffer);
+#else
+ printf("not supported yet\n");
+#endif
+
+ return 0;
+}
+