commit 0f4e0bbca959251f256a285d829b6517430e7401 from: Sven M. Hallberg <pesco@khjk.org> date: Thu Nov 09 19:11:07 2023 UTC clean up return latency code in cwstop commit - 02b03fabec05c9e959d5b89c8a5b06c5e4c1d330 commit + 0f4e0bbca959251f256a285d829b6517430e7401 blob - f5becb2c828ecec2e3c1779ead6c88ab362c3f27 blob + c4104284c0719a3b60be1879dd6c8dd397bb8b80 --- cw.c +++ cw.c @@ -325,6 +325,9 @@ cwstart() int cwstop(void) { + struct timespec t, dt; + int ms; + if (!sio_stop(hdl)) errx(1, "sio_stop failed"); @@ -333,14 +336,14 @@ cwstop(void) * between playback finishing and the return of the function. This * value can be negative, meaning that we actually returned earlier * than playback will finish. + * + * Since we cannot directly determine the point in time when playback + * finished (or will finish), we take the current time since the + * _start_ of playback minus the total playback time, as determined by + * the number of frames written. */ - struct timespec tnow, dt; - int playms, nowms; - - clock_gettime(CLOCK_MONOTONIC, &tnow); - timespecsub(&tnow, &tplay, &dt); - nowms = dt.tv_sec * 1000 + dt.tv_nsec / 1000000; - playms = writ * 1000 / par.rate; - - return nowms - playms; + clock_gettime(CLOCK_MONOTONIC, &t); + timespecsub(&t, &tplay, &dt); + ms = dt.tv_sec * 1000 + dt.tv_nsec / 1000000; + return ms - writ * 1000 / par.rate; }