Adds aec testcase 24/260324/10
authorJaechul Lee <jcsing.lee@samsung.com>
Wed, 23 Jun 2021 08:04:40 +0000 (17:04 +0900)
committerJaechul Lee <jcsing.lee@samsung.com>
Thu, 8 Jul 2021 02:46:25 +0000 (11:46 +0900)
[Version] 0.5.41
[Issue Type] Add testcase

Change-Id: I997136f7aeed4538a457e3f3c80bee107c8e0de9
Signed-off-by: Jaechul Lee <jcsing.lee@samsung.com>
CMakeLists.txt
packaging/capi-media-audio-io.spec
test/CMakeLists.txt
test/audio_io_test_ec.c [new file with mode: 0644]

index 1c72833..b83d867 100644 (file)
@@ -39,6 +39,7 @@ ENDIF(TIZEN_FEATURE_DISABLE_SESSION_BACKWARD_COMP)
 
 IF(TIZEN_FEATURE_PRODUCT_TV)
     ADD_DEFINITIONS("-DDISABLE_MOBILE_BACK_COMP")
+    ADD_DEFINITIONS("-DTIZEN_FEATURE_TV_PROD")
 ENDIF(TIZEN_FEATURE_PRODUCT_TV)
 
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=${LIB_INSTALL_DIR}")
index 83ff1f0..d954082 100644 (file)
@@ -1,6 +1,6 @@
 Name:           capi-media-audio-io
 Summary:        An Audio Input & Audio Output library in Tizen Native API
-Version:        0.5.40
+Version:        0.5.41
 Release:        0
 Group:          Multimedia/API
 License:        Apache-2.0
@@ -94,6 +94,7 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
 %files tool
 %manifest %{name}.manifest
 %{_prefix}/bin/audio_io_test
+%{_prefix}/bin/audio_io_test_ec
 
 %if 0%{?gcov:1}
 %files gcov
index 9505dff..e225884 100644 (file)
@@ -17,3 +17,4 @@ FOREACH(src ${sources})
 ENDFOREACH()
 
 INSTALL(TARGETS audio_io_test DESTINATION bin)
+INSTALL(TARGETS audio_io_test_ec DESTINATION bin)
diff --git a/test/audio_io_test_ec.c b/test/audio_io_test_ec.c
new file mode 100644 (file)
index 0000000..1559cb3
--- /dev/null
@@ -0,0 +1,275 @@
+#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;
+}
+