ALSA: I need a proper example of low-latency loop for DSP

Programming applications for making music on Linux.

Moderators: MattKingUSA, khz

Post Reply
fxc
Established Member
Posts: 4
Joined: Fri Jun 28, 2019 6:54 pm

ALSA: I need a proper example of low-latency loop for DSP

Post by fxc »

Hello everybody.

I'm writing, for the sake of fun and understanding how things works, my own audio DSP to apply effects to my guitar. I'm sure I'm not the only one here that has done something similar :-)

Currently my "audio engine" is out of the prototype stage and works the way I like, but I have some reliability problems. I was about to start a topic here regarding what happens but, thinking about it, what I'm trying to build is a pretty common thing and my knowledge of ALSA is honestly still not enough.

Essentially, I need an audio loop that:
- has, obviously, the lowest latency possible;
- is able to recover properly from conditions where the AUDIO IN + DSP CALCULATIONS + AUDIO OUT pass couldn't be performed in time for any reason (this is the reliability test my code fails).

So I'm looking for one (or more) good examples and/or tutorials to study and follow to achieve and understand a low-latency DSP. I looked into the examples in the ALSA documentations but, honestly, aren't enough clear or well written. Any suggestions?

Thanks.
j_e_f_f_g
Established Member
Posts: 2032
Joined: Fri Aug 10, 2012 10:48 pm
Been thanked: 357 times

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by j_e_f_f_g »

You need to use MMAP mode for audio input/output.

Author of BackupBand at https://sourceforge.net/projects/backupband/files/
My fans show their support by mentioning my name in their signature.

fxc
Established Member
Posts: 4
Joined: Fri Jun 28, 2019 6:54 pm

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by fxc »

I'll look into it, but I don't get how MMAP could solve my problems. Anyway, thanks.
User avatar
sadko4u
Established Member
Posts: 986
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 359 times

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by sadko4u »

Why ALSA but not JACKD?
LSP (Linux Studio Plugins) Developer and Maintainer.
fxc
Established Member
Posts: 4
Joined: Fri Jun 28, 2019 6:54 pm

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by fxc »

Because the value added by jackd - its ability to route audio signals - is useless in my context. I need 1 mono input, do some DSP calculations, and 1 mono output. Nothing else. I can do it with jackd, but it would be one more software layer with no benefit.
User avatar
Markus
Established Member
Posts: 81
Joined: Tue Jul 21, 2015 9:29 am

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by Markus »

To be honest: I absolutely hate postings like I'm going to write now. Because you were asking for help with ALSA and nothing else. If you're bored by answers leading away from your initial question - please skip this one b/c it's of no help at all.

I'd suggest an Jack client, too, maybe even an established DSP wrapper like LADSPA or LV2. Why? Because of ease. It's a horror to deal with ALSA, as you already pointed out. Shitty documentation, no sources to really learn from - it's learning by failing. Went through it partially for reasons and it sucks. Additionally you'll be stuck in your tiny bubble, even try to move your stuff to another machine with a different HDA chip. What else? No play-alongs, no recordings, no external DSP, no fail-safe. And you already have to deal with a software layer - which is ALSA in this case.

That's why I would suggest building a client for a flexible low-latency-capable audio server like jack. Or even better, because even more flexibility, a plug-in to be used in any other host. You'll get results in no time, be able to concentrate on the core of your application, can integrate into other environments, get effortless portability and much more. Routing is not the main point here but getting usable things done. Better investing time into what you really want to achieve than into trying to figure out how to deal with low level API.

But this is just my personal opinion and, as I said, I normally hate reading this kind of "answer" so please bear with me going off-topic.
User avatar
SpotlightKid
Established Member
Posts: 250
Joined: Sun Jul 02, 2017 1:24 pm
Has thanked: 48 times
Been thanked: 54 times

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by SpotlightKid »

Markus wrote:maybe even an established DSP wrapper like LADSPA or LV2. Why?
Unless you're targetting a non-Linux embedded environment (but then you wouldn't be able to use ALSA as well), make that a definitely. For one thing, you don't have to worry about implementing dynamic parameter control via MIDI or other protocols, because the plugin host handles that for you.
User avatar
sadko4u
Established Member
Posts: 986
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 359 times

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by sadko4u »

SpotlightKid wrote: Unless you're targetting a non-Linux embedded environment (but then you wouldn't be able to use ALSA as well), make that a definitely. For one thing
JACKD teaches how to write proper audio applications and audio processors. Here's the receipt:
1. Connect to JACK.
2. Define your input and output buffers which, actually, are PORTS in JACK terminology.
3. Define a callback routine that will take data from input buffers, process it and store data to output buffers.
4. ...
5. PROFIT!

If you want to port your solution to pure ALSA, you'll have no problems because you'll just need to write wrapper that prepares data in data buffers and runs your callback function.
you don't have to worry about implementing dynamic parameter control via MIDI or other protocols, because the plugin host handles that for you.
Wrong. Especially for smooth parameter automation in a DAW.
LSP (Linux Studio Plugins) Developer and Maintainer.
User avatar
raboof
Established Member
Posts: 1855
Joined: Tue Apr 08, 2008 11:58 am
Location: Deventer, NL
Has thanked: 50 times
Been thanked: 74 times
Contact:

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by raboof »

fxc wrote:is able to recover properly from conditions where the AUDIO IN + DSP CALCULATIONS + AUDIO OUT pass couldn't be performed in time for any reason (this is the reliability test my code fails).
This is an interesting goal, but as far as I know this is basically research territory. I don't think I know any software that currently does this.

