Skip to contentSkip to conversation

Advice on using ALSA with callbacks?

DISCUSSION Dave Raggett 3 participants 6 messages Dec 28, 2005 – Dec 29, 2005

lore
Share & tools
lore
#0 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I am writing an audio streaming application for speech synthesis and recognition, and wish to take advantage of ALSA (my code currently uses OSS). The networking code is driven by the poll system call (see man poll) and uses ring buffers for reading and writing.

I would like to use call backs for when ALSA has captured a frame of audio data from the microphone, or when ALSA is ready for a frame of audio data for playback. I am using single channel audio with 8000 samples a second and signed 16 bit linear little endian samples.

My expectation is that I don't need to access ALSA from a separate thread from the networking code that is driven with the poll system call. Once ALSA has been correctly initialized, it would result in asynchronous calls to the call back routines. I could then avoid the need for locks on the ring buffers that these routines share with the network code.

An alternative would be to provide a separate thread and for it to use blocking calls to ALSA, but this doesn't feel like the best way to go. It would also need one thread for playback and another for capture (for use with speech recognition). I am therefore keen to learn how to implement a single threaded callback solution.

The tutorials by Paul Davis are nice and clear, but unfortunately don't cover the use of call backs. Is there a relatively simple example program I could look at for what I need to do?

Any advice would be much appreciated.

- --
Dave Raggett <[email protected]> W3C lead for multimodal interaction
http://www.w3.org/People/Raggett +44 1225 866240 (or 867351)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFDstUJb3AdEmxAsUsRAg7SAKDrE/hNJy5NxxAarr753gVOoE6qsgCcDfFR KqdWbkiVDyvaEpcHsa5T8P4=
=jFug
-----END PGP SIGNATURE-----

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click

#1 lore raw
Quoted 5 lines · Dave Raggett

The tutorials by Paul Davis are nice and clear, but unfortunately don't cover the use of call backs. Is there a relatively simple example program I could look at for what I need to do?

Any advice would be much appreciated.

My advice is to use poll() also for the audio data. It's not really required to use an asynchronous source for your application.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click

#2 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 10 lines · Jaroslav Kysela
Quoted 3 lines · Dave Raggett

The tutorials by Paul Davis are nice and clear, but unfortunately don't cover the use of call backs. Is there a relatively simple example program I could look at for what I need to do?

Any advice would be much appreciated.

My advice is to use poll() also for the audio data. It's not really required to use an asynchronous source for your application.

Jaroslav

Thanks for the swift response.

I am currently using poll() with OSS but don't understand how to get this to make this work with ALSA. In OSS it is easy to get a file descriptor for use with poll by opening /dev/audio. Is there something analogous for use with ALSA? Can ALSA be set up in such a way that poll only sees the file descriptor as ready for reading when a complete frame is available, and likewise to only become ready for writing when there is space for a complete frame of playback data?

Many thanks,
- --
Dave Raggett <[email protected]> W3C lead for multimodal interaction
http://www.w3.org/People/Raggett +44 1225 866240 (or 867351)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFDstkcb3AdEmxAsUsRAqRKAKDSCtSyFjYb+hlwvQWX105kmQ1rtQCgpawS Ovz++sL1MJAG6+O7xc5P/UE=
=SmNZ
-----END PGP SIGNATURE-----

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click

#3 lore raw
Quoted 20 lines · Dave Raggett

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 6 lines · Jaroslav Kysela
Quoted 4 lines · Dave Raggett

The tutorials by Paul Davis are nice and clear, but unfortunately don't cover the use of call backs. Is there a relatively simple example program I could look at for what I need to do?

Quoted 6 lines
Quoted 1 line

Any advice would be much appreciated.

My advice is to use poll() also for the audio data. It's not really required to use an asynchronous source for your application.

Jaroslav

Thanks for the swift response.

Quoted 8 lines

I am currently using poll() with OSS but don't understand how to get this to make this work with ALSA. In OSS it is easy to get a file descriptor for use with poll by opening /dev/audio. Is there something analogous for use with ALSA? Can ALSA be set up in such a way that poll only sees the file descriptor as ready for reading when a complete frame is available, and likewise to only become ready for writing when there is space for a complete frame of playback data?

Yes, look to the example code - alsa-lib/test/pcm.c and functions

snd_pcm_poll_descriptors_count()
snd_pcm_poll_descriptors()
snd_pcm_poll_descriptors_revents()

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click

#4 lore raw
In reply to Dave Raggett

Hello,

Quoted 8 lines · Dave Raggett

I am currently using poll() with OSS but don't understand how to get this to make this work with ALSA. In OSS it is easy to get a file descriptor for use with poll by opening /dev/audio. Is there something analogous for use with ALSA? Can ALSA be set up in such a way that poll only sees the file descriptor as ready for reading when a complete frame is available, and likewise to only become ready for writing when there is space for a complete frame of playback data?

yes, as Jaroslav already replied, snd_pcm_poll_descriptors() does just the thing.

To configure the wake-up threshold, you need to configure the "avail-min" property with snd_pcm_sw_params_set_avail_min() and friends. Additionally it's good to set the period size (usually the interrupt frequency) to match your blocksize with snd_pcm_hw_params_set_period_*() functions.

All in all, I've used ALSA in various networked apps that use a centralized poll/select driven event-loop, and the alsa-lib API is very nice (things like avail-min are very useful and not supported by OSS).

--
under work: Sofia-SIP at http://sofia-sip.sf.net

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click

#5 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Many thanks. Both you and Jaroslav have been very helpful. My goal is to enable network based speech recognition and synthesis services for web applications as part of my work for W3C. I am exploring passing the audio either as part of HTTP requests and responses, or via a separate RTP stream. The code will be available under W3C's open source license.

Quoted 13 lines · Kai Vehmanen

Hello,

Quoted 8 lines · Dave Raggett

I am currently using poll() with OSS but don't understand how to get this to make this work with ALSA. In OSS it is easy to get a file descriptor for use with poll by opening /dev/audio. Is there something analogous for use with ALSA? Can ALSA be set up in such a way that poll only sees the file descriptor as ready for reading when a complete frame is available, and likewise to only become ready for writing when there is space for a complete frame of playback data?

Quoted 5 lines

yes, as Jaroslav already replied, snd_pcm_poll_descriptors() does just the thing.

To configure the wake-up threshold, you need to configure the "avail-min" property with snd_pcm_sw_params_set_avail_min() and friends. Additionally it's good to set the period size (usually the interrupt frequency) to match your blocksize with snd_pcm_hw_params_set_period_*() functions.

All in all, I've used ALSA in various networked apps that use a centralized poll/select driven event-loop, and the alsa-lib API is very nice (things like avail-min are very useful and not supported by OSS).

- --
Dave Raggett <[email protected]> W3C lead for multimodal interaction
http://www.w3.org/People/Raggett +44 1225 866240 (or 867351)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFDs8ASb3AdEmxAsUsRAr7MAJ0SIcDACQERcg1EdfZbcDx35AylrwCgusRx mMqDafBzOCcqFSGUvdNK4jE=
=Fkd1
-----END PGP SIGNATURE-----

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click