Searching a data-structure for access based on passed time/current time?

13 Views Asked by At

I am thinking about how one would create time-based events, firing in the order of milliseconds.

In many applications, such as the audio and video/lighting industries, calling functions/taking action upon a certain time is crucial.

E.g. playing back audio requires to load a buffer, which then plays back the audio live via for example sound cards or the network.

Similarly, in lighting you usually use timecode, which (like the name implies) receives timecode on a device so it can fire presets at the right time.

Such timecodes are usually in 25/28/30 "FPS", or better phrased: They have a resolution of 30 ticks per second.

This usually means that over a timeframe of a few minutes (most of the time over the timeframe of a song), one has to maintain a list, with cues, that need to be called at certain timestamps.

Regular Arrays dont completely cut it, AFAIK. You would have to guarantee that the array is in chronological order. Also, you need to access the elements themselves to get the time at which they should be activated/called.

Looking through this array each time a "tick" comes in from the clock signal would be very expensive.

How could I organize such a system, where I would need to store objects, and call the objects based on a time value?

One of my ideas was to utilize two arrays, one for the objects, and another one for storing the time difference between 2 objects being called. As long as the waiting time between two cues is larger than the elapsed time, I would just wait. If the elapsed time is larger than the wanted waiting time, I would retrieve all objects that lie inside of that timeframe and call some function on them.

Since this issue/idea/solution must also exist in the audio/video world, where you need to wait for e.g. the next image to be displayed, I thought I would've found something on the web, but sadly I didn't.

Additional Info: In the end, this would be implemented in C++. However, I think this is a general code-design question, therefore I am more intrigued to get answers that hint at possible solutions and keywords :)

0

There are 0 best solutions below