❔ Following BPM with code
I’m wanting to create my own very small dmx light show maker in c#. I realize it takes forever to preprogram a song but just for the hell of it I think it would be fun. I’ve gotten as far as sending signals to the lights over USB. I’m just not sure how to change the lights at the same exact time as the rhythm of the song. I’ve tried using sleep but I can’t get it to exactly follow the BPM. Any pointers here? I’ve seen the word timecode around but I’m not sure if I want this or not.
11 Replies
You can use
PeriodicTimer
, I believe it's the most accurate timer in C# right nowAlright I’ll give that a shot
Hm, it's still a little off
there are ofc algorithms to analize the sound to get the bpm but dont ask me how to do it
I know the BPM of the song
But I'm struggling to stay exactly on rythm
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Hm, it starts out fine but then gets off. Even if it’s 10ms behind I don’t really mind. I just want something that exactly follows bpm. I’m not sure if I need to take an approach kind of like a video editor with a timeline and have light tracks or… not too sure
sounds like the error accumulates.. just for completeness.. if you let it run longer does it goes even more offbeat?
yes the longer i run it the more off Beat it gets
Within 30 or so seconds, the lights are notably not following the rhythm of the song
ok two thoughts i got while reading this thread...
1. the reason why sleep is causing you issues is probably following
lets asume you want to execute a method every 10ms, and the method itself takes 1ms
what would happen is that the intervals is now 11ms not 10.. meaning you get 1ms off every 10 ms
2. to my understanding if you use the timers "the intended" way (hook to tick and let it run), it will get offtick anyway, the more accurate it is, the smaller the issue.
personally i would recommend using a stopwatch or the like to give you elapsed or current time and then calculate from here how far away the next tick should be...
It's worse than that... Sleeps are only accurate to 100ms or so (and they'll always be too long, not too short), and that includes things like timer callbacks. So yeah if your code does
Thing();
Sleep();
Thing();
Sleep();
Then the first sleep might be 100ms over, but the second sleep might be 100ms over as well, so you're up to 200ms out by the end of the second sleep.
You need to keep track of how long it's been since the song started (a
Stopwatch
is the tool for that, as others mentioned), and base your delays off that. If a light effect should hppen 15s into the song, make it happen 15s after the song started, rather than some delay after the previous effect.
But even then, Windows is not a real-time OS and you're never going to be exactly in time. Maybe it doesn't matter, but if it does, you need a real-time system, such as an embedded device like an Arduino, in there to do things at exactly the right time.Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.