Best way to get code to run at very precise timing?
Hi, I'm trying to improve some live beat detection code I've written. The main issue I have right now is that my sampling rate for beat detection is dependent on the frame rate. In other words when the frame rate dips, the sampling rate of the beat detection dips as well. I'm currently using a co-routine with wait for seconds. I know that fixed update could offer an improvement but the problem that comes up then is there will be gaps in the sampling data every time update runs. This will be made worse because as update slows down more and more fixed update will run more and more. So I will be left with super compressed portions of audio data with increasingly large gaps.
Ideally I'd like to somehow interrupt the main thread every set amount of time and run some code to sample the audio. I've looked around a bit and discovered the .net System.timers class can do this. However, my understanding is that this uses threading and I'm a little worried about stability and thread safety.
So overall what I'm asking is what's the best way to achieve code running on a precise time interval. Is the .net timer option the only way? if so is it safe to use? or is there another way? I know this is a pretty niche question so if anyone can give me any kind of help I'll be extremely grateful. Thank you in advance!
Your answer
Follow this Question
Related Questions
Need coroutine to render a texture value clamped from 0 to 1 over time 1 Answer
My code is not working - HELP - IEnumerator Coroutines 0 Answers
One Audiosource catchy/lagging, two working 1 Answer
Coroutine and SetFloat not working as expected 0 Answers
How to do very accurate time measurement 3 Answers