* Little question about ALSA callbacks
@ 2005-07-27 9:28 Alfredo
2005-07-27 9:48 ` Takashi Iwai
2005-07-27 10:05 ` Joern Seger
0 siblings, 2 replies; 5+ messages in thread
From: Alfredo @ 2005-07-27 9:28 UTC (permalink / raw)
To: alsa-devel@lists.sourceforge.net
[-- Attachment #1: Type: text/plain, Size: 1647 bytes --]
Hello, I have read the article "Introduction to Sound Programming with
ALSA from Linux Journal. This article explains:
// Starts here:
------------------------------------------------------------------------
-------------------------------------------
"In the previous examples, the PCM streams were operating in blocking
mode, that is, the calls would not return until the data had been
transferred. In an interactive event-driven application, this situation
could lock up the application for unacceptably long periods of time.
ALSA allows opening a stream in nonblocking mode where the read and
write functions return immediately. If data transfers are pending and
the calls cannot be processed, ALSA returns an error code of EBUSY.
Many graphical applications use callbacks to handle events. ALSA
supports opening a PCM stream in asynchronous mode. This allows
registering a callback function to be called when a period of sample
data has been transferred."
// Finishes here:
------------------------------------------------------------------------
-------
I'm developing an event driven Linux server that will use ALSA library.
Please, could you point me to some resources in order to learn how to
use alsa in asynchronous mode (the event driven mechanism will be based
in poll()).
Clemens Ladisch helped me suggesting that I must use poll to wait for
events in different file descriptors. I need to be notified when audio
buffer has been recorded. I need to learn how to use ALSA in conjunction
with poll.
I'm a Linux developer beginner and I'm glad to use ALSA.
Thank you very much ;-)
[-- Attachment #2: Type: text/html, Size: 4258 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Little question about ALSA callbacks
2005-07-27 9:28 Little question about ALSA callbacks Alfredo
@ 2005-07-27 9:48 ` Takashi Iwai
2005-07-27 11:21 ` Alfredo
2005-07-27 10:05 ` Joern Seger
1 sibling, 1 reply; 5+ messages in thread
From: Takashi Iwai @ 2005-07-27 9:48 UTC (permalink / raw)
To: Alfredo; +Cc: alsa-devel@lists.sourceforge.net
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=ISO-2022-JP-2, Size: 2386 bytes --]
At Wed, 27 Jul 2005 11:28:26 +0200,
Alfredo wrote:
>
> Hello, I have read the article ^[$B!H^[(BIntroduction to Sound Programming with ALSA from Linux
> Journal. This article explains:
>
> // Starts here:
> -------------------------------------------------------------------------------------------------------------------
>
> ^[$B!H^[(BIn the previous examples, the PCM streams were operating in blocking mode, that is,
> the calls would not return until the data had been transferred. In an interactive
> event-driven application, this situation could lock up the application for
> unacceptably long periods of time. ALSA allows opening a stream in nonblocking mode
> where the read and write functions return immediately. If data transfers are pending
> and the calls cannot be processed, ALSA returns an error code of EBUSY.
>
> Many graphical applications use callbacks to handle events. ALSA supports opening a
> PCM stream in asynchronous mode. This allows registering a callback function to be
> called when a period of sample data has been transferred.^[$B!H^[(B
>
> // Finishes here:
> -------------------------------------------------------------------------------
>
> I^[.F^[N"m developing an event driven Linux server that will use ALSA library. Please, could
> you point me to some resources in order to learn how to use alsa in asynchronous mode
> (the event driven mechanism will be based in poll()).
>
> Clemens Ladisch helped me suggesting that I must use poll to wait for events in
> different file descriptors. I need to be notified when audio buffer has been recorded.
> I need to learn how to use ALSA in conjunction with poll.
The async mode described above uses a signal instead of poll, so this
is not a recommended way although it seems working on most of
architectures. Using poll is better.
If your app is a graphical one or do a heavy job (e.g. encoding)
together with a sound engine, you'll likely need a dedicated audio
thread for stability. In that case, consdering to use JACK would be a
good idea, too...
Takashi
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Little question about ALSA callbacks
2005-07-27 9:28 Little question about ALSA callbacks Alfredo
2005-07-27 9:48 ` Takashi Iwai
@ 2005-07-27 10:05 ` Joern Seger
2005-07-27 14:30 ` Alfredo
1 sibling, 1 reply; 5+ messages in thread
From: Joern Seger @ 2005-07-27 10:05 UTC (permalink / raw)
To: alsa-devel; +Cc: Alfredo
Hi Alfredo,
I just had the same problem here some days ago. (It was mainly about
recording)
This is my solution: (taken from a bigger project and hacked together from
some tutorials - so this is not really clean!)
opening as usual (with SND_PCM_NONBLOCK) and then do:
---- snip --------
/* tell ALSA to wake us up whenever 256 or more frames
of record data can be delivered. Also, tell
ALSA that we'll start the device ourselves.
*/
if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
fprintf (stderr, "cannot allocate software parameters structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_sw_params_current (rec_handle, sw_params)) < 0) {
fprintf (stderr, "cannot initialize software parameters structure (%s)\n",
snd_strerror (err));
exit (1);
}
int handleBytes = 256; // bytesToCapture/2;
if ((err = snd_pcm_sw_params_set_avail_min (rec_handle, sw_params,
handleBytes)) < 0) {
fprintf (stderr, "cannot set minimum available count (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_sw_params_set_start_threshold (rec_handle, sw_params,
handleBytes)) < 0) {
fprintf (stderr, "cannot set start mode (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_sw_params (rec_handle, sw_params)) < 0) {
fprintf (stderr, "cannot set software parameters (%s)\n",
snd_strerror (err));
exit (1);
}
/* the interface will interrupt the kernel every 256 Samples, and ALSA
will wake up this program very soon after that.
*/
if ((err = snd_pcm_prepare (rec_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
snd_pcm_sw_params_free (sw_params);
// now get the descriptors:
int count = snd_pcm_poll_descriptors_count(rec_handle);
slog(Slog::levelDebug)<<" count = "<<count<<oendl;
if (count <= 0) {
printf("Invalid poll descriptors count\n");
return count;
}
struct pollfd *ufds;
ufds = new pollfd[count];
if (ufds == NULL) {
printf("Not enough memory\n");
return -ENOMEM;
}
if ((err = snd_pcm_poll_descriptors(rec_handle, ufds, count)) < 0) {
printf("Unable to obtain poll descriptors for playback: %s\n",
snd_strerror(err));
return err;
}
recDesc = ufds->fd; //< this is my descriptor for select!!!
// tell scheduler, that we want to be informed on soundcard events
sched->add_select(this, recDesc);
snd_pcm_start(rec_handle);
--- snip --------
The following method will be called, if the descriptor is set via
select/FD_SET etc (see manpage for this)
--- snip --------
void alsaSoundLayer::descHandler(Event e)
{
[...]
snd_pcm_sframes_t samples_to_deliver;
if ((samples_to_deliver = snd_pcm_avail_update (rec_handle)) <= 0) {
if (samples_to_deliver == 0) {
slog(Slog::levelDebug)<<name<<": desc was called, but no data
available!?\n";
return;
}
[ ... error handling ... ]
char* samples = new char[samples_to_deliver*2]; // samples are short
if ((rc = snd_pcm_readi( rec_handle, (void*) samples, samples_to_deliver ))
< 0)
[... error handling ...]
len = rc*2;
[... do whatever with the packet ...]
return;
}
--- snip ------
Hope this helps!
regards - Jörn
Am Mittwoch, 27. Juli 2005 11:28 schrieb Alfredo:
> Hello, I have read the article "Introduction to Sound Programming with
> ALSA from Linux Journal. This article explains:
>
>
>
> // Starts here:
> ------------------------------------------------------------------------
> -------------------------------------------
>
> "In the previous examples, the PCM streams were operating in blocking
> mode, that is, the calls would not return until the data had been
> transferred. In an interactive event-driven application, this situation
> could lock up the application for unacceptably long periods of time.
> ALSA allows opening a stream in nonblocking mode where the read and
> write functions return immediately. If data transfers are pending and
> the calls cannot be processed, ALSA returns an error code of EBUSY.
>
> Many graphical applications use callbacks to handle events. ALSA
> supports opening a PCM stream in asynchronous mode. This allows
> registering a callback function to be called when a period of sample
> data has been transferred."
>
> // Finishes here:
> ------------------------------------------------------------------------
> -------
>
> I'm developing an event driven Linux server that will use ALSA library.
> Please, could you point me to some resources in order to learn how to
> use alsa in asynchronous mode (the event driven mechanism will be based
> in poll()).
>
> Clemens Ladisch helped me suggesting that I must use poll to wait for
> events in different file descriptors. I need to be notified when audio
> buffer has been recorded. I need to learn how to use ALSA in conjunction
> with poll.
>
> I'm a Linux developer beginner and I'm glad to use ALSA.
>
> Thank you very much ;-)
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_idt77&alloc_id\x16492&op=click
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: Little question about ALSA callbacks
2005-07-27 9:48 ` Takashi Iwai
@ 2005-07-27 11:21 ` Alfredo
0 siblings, 0 replies; 5+ messages in thread
From: Alfredo @ 2005-07-27 11:21 UTC (permalink / raw)
To: alsa-devel@lists.sourceforge.net
Thank you very much for your suggestions Takashi.
-----Original Message-----
From: Takashi Iwai [mailto:tiwai@suse.de]
Sent: miércoles, 27 de julio de 2005 10:48
To: Alfredo
Cc: alsa-devel@lists.sourceforge.net
Subject: Re: [Alsa-devel] Little question about ALSA callbacks
At Wed, 27 Jul 2005 11:28:26 +0200,
Alfredo wrote:
>
> Hello, I have read the article ^[$B!H^[(BIntroduction to Sound
Programming with ALSA from Linux
> Journal. This article explains:
>
> // Starts here:
>
------------------------------------------------------------------------
-------------------------------------------
>
> ^[$B!H^[(BIn the previous examples, the PCM streams were operating in
blocking mode, that is,
> the calls would not return until the data had been transferred. In an
interactive
> event-driven application, this situation could lock up the application
for
> unacceptably long periods of time. ALSA allows opening a stream in
nonblocking mode
> where the read and write functions return immediately. If data
transfers are pending
> and the calls cannot be processed, ALSA returns an error code of
EBUSY.
>
> Many graphical applications use callbacks to handle events. ALSA
supports opening a
> PCM stream in asynchronous mode. This allows registering a callback
function to be
> called when a period of sample data has been transferred.^[$B!H^[(B
>
> // Finishes here:
>
------------------------------------------------------------------------
-------
>
> I^[.F^[N"m developing an event driven Linux server that will use ALSA
library. Please, could
> you point me to some resources in order to learn how to use alsa in
asynchronous mode
> (the event driven mechanism will be based in poll()).
>
> Clemens Ladisch helped me suggesting that I must use poll to wait for
events in
> different file descriptors. I need to be notified when audio buffer
has been recorded.
> I need to learn how to use ALSA in conjunction with poll.
The async mode described above uses a signal instead of poll, so this
is not a recommended way although it seems working on most of
architectures. Using poll is better.
If your app is a graphical one or do a heavy job (e.g. encoding)
together with a sound engine, you'll likely need a dedicated audio
thread for stability. In that case, consdering to use JACK would be a
good idea, too...
Takashi
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_idt77&alloc_id\x16492&op=click
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: Little question about ALSA callbacks
2005-07-27 10:05 ` Joern Seger
@ 2005-07-27 14:30 ` Alfredo
0 siblings, 0 replies; 5+ messages in thread
From: Alfredo @ 2005-07-27 14:30 UTC (permalink / raw)
To: Joern Seger, alsa-devel@lists.sourceforge.net
Thank you very much! Your help is really appreciated.
I will improve my knowledge to help other developers.
-----Original Message-----
From: Joern Seger [mailto:j.seger@gmx.de]
Sent: miércoles, 27 de julio de 2005 11:06
To: alsa-devel@lists.sourceforge.net
Cc: Alfredo
Subject: Re: [Alsa-devel] Little question about ALSA callbacks
Hi Alfredo,
I just had the same problem here some days ago. (It was mainly about
recording)
This is my solution: (taken from a bigger project and hacked together
from
some tutorials - so this is not really clean!)
opening as usual (with SND_PCM_NONBLOCK) and then do:
---- snip --------
/* tell ALSA to wake us up whenever 256 or more frames
of record data can be delivered. Also, tell
ALSA that we'll start the device ourselves.
*/
if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
fprintf (stderr, "cannot allocate software parameters structure
(%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_sw_params_current (rec_handle, sw_params)) < 0) {
fprintf (stderr, "cannot initialize software parameters structure
(%s)\n",
snd_strerror (err));
exit (1);
}
int handleBytes = 256; // bytesToCapture/2;
if ((err = snd_pcm_sw_params_set_avail_min (rec_handle, sw_params,
handleBytes)) < 0) {
fprintf (stderr, "cannot set minimum available count (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_sw_params_set_start_threshold (rec_handle,
sw_params,
handleBytes)) < 0) {
fprintf (stderr, "cannot set start mode (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_sw_params (rec_handle, sw_params)) < 0) {
fprintf (stderr, "cannot set software parameters (%s)\n",
snd_strerror (err));
exit (1);
}
/* the interface will interrupt the kernel every 256 Samples, and ALSA
will wake up this program very soon after that.
*/
if ((err = snd_pcm_prepare (rec_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
snd_pcm_sw_params_free (sw_params);
// now get the descriptors:
int count = snd_pcm_poll_descriptors_count(rec_handle);
slog(Slog::levelDebug)<<" count = "<<count<<oendl;
if (count <= 0) {
printf("Invalid poll descriptors count\n");
return count;
}
struct pollfd *ufds;
ufds = new pollfd[count];
if (ufds == NULL) {
printf("Not enough memory\n");
return -ENOMEM;
}
if ((err = snd_pcm_poll_descriptors(rec_handle, ufds, count)) < 0) {
printf("Unable to obtain poll descriptors for playback: %s\n",
snd_strerror(err));
return err;
}
recDesc = ufds->fd; //< this is my descriptor for select!!!
// tell scheduler, that we want to be informed on soundcard events
sched->add_select(this, recDesc);
snd_pcm_start(rec_handle);
--- snip --------
The following method will be called, if the descriptor is set via
select/FD_SET etc (see manpage for this)
--- snip --------
void alsaSoundLayer::descHandler(Event e)
{
[...]
snd_pcm_sframes_t samples_to_deliver;
if ((samples_to_deliver = snd_pcm_avail_update (rec_handle)) <= 0) {
if (samples_to_deliver == 0) {
slog(Slog::levelDebug)<<name<<": desc was called, but no data
available!?\n";
return;
}
[ ... error handling ... ]
char* samples = new char[samples_to_deliver*2]; // samples are short
if ((rc = snd_pcm_readi( rec_handle, (void*) samples,
samples_to_deliver ))
< 0)
[... error handling ...]
len = rc*2;
[... do whatever with the packet ...]
return;
}
--- snip ------
Hope this helps!
regards - Jörn
Am Mittwoch, 27. Juli 2005 11:28 schrieb Alfredo:
> Hello, I have read the article "Introduction to Sound Programming with
> ALSA from Linux Journal. This article explains:
>
>
>
> // Starts here:
>
------------------------------------------------------------------------
> -------------------------------------------
>
> "In the previous examples, the PCM streams were operating in blocking
> mode, that is, the calls would not return until the data had been
> transferred. In an interactive event-driven application, this
situation
> could lock up the application for unacceptably long periods of time.
> ALSA allows opening a stream in nonblocking mode where the read and
> write functions return immediately. If data transfers are pending and
> the calls cannot be processed, ALSA returns an error code of EBUSY.
>
> Many graphical applications use callbacks to handle events. ALSA
> supports opening a PCM stream in asynchronous mode. This allows
> registering a callback function to be called when a period of sample
> data has been transferred."
>
> // Finishes here:
>
------------------------------------------------------------------------
> -------
>
> I'm developing an event driven Linux server that will use ALSA
library.
> Please, could you point me to some resources in order to learn how to
> use alsa in asynchronous mode (the event driven mechanism will be
based
> in poll()).
>
> Clemens Ladisch helped me suggesting that I must use poll to wait for
> events in different file descriptors. I need to be notified when audio
> buffer has been recorded. I need to learn how to use ALSA in
conjunction
> with poll.
>
> I'm a Linux developer beginner and I'm glad to use ALSA.
>
> Thank you very much ;-)
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_idt77&alloc_id\x16492&op=click
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-07-27 14:30 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-07-27 9:28 Little question about ALSA callbacks Alfredo
2005-07-27 9:48 ` Takashi Iwai
2005-07-27 11:21 ` Alfredo
2005-07-27 10:05 ` Joern Seger
2005-07-27 14:30 ` Alfredo
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.