That's not to say it is impossible. I think there are 2 parts to this problem:

- how to find out you are about to miss your deadline. the ALSA API of course has a way to signal xruns (buffer underruns), but at that stage you are already too late. You could try somehow looking at the wall clock, but at low buffer sizes even looking at the wall clock might be a dangerous operation for your deadline...

- what to do when you somehow find you are about to miss your deadline. I guess your goal is to reduce audible artifacts. That will be hard, though, since the whole point was that you didn't succeed in processing the input in time. I think what would work highly depends on what your algorithm looks like. Some blend of the processed data from the last frame and the clean signal of the current frame might be an approximation to start with, also if your algorithm works in 'rounds' perhaps you could take into account the intermediate signal even though you haven't finished all 'rounds' - but it obviously won't be perfect.
fxc wrote:So I'm looking for one (or more) good examples and/or tutorials to study and follow to achieve and understand a low-latency DSP.
I don't know of any software that does what you describe, much less one that would be a good example or a tutorial ;)
User avatar
SpotlightKid
Established Member
Posts: 250
Joined: Sun Jul 02, 2017 1:24 pm
Has thanked: 48 times
Been thanked: 54 times

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by SpotlightKid »

sadko4u wrote:Here's the receipt:
1. [...]
How's that different from a writing a plugin? Especially, if you're using a framework, that warps the differences of the various plugin standards. Ok, you don't have to allocate the input and output buffers yourself. Big deal.
SpotlightKid wrote:you don't have to worry about implementing dynamic parameter control via MIDI or other protocols, because the plugin host handles that for you.
[/quote]Wrong. Especially for smooth parameter automation in a DAW.[/quote]

I meant, you don't have to worry about parsing MIDI events and CC (or OSC address etc.) to parameter mapping. Of course, you still need to worry about stepping and clicks, due to sudden parameter changes.
User avatar
sadko4u
Established Member
Posts: 986
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 359 times

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by sadko4u »

SpotlightKid wrote:How's that different from a writing a plugin? Especially, if you're using a framework, that warps the differences of the various plugin standards. Ok, you don't have to allocate the input and output buffers yourself. Big deal.
Actually, that's it. You write something similar to a plugin and focus on a DSP code and on the UI. Then you easily can port your changes to any other plugin format. And also you can write an application which wraps your plugin and works... just as a standalone application.
After I've implemented JACK support in LSP Plugins, I dramatically reduced the amount of time I need to spend debugging the code. I don't need any host. I just set up breakpoints in my favourite IDE and run standalone version of plugin that I'm currently working on.
SpotlightKid wrote:I meant, you don't have to worry about parsing MIDI events and CC (or OSC address etc.) to parameter mapping. Of course, you still need to worry about stepping and clicks, due to sudden parameter changes.
You have to worry about parsing MIDI events and CC.
In LV2, there are LV2:Atoms that are used to implement MIDI transport.
In VST, there are callbacks that deliver MIDI events to your plugin.
In JACK, you have separate MIDI buffers and you have to worry about the time order of events because JACK requires MIDI events to be sorted chronologically.
The same is true about OSC. For example, LV2 implements LV2:Atom-based transport of OSC events. In JACK, there are no special OSC buffers, so you need to implement some OSC server or use third-party libraries like liblo.
LSP (Linux Studio Plugins) Developer and Maintainer.
fxc
Established Member
Posts: 4
Joined: Fri Jun 28, 2019 6:54 pm

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by fxc »

Thank you guys for your answers.

As I said in the first post, my knowledge of ALSA is still very limited. I still have to fully realize how difficult it is to use it. Till now, honestly, it wasn't so hard. So I asked for help, naively thinking it shouldn't be so hard solving this problem.

The basic reason of what I'm doing is to have fun writing code and, most important, learn something. Otherwise I'd just use Guitarix and save even more time that using Jackd would save me.
I don't want to use something that routes signals, I wanna route signals by myself. I want to interface my code to DSP plugins directly (and interfacing to LADSPA was, actually, surprisingly easy; apart from that absurd logic of the default parameter value). I even designed my own plugin format. Why? because it is a f***ing good way to understand things. LV2 will come in the next future.

I think I gave a reasonable motivation why I prefer stick with ALSA and keep Jackd aside, so I don't get these slightly angry answers.

Anyway, thank you for pointing me to the dangers of ALSA and the reasons to give a look to alternatives.
User avatar
SpotlightKid
Established Member
Posts: 250
Joined: Sun Jul 02, 2017 1:24 pm
Has thanked: 48 times
Been thanked: 54 times

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by SpotlightKid »

What I'm saying is that MIDI/OSC to parameter routing should be left to the plugin host, unless you have special needs. If you only have normal int/float/list parameters, no need to do that in the plugin yourself.
User avatar
sadko4u
Established Member
Posts: 986
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 359 times

Re: ALSA: I need a proper example of low-latency loop for DSP

Post by sadko4u »

SpotlightKid wrote:What I'm saying is that MIDI/OSC to parameter routing should be left to the plugin host, unless you have special needs. If you only have normal int/float/list parameters, no need to do that in the plugin yourself.
That is, but OSC is more than just parameter mapping. OSC is pretty simple at the first look but very complicated by the other side. The OSC message allows to send more than one parameter. Moreover, messages can be bundled together and have deep nesting.
Again, if you write ALSA/JACK application, you have to be with your own and think about how you will handle OSC messages.
Anyway, it's an offtopic now. We're not discussing OSC here. We're discussing why JACK is better option to start with.
LSP (Linux Studio Plugins) Developer and Maintainer.
Post Reply