A synth without threads

Programming applications for making music on Linux.

Moderators: MattKingUSA, khz

Post Reply
wboe
Established Member
Posts: 3
Joined: Sat Sep 05, 2015 1:08 pm

A synth without threads

Post 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
tramp
Established Member
Posts: 2335
Joined: Mon Jul 01, 2013 8:13 am
Has thanked: 9 times
Been thanked: 454 times

Re: A synth without threads

Post 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?
On the road again.
wboe
Established Member
Posts: 3
Joined: Sat Sep 05, 2015 1:08 pm

Re: A synth without threads

Post 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
tramp
Established Member
Posts: 2335
Joined: Mon Jul 01, 2013 8:13 am
Has thanked: 9 times
Been thanked: 454 times

Re: A synth without threads

Post 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.
On the road again.
Post Reply