Page 1 of 1

A synth without threads

Posted: Sat Dec 05, 2020 4:21 pm
by wboe
Hi,

Has somebody ever tried to make a synth (or some other instrument) without using threads? Usually there is at least one thread for the audio and one e.g. for the midi keyboard.

I tied to do it in a single-threaded program, and it worked. The audio buffer that was needed in normal threaded programs is 512 frames (on my Linux box, no RT, sample rate 44100 Hz), but now it appeared that 128 frames was enough. That's a very low latency.

The trick is to use sytem call read(), with parameter O_RDONLY | O_NONBLOCK. The audio loop is blocking on Alsa function snd_pcm_writei().

This should be a well-known design method, but I never saw it yet.

Wouter Boeke

Re: A synth without threads

Posted: Sat Dec 05, 2020 4:50 pm
by tramp
That only work when you are the only one. as soon the PC get some work to do, it explode.
And, usual there is one thread for audio/midi, one for the GUI and one worker thread (were you call read or such stuff).
On most modern PC's that will leave at least one Core untouched for other stuff to do.
The question which remain is: why would you do that all in one thread? What advance should it have?

Re: A synth without threads

Posted: Sat Dec 05, 2020 5:50 pm
by wboe
The first motivation for single-threading was: simpler code. No mutexes, no synchronisation, everything predictable. The fact that such a small audio buffer was possible came as an extra bonus.

I think that a real GUI can operate also in this setup. In my projects the GUI always runs in the main thread. I posted this question to investigate whether I was inventing the wheel again, or not.

Wouter Boeke

Re: A synth without threads

Posted: Sat Dec 05, 2020 6:20 pm
by tramp
wboe wrote: Sat Dec 05, 2020 5:50 pm The first motivation for single-threading was: simpler code. No mutexes, no synchronisation, everything predictable. The fact that such a small audio buffer was possible came as an extra bonus.

I think that a real GUI can operate also in this setup. In my projects the GUI always runs in the main thread. I posted this question to investigate whether I was inventing the wheel again, or not.

Wouter Boeke
So you are doing plain ALSA, I guess.
As I said, as long you are alone, your approve may work, but, there is a reason why open-mp was developed, and why developers prefer a multi threading approve.
Keep in mind, development is not about code, it is about data and it's handing.