153a5a1b3Sopenharmony_ci/***
253a5a1b3Sopenharmony_ci  This file is part of PulseAudio.
353a5a1b3Sopenharmony_ci
453a5a1b3Sopenharmony_ci  PulseAudio is free software; you can redistribute it and/or modify
553a5a1b3Sopenharmony_ci  it under the terms of the GNU Lesser General Public License as published
653a5a1b3Sopenharmony_ci  by the Free Software Foundation; either version 2.1 of the License,
753a5a1b3Sopenharmony_ci  or (at your option) any later version.
853a5a1b3Sopenharmony_ci
953a5a1b3Sopenharmony_ci  PulseAudio is distributed in the hope that it will be useful, but
1053a5a1b3Sopenharmony_ci  WITHOUT ANY WARRANTY; without even the implied warranty of
1153a5a1b3Sopenharmony_ci  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1253a5a1b3Sopenharmony_ci  General Public License for more details.
1353a5a1b3Sopenharmony_ci
1453a5a1b3Sopenharmony_ci  You should have received a copy of the GNU Lesser General Public License
1553a5a1b3Sopenharmony_ci  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
1653a5a1b3Sopenharmony_ci***/
1753a5a1b3Sopenharmony_ci
1853a5a1b3Sopenharmony_ci#ifdef HAVE_CONFIG_H
1953a5a1b3Sopenharmony_ci#include <config.h>
2053a5a1b3Sopenharmony_ci#endif
2153a5a1b3Sopenharmony_ci
2253a5a1b3Sopenharmony_ci#include <stdio.h>
2353a5a1b3Sopenharmony_ci#include <stdlib.h>
2453a5a1b3Sopenharmony_ci
2553a5a1b3Sopenharmony_ci#include <check.h>
2653a5a1b3Sopenharmony_ci
2753a5a1b3Sopenharmony_ci#include <pulse/timeval.h>
2853a5a1b3Sopenharmony_ci
2953a5a1b3Sopenharmony_ci#include <pulsecore/log.h>
3053a5a1b3Sopenharmony_ci#include <pulsecore/time-smoother.h>
3153a5a1b3Sopenharmony_ci
3253a5a1b3Sopenharmony_ciSTART_TEST (smoother_test) {
3353a5a1b3Sopenharmony_ci    pa_usec_t x;
3453a5a1b3Sopenharmony_ci    unsigned u = 0;
3553a5a1b3Sopenharmony_ci    pa_smoother *s;
3653a5a1b3Sopenharmony_ci    int m;
3753a5a1b3Sopenharmony_ci
3853a5a1b3Sopenharmony_ci/*     unsigned msec[] = { */
3953a5a1b3Sopenharmony_ci/*         200, 200, */
4053a5a1b3Sopenharmony_ci/*         300, 320, */
4153a5a1b3Sopenharmony_ci/*         400, 400, */
4253a5a1b3Sopenharmony_ci/*         500, 480, */
4353a5a1b3Sopenharmony_ci/*         0, 0 */
4453a5a1b3Sopenharmony_ci/*     }; */
4553a5a1b3Sopenharmony_ci
4653a5a1b3Sopenharmony_ci    int msec[200];
4753a5a1b3Sopenharmony_ci
4853a5a1b3Sopenharmony_ci    srand(0);
4953a5a1b3Sopenharmony_ci
5053a5a1b3Sopenharmony_ci    if (!getenv("MAKE_CHECK"))
5153a5a1b3Sopenharmony_ci        pa_log_set_level(PA_LOG_DEBUG);
5253a5a1b3Sopenharmony_ci
5353a5a1b3Sopenharmony_ci    for (m = 0, u = 0; u < PA_ELEMENTSOF(msec); u+= 2) {
5453a5a1b3Sopenharmony_ci
5553a5a1b3Sopenharmony_ci        msec[u] = m+1 + (rand() % 100) - 50;
5653a5a1b3Sopenharmony_ci        msec[u+1] = m + (rand() % 2000) - 1000   + 5000;
5753a5a1b3Sopenharmony_ci
5853a5a1b3Sopenharmony_ci        m += rand() % 100;
5953a5a1b3Sopenharmony_ci
6053a5a1b3Sopenharmony_ci        if (msec[u] < 0)
6153a5a1b3Sopenharmony_ci            msec[u] = 0;
6253a5a1b3Sopenharmony_ci
6353a5a1b3Sopenharmony_ci        if (msec[u+1] < 0)
6453a5a1b3Sopenharmony_ci            msec[u+1] = 0;
6553a5a1b3Sopenharmony_ci    }
6653a5a1b3Sopenharmony_ci
6753a5a1b3Sopenharmony_ci    s = pa_smoother_new(700*PA_USEC_PER_MSEC, 2000*PA_USEC_PER_MSEC, false, true, 6, 0, true);
6853a5a1b3Sopenharmony_ci
6953a5a1b3Sopenharmony_ci    for (x = 0, u = 0; x < PA_USEC_PER_SEC * 10; x += PA_USEC_PER_MSEC) {
7053a5a1b3Sopenharmony_ci
7153a5a1b3Sopenharmony_ci        while (u < PA_ELEMENTSOF(msec) && (pa_usec_t) msec[u]*PA_USEC_PER_MSEC < x) {
7253a5a1b3Sopenharmony_ci            pa_smoother_put(s, (pa_usec_t) msec[u] * PA_USEC_PER_MSEC, (pa_usec_t) msec[u+1] * PA_USEC_PER_MSEC);
7353a5a1b3Sopenharmony_ci            pa_log_debug("%i\t\t%i", msec[u],  msec[u+1]);
7453a5a1b3Sopenharmony_ci            u += 2;
7553a5a1b3Sopenharmony_ci
7653a5a1b3Sopenharmony_ci            if (u < PA_ELEMENTSOF(msec))
7753a5a1b3Sopenharmony_ci                pa_smoother_resume(s, (pa_usec_t) msec[u] * PA_USEC_PER_MSEC, true);
7853a5a1b3Sopenharmony_ci        }
7953a5a1b3Sopenharmony_ci
8053a5a1b3Sopenharmony_ci        pa_log_debug("%llu\t%llu", (unsigned long long) (x/PA_USEC_PER_MSEC), (unsigned long long) (pa_smoother_get(s, x)/PA_USEC_PER_MSEC));
8153a5a1b3Sopenharmony_ci    }
8253a5a1b3Sopenharmony_ci
8353a5a1b3Sopenharmony_ci    pa_smoother_free(s);
8453a5a1b3Sopenharmony_ci}
8553a5a1b3Sopenharmony_ciEND_TEST
8653a5a1b3Sopenharmony_ci
8753a5a1b3Sopenharmony_ciint main(int argc, char *argv[]) {
8853a5a1b3Sopenharmony_ci    int failed = 0;
8953a5a1b3Sopenharmony_ci    Suite *s;
9053a5a1b3Sopenharmony_ci    TCase *tc;
9153a5a1b3Sopenharmony_ci    SRunner *sr;
9253a5a1b3Sopenharmony_ci
9353a5a1b3Sopenharmony_ci    s = suite_create("Smoother");
9453a5a1b3Sopenharmony_ci    tc = tcase_create("smoother");
9553a5a1b3Sopenharmony_ci    tcase_add_test(tc, smoother_test);
9653a5a1b3Sopenharmony_ci    suite_add_tcase(s, tc);
9753a5a1b3Sopenharmony_ci
9853a5a1b3Sopenharmony_ci    sr = srunner_create(s);
9953a5a1b3Sopenharmony_ci    srunner_run_all(sr, CK_NORMAL);
10053a5a1b3Sopenharmony_ci    failed = srunner_ntests_failed(sr);
10153a5a1b3Sopenharmony_ci    srunner_free(sr);
10253a5a1b3Sopenharmony_ci
10353a5a1b3Sopenharmony_ci    return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
10453a5a1b3Sopenharmony_ci}
105