TonicAudio / Tonic

Easy and efficient audio synthesis in C++
The Unlicense
523 stars 63 forks source link

[Question] "Real time safety" and inter-thread communication in Tonic. #270

Open rvega opened 9 years ago

rvega commented 9 years ago

Most of the literature I've read on real-time programming for audio shows that there should be some sort of lock-free data structure (usually a ring buffer) to pass control messages from the GUI to the audio thread and another one to pass audio samples from the audio thread to the GUI (if one needs to draw the audio wave in the GUI, for example). This is to prevent audio glitches and concurrency issues.

I can see that functions like synth.setParameter() are used, but, I can't figure out if this functions implement some sort of inter-thread communication similar to the one I described or if they use mutexes or something similar instead.

Can someone please explain how the communication between GUI and audio threads work in Tonic and/or the example apps? If a lock-free data structure scheme is not used, why?

morganpackard commented 9 years ago

Tonic doesn't use any protection mechanism for the inter-thread communication other than being careful not to modify anything other than primitive values (generally floats) from anywhere but the audio thread. In practice, this generally works fine, though is theoretically not safe. Don't use it to launch a space ship or run power a medical device. But generally, you can be confident that the inter-thread communication is stable and works.

On Sat, Nov 21, 2015 at 8:27 PM, Rafael Vega notifications@github.com wrote:

Most of the literature I've read on real-time programming for audio shows that there should be some sort of lock-free data structure (usually a ring buffer) to pass control messages from the GUI to the audio thread and another one to pass audio samples from the audio thread to the GUI (if one needs to draw the audio wave in the GUI, for example). This is to prevent audio glitches and concurrency issues.

I can see that functions like synth.setParameter() are used, but, I can't figure out if this functions implement some sort of inter-thread communication similar to the one I described or if they use mutexes or something similar instead.

Can someone please explain how the communication between GUI and audio threads work in Tonic and/or the example apps? If a lock-free data structure scheme is not used, why?

— Reply to this email directly or view it on GitHub https://github.com/TonicAudio/Tonic/issues/270.

Morgan Packard cell: (720) 891-0122 twitter: @morganpackard

aramazhari commented 7 years ago

I know this is an old thread but I also have trouble calling synth.setOutputGen() from another thread. I'm using imgui (a famous GUI library) and change a generator value upon moving a slider around, which is a very practical purpose of using TonicAudio and that gives me exceptions in all places. I do see that there is a MutexLock / Unlock within setOutputGen() but that doesn't seem to help at all.

If there are any examples on best usage of the Tonic api + real-time gui I would appreciate it.

morganpackard commented 7 years ago

What are the exceptions?

On Sat, Sep 16, 2017 at 3:23 PM Aaron Azhari notifications@github.com wrote:

I know this is an old thread but I have trouble calling synth.setOutputGen from another thread. I'm using imgui (a famous GUI library) and change a generator value upon moving a slider around, which is a very practical purpose of using TonicAudio and that gives me exceptions in all places. I do see that there is a MutexLock / Unlock within setOutputGen but that doesn't seem to help at all.

If there are any examples on best usage of the Tonic api + real-time gui I would appreciate it.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/TonicAudio/Tonic/issues/270#issuecomment-329989504, or mute the thread https://github.com/notifications/unsubscribe-auth/AA9pZcUpKsI3AE6AFV_h63y07JD8eJI-ks5sjCA8gaJpZM4GnB24 .

--

Morgan Packard phone: (720) 891-0122

aramazhari commented 7 years ago

There are so many. Mostly read access violations. Running the software multiple times you will see them in different locations. Simply put, Tonic internally seem to race its own stuff. Since the only call from outside that I make is synth.setOutputGen.

Here are some of them but these are mostly coincidental. Exception Screenshots