I am starting a new C# project which is going to be similar to the music language Sonic Pi, and I thought that await Task.Delay would be a good way to programmatically start and stop playing notes with fairly high precision.
However, trying to keep multiple tracks in sync using this method does not appear to be accurate enough, and I’ve already found several explanations online about how the threadpool will cause multiple tasks to get out of sync, even if they are all awaiting the same Task.Delay value, which I suspected from the beginning was going to be a problem. I just haven’t been able to come up with a good way to keep them in sync.
I tried converting all of the Tasks to Threads with Task.Run, and using Thread.Sleep instead of await Task.Delay, but the end result was the same. The first two threads would be fairly stable, but spinning up a third and fourth thread would take a few hundred milliseconds of delay, causing the later threads to immediately become out of sync with the earlier ones.
My current solution is to create new threads with Task.Run, but to pass a synchronized “start time” value to each thread. This start time can be used by each thread independently to compare to the current Environment.TickCount to determine when to start playing notes. This solves the problem, but it requires me to write additional code to calculate the timing values for each note that is played, and add those values to the initial value of the thread, which isn’t pretty.
So, my question is… does anyone have any suggestions on how I can keep up to, say, 32 Tasks / Threads in sync to the point that they can play the exact same sequence of notes at the exact same time?
My goal is to be able to write a multi-track song completely in C#, putting each track in a separate function, and have all of the tracks start and end at the exact same time.
Thanks for your help.
However, trying to keep multiple tracks in sync using this method does not appear to be accurate enough, and I’ve already found several explanations online about how the threadpool will cause multiple tasks to get out of sync, even if they are all awaiting the same Task.Delay value, which I suspected from the beginning was going to be a problem. I just haven’t been able to come up with a good way to keep them in sync.
I tried converting all of the Tasks to Threads with Task.Run, and using Thread.Sleep instead of await Task.Delay, but the end result was the same. The first two threads would be fairly stable, but spinning up a third and fourth thread would take a few hundred milliseconds of delay, causing the later threads to immediately become out of sync with the earlier ones.
My current solution is to create new threads with Task.Run, but to pass a synchronized “start time” value to each thread. This start time can be used by each thread independently to compare to the current Environment.TickCount to determine when to start playing notes. This solves the problem, but it requires me to write additional code to calculate the timing values for each note that is played, and add those values to the initial value of the thread, which isn’t pretty.
So, my question is… does anyone have any suggestions on how I can keep up to, say, 32 Tasks / Threads in sync to the point that they can play the exact same sequence of notes at the exact same time?
My goal is to be able to write a multi-track song completely in C#, putting each track in a separate function, and have all of the tracks start and end at the exact same time.
Thanks for your help.