Alsa-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Jack O'Quin" <joq@io.com>
To: "Asbjørn Sæbø" <asbjs@stud.ntnu.no>
Cc: alsa-devel@lists.sourceforge.net
Subject: Re: Lowest latency: JACK, or ALSA directly?
Date: 12 Nov 2004 10:42:33 -0600	[thread overview]
Message-ID: <87oei3xbly.fsf@sulphur.joq.us> (raw)
In-Reply-To: <20041112090416.GA3167@stud.ntnu.no>

asbjs@stud.ntnu.no (Asbjørn Sæbø) writes:

> On Thu, Nov 11, 2004 at 09:11:28AM -0600, Jack O'Quin wrote:
> > JACK *does* constrain the ALSA period to a power of two.  If that is
> > not your ideal buffer size, then you could experience extra latency
> > due to that.
> 
> By "period" you mean the size of the period (in frames), not the number 
> of periods, right?

Right.  That is ALSA terminology, IIUC.

> (As an aside, there is another thing that is not clear to me.  A set of 
> samples from all channels is called a frame, and a set of frames is a 
> period.  But the total buffer is made up from a set of periods again.  
> How does this buffer function? Does one get data from the sound card 
> each and every period (which I believe), or only when the buffer is 
> full?  Do you get the data from a period when it is full, or do you 
> have to wait some periods?)

The sound card generates an interrupt at the end of each period.  This
determines the input latency.  The hardware buffer holds multiple
periods (you need at least two).  Its size determines the maximum
output latency.  With JACK, there's not much reason to use more than
two or three periods, unless you're running without realtime
privileges.

> > [...]
> > The JACK interface is simpler, but you have to get comfortable with
> > programming using callbacks.  Here's a simple JACK example client...
> > 
> >   http://cvs.sourceforge.net/viewcvs.py/jackit/jack/example-clients/simple_client.c?rev=1.15&view=markup
> 
> That was quite understandable, at least the callback principle.  I 
> do have a few questions, though.  
> 
> I can not see the application setting any parameters, like f.i. the 
> number of channels.  Does it just accept whatever data it is given from 
> jack?  

It defines its own input and output ports using jack_port_register().
The example client has one of each.  These are independent of
hardware, the input may come from one or more other clients or go
somewhere else.  The example client then checks for physical capture
and playback ports, the hardware input and output channels.  If there
are any, it connects to one of each.

> How does the application know what the data it receives from jack are
> (number of frames, number of channels)?  (In the case it is supposed to
> be processing them, not just passing them on?)

The number of frames is a parameter to the process() callback.  That
could change from one cycle to the next.  

If you need to know in advance (perhaps to set internal buffer sizes),
call jack_get_buffer_size() to reveal the current limit and register a
callback with jack_set_buffer_size_callback() before activating your
process() callback...

  http://jackit.sourceforge.net/docs/reference/html/jack_8h.html#a13

> The program I have now is very simple, it just reads samples from the
> sound card and writes them to a network socket.  If jackified, would it
> in essence do the same thing, only using jack as an asbtraction layer
> over ALSA?  Or would it be splitted into two parts, one that reads the
> samples (and sends them to jack) and one that receives samples from jack
> and sends them to the network?

If you only send samples, I suppose it would work similarly to the
JACK example capture_client.  Define JACK input ports for each
channel.  You could automatically connect them to hardware inputs, or
you could connect them externally using qjackctl or some other
patchbay client.

You will need a separate thread for writing to the network socket.
Otherwise, it might block and blow the realtime response deadline of
the process cycle.  The capture_client.c example uses a ringbuffer to
send data from the process thread to its disk writer thread...

  http://cvs.sourceforge.net/viewcvs.py/jackit/jack/example-clients/capture_client.c?view=markup

> > [...]
> > Even if you don't want to connect other programs, they might want to
> > connect to yours.
> 
> How can they?  If I write an application that reads data from the 
> soundcard using jack, can other programs intercept the data?  (That 
> could of course be very useful in some circumstances.)

Yes, the user can alter the JACK connection graph at any time.  Your
program doesn't need to know about that, unless it wants to.
-- 
  joq


-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_idU88&alloc_id\x12065&op=click

  reply	other threads:[~2004-11-12 16:42 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-11 14:41 Lowest latency: JACK, or ALSA directly? Asbjørn Sæbø
2004-11-11 15:11 ` Jack O'Quin
2004-11-12  9:04   ` Asbjørn Sæbø
2004-11-12 16:42     ` Jack O'Quin [this message]
2004-11-12 17:01       ` Takashi Iwai
2004-11-12 17:19         ` Jack O'Quin
2004-11-17 14:55           ` Takashi Iwai
2004-11-17 17:32             ` Jack O'Quin
2004-11-17 18:28               ` Takashi Iwai
2004-11-17 19:41                 ` Takashi Iwai
2004-11-18  2:07                   ` Jack O'Quin
2004-11-18 12:38                     ` Takashi Iwai
2004-11-11 15:34 ` Paul Davis
2004-11-12  9:11   ` Asbjørn Sæbø
2004-11-12 14:47   ` Gilles Degottex
2004-11-12 15:28     ` Giuliano Pochini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87oei3xbly.fsf@sulphur.joq.us \
    --to=joq@io.com \
    --cc=alsa-devel@lists.sourceforge.net \
    --cc=asbjs@stud.ntnu.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox