ALSA output frame size

Moderators: MattKingUSA, khz, muldjord, deva

Post Reply
corrados
Established Member
Posts: 39
Joined: Sat Jan 02, 2021 4:01 pm

ALSA output frame size

Post by corrados »

As far as I understood, if I use the jack audio interface, the Drumgizmo internal frame size (block size) is the same as the jack audio server uses, right? But what frame size is used if the ALSA output is selected? I assume the default value of 1024 is used since the latency seems to be very high if I use the ALSA output interface (even if I select "-O frames=2"). Is there a way to configure the frame size with the CLI?
User avatar
deva
Established Member
Posts: 285
Joined: Sun Oct 23, 2016 10:15 am
Has thanked: 3 times
Been thanked: 31 times
Contact:

Re: ALSA output frame size

Post by deva »

You should be able to set the framesize with the -O parameter. If this is not the case then perhaps there is a bug in the alsa output module.
I don't think it has been used much, so it is entirely possible that this part of the code has never worked as intended without any of us noticing...
corrados
Established Member
Posts: 39
Joined: Sat Jan 02, 2021 4:01 pm

Re: ALSA output frame size

Post by corrados »

Yes, we can set the frame size with the -O parameter (which is then actually the period size since we use the snd_pcm_hw_params_set_period_size_near() function). According to https://www.alsa-project.org/wiki/FramesPeriods, a frame is just a sample if we assume a mono stream and the period size is basically the block size, i.e., what we set with the "-O frame" parameter. But then you could also specify the number of periods in the buffer with snd_pcm_hw_params_set_periods_near as far as I understand. If we do not set it explicitely, I am not sure what is then used in the ALSA interface.

Anyway, in the ALSA output module run() function, we provide the ALSA interface with a sample buffer of size nsamples and send this buffer to the ALSA interface with the snd_pcm_writei() function. The question is, can I modify the parameter nsamples? As far as I understand, the minimum latency between the MIDI input and ALSA output is depending on the nsamples number, the ALSA period size and the number of ALSA periods, right? So, currently I can change the period size with the CLI but not the other parameters. Or maybe I have missed something obvious.
User avatar
deva
Established Member
Posts: 285
Joined: Sun Oct 23, 2016 10:15 am
Has thanked: 3 times
Been thanked: 31 times
Contact:

Re: ALSA output frame size

Post by deva »

At initialisation timt the engine will ask the output module which ramesize to use through the getBufferSize() method. But for the alsa output module this does not seem to be implemented... which will hit the base-class (in src/audiooutputengine.h) implementation which always return 1024.
So you can try adding that method and return the actual framesize and see if that helps?
corrados
Established Member
Posts: 39
Joined: Sat Jan 02, 2021 4:01 pm

Re: ALSA output frame size

Post by corrados »

Yes, you are right. The ALSA out interface does not yet implement the getBufferSize function. I've added it to the following branch: http://git.drumgizmo.org/drumgizmo.git/ ... aoutbuffer
User avatar
deva
Established Member
Posts: 285
Joined: Sun Oct 23, 2016 10:15 am
Has thanked: 3 times
Been thanked: 31 times
Contact:

Re: ALSA output frame size

Post by deva »

Great. I have merged it to develop :-)
corrados
Established Member
Posts: 39
Joined: Sat Jan 02, 2021 4:01 pm

Re: ALSA output frame size

Post by corrados »

Have you used a cherry-pick for putting my commit on the develop branch because I did not see any merge commit?
Should I now remove my branch and the commit on my branch will then be deleted by the git garbage collector?

BTW: I found out that there are more changes to the ALSA output interface necessary. I'll do some more tests and will create another branch for it soon.
User avatar
deva
Established Member
Posts: 285
Joined: Sun Oct 23, 2016 10:15 am
Has thanked: 3 times
Been thanked: 31 times
Contact:

Re: ALSA output frame size

Post by deva »

We alsways use rebase with develop and then fast-forward merge
User avatar
deva
Established Member
Posts: 285
Joined: Sun Oct 23, 2016 10:15 am
Has thanked: 3 times
Been thanked: 31 times
Contact:

Re: ALSA output frame size

Post by deva »

corrados wrote: Tue Feb 09, 2021 3:39 pm BTW: I found out that there are more changes to the ALSA output interface necessary. I'll do some more tests and will create another branch for it soon.
Cool! You're on fire :-D
corrados
Established Member
Posts: 39
Joined: Sat Jan 02, 2021 4:01 pm

Re: ALSA output frame size

Post by corrados »

Here are my new changes: http://git.drumgizmo.org/drumgizmo.git/ ... 4c51cea44d
With this change, low latency audio is now possible with my audio hardware.
User avatar
deva
Established Member
Posts: 285
Joined: Sun Oct 23, 2016 10:15 am
Has thanked: 3 times
Been thanked: 31 times
Contact:

Re: ALSA output frame size

Post by deva »

Awesome :-)

Do you know what the periods was before, by default I mean?
corrados
Established Member
Posts: 39
Joined: Sat Jan 02, 2021 4:01 pm

Re: ALSA output frame size

Post by corrados »

On my PC I had a default value of 1024 which gave me more than a second latency.
User avatar
deva
Established Member
Posts: 285
Joined: Sun Oct 23, 2016 10:15 am
Has thanked: 3 times
Been thanked: 31 times
Contact:

Re: ALSA output frame size

Post by deva »

That seems completely useless ... I definitely vote for the 3 periods default that you put in.
User avatar
deva
Established Member
Posts: 285
Joined: Sun Oct 23, 2016 10:15 am
Has thanked: 3 times
Been thanked: 31 times
Contact:

Re: ALSA output frame size

Post by deva »

It has been merged to develop now. (Also) Great job :-)
Post Reply