11 #include <sound_manager.h>
12 #include <sound_manager_internal.h>
14 #ifndef TIZEN_FEATURE_TV_PROD
15 static double _get_rand_double_range(double min, double max)
21 ret = (max / RAND_MAX) * r * (r & 0x1 ? 1 : -1);
26 static void _white_noise_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data)
31 double amp = 0.3 * 32767.0;
33 buffer = (char *)malloc(nbytes);
37 ptr = (short *)buffer;
38 for (i = 0; i < nbytes; i += 2, ptr++)
39 ptr[0] = (short)(amp * _get_rand_double_range(-1.0, 1.0));
41 audio_out_write(handle, buffer, nbytes);
46 int play_white_noise_async(audio_out_h *output)
48 audio_out_h _output = NULL;
53 ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &_output);
54 if (ret != AUDIO_IO_ERROR_NONE) {
55 printf("failed to create audio out\n");
59 ret = audio_out_set_stream_cb(_output, _white_noise_stream_write_cb, NULL);
60 if (ret != AUDIO_IO_ERROR_NONE) {
61 printf("failed to set stream callback\n");
65 ret = audio_out_prepare(_output);
66 if (ret != AUDIO_IO_ERROR_NONE) {
67 printf("failed to prepare \n");
76 audio_out_destroy(_output);
81 int stop_white_noise_async(audio_out_h output)
85 ret = audio_out_unprepare(output);
86 if (ret != AUDIO_IO_ERROR_NONE)
87 printf("failed to prepare\n");
89 ret = audio_out_destroy(output);
90 if (ret != AUDIO_IO_ERROR_NONE) {
91 printf("failed to destroy output\n");
98 int capture_sound(char **buffer, int *length, int sec)
101 char *_buffer = NULL;
102 audio_in_h input = NULL;
103 int _length = 16000 * 1 * 2 * sec; // 16Khz, mono, S16LE
106 printf("buffer is null\n");
110 ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
111 if (ret != AUDIO_IO_ERROR_NONE) {
112 printf("failed to create audio_input\n");
116 ret = audio_in_prepare(input);
117 if (ret != AUDIO_IO_ERROR_NONE) {
118 printf("failed to prepare %x0x\n", ret);
122 _buffer = malloc(_length);
124 printf("_buffer is null\n");
128 ret = audio_in_read(input, _buffer, _length);
130 printf("failed to read ret(0x%x), length(%d)\n", ret, _length);
133 ret = audio_in_unprepare(input);
134 if (ret != AUDIO_IO_ERROR_NONE) {
135 printf("failed to unprepare 0x%x", ret);
146 audio_in_destroy(input);
153 int enable_echo_cancellation(bool enable)
158 ret = sound_manager_start_aec();
160 ret = sound_manager_stop_aec();
163 printf("failed to %s echo cancellation. ret(0x%x)\n",
164 enable ? "start" : "stop", ret);
171 int test_echo_cancellation(char **buffer, bool enable_ec, bool enable_ref, int time_sec)
173 audio_out_h output = NULL;
174 char *_buffer = NULL;
177 if (enable_ec && enable_echo_cancellation(true))
180 if (enable_ref && play_white_noise_async(&output))
183 printf("Say something for %d seconds.. enable_ec(%s) enable_ref(%s)\n",
185 enable_ec ? "on" : "off",
186 enable_ref? "on" : "off");
188 if (capture_sound(&_buffer, &length, time_sec))
191 if (output && stop_white_noise_async(output))
196 if (enable_ec && enable_echo_cancellation(false))
205 stop_white_noise_async(output);
208 enable_echo_cancellation(false);
216 int dump_to_file(const char *filename, char *buffer, int length)
220 fd = open(filename, O_CREAT|O_TRUNC|O_RDWR, 0644);
222 printf("failed to open file\n");
226 ret = write(fd, buffer, length);
228 printf("failed to write\n");
236 int main(int argc, char **argv)
238 #ifndef TIZEN_FEATURE_TV_PROD
248 printf("- Usages :\n");
249 printf("- # audio_io_test_ec [filename] [on/off 0:1] [noise 0:1] [seconds]\n");
254 enable_ec = !!atoi(argv[2]);
255 enable_ref = !!atoi(argv[3]);
256 time_sec = atoi(argv[4]);
258 size = test_echo_cancellation(&buffer, enable_ec, enable_ref, time_sec);
260 printf("failed to test aec\n");
264 if (dump_to_file(filename, buffer, size))
265 printf("failed to save file %s\n", filename);
270 printf("not supported yet\n");