Add external timestamp support to endpointer to prevent clock drift#421
Add external timestamp support to endpointer to prevent clock drift#421
Conversation
- Added ps_endpointer_timestamp_cb_t callback type for external timestamps - Added ps_endpointer_set_timestamp_func() to set external timestamp source - Added ps_endpointer_timestamp() to get current timestamp - Modified internal timestamp handling to use callback when available - Maintains full backward compatibility - existing code works unchanged - Added comprehensive test suite (test_endpointer_timestamp) - Added example program demonstrating the new functionality This addresses issue #352 by allowing applications to provide their own timestamp source (e.g., system time, monotonic clock) to avoid drift between audio clock and system clock. The implementation maintains the audio-based timestamp tracking internally for queue management while using the external source for speech start/end timestamps when provided.
dhdaines
left a comment
There was a problem hiding this comment.
Looks good to me! I'm not and probably never will be an expert in real-time audio processing but I definitely understand why this is necessary.
There's one comment in the code to verify. Also I have one question: is this mechanism sufficiently flexible to be used with common audio APIs e.g. CoreAudio, OpenAL, etc? (there are like thousands of these and I understand basically zero of them)
|
it should be pretty generic, the callback just needs to return a double with the current time: Let me see if i can make some examples CoreAudio PortAudio ALSA WebAudio Windows WASAPI |
dhdaines
left a comment
There was a problem hiding this comment.
Looks good now! Thank you so much!
This will go into a PocketSphinx 5.1.0 release in the next few days once I iron out some build issues.
Oh, thank you for the examples. Probably this should go in some documentation somewhere but just to have a record of it here already helps. |
Summary
This PR adds support for external timestamp sources to
ps_endpointer_tto prevent timestamp drift between audio and system clocks, addressing issue #352.Problem
The endpointer currently calculates timestamps based solely on audio sample counts:
This causes drift when the audio clock is not synchronized with the system clock, making it difficult to correlate speech detection events with system time.
Solution
This implementation allows applications to provide their own timestamp source (e.g., system time, monotonic clock) while maintaining full backward compatibility:
New Features
Timestamp Callback Type:
ps_endpointer_timestamp_cb_tNew API Functions:
ps_endpointer_set_timestamp_func()- Set external timestamp sourceps_endpointer_timestamp()- Get current timestampExample Program:
examples/endpointer_timestamp_example.cImplementation Details
Testing
test/unit/test_endpointer_timestamp.cBackward Compatibility
✅ Fully backward compatible - Existing code continues to work without any changes:
Usage Example
Fixes #352