Quantcast
Username/Email:  Password: 

Introduction to Sound Programming with ALSA

Make maximum use of all the functionality in the new 2.6 kernel sound architecture using a simple API.

ALSA stands for the Advanced Linux Sound Architecture. It consists of a set of kernel drivers, an application programming interface (API) library and utility programs for supporting sound under Linux. In this article, I present a brief overview of the ALSA Project and its software components. The focus is on programming the PCM interfaces of ALSA, including programming examples with which you can experiment.

You may want to explore ALSA simply because it is new, but it is not the only sound API available. ALSA is a good choice if you are performing low-level audio functions for maximum control and performance or want to make use of special features not supported by other sound APIs. If you already have written an audio application, you may want to add native support for the ALSA sound drivers. If your primary interest isn't audio and you simply want to play sound files, using one of the higher-level sound toolkits, such as SDL, OpenAL or those provided in desktop environments, may be a better choice. By using ALSA you are restricted to using systems running a Linux kernel with ALSA support.

History of ALSA

The ALSA Project was started because the sound drivers in the Linux kernel (OSS/Free drivers) were not being maintained actively and were lagging behind the capabilities of new sound technology. Jaroslav Kysela, who previously had written a sound card driver, started the project. Over time, more developers joined, support for many sound cards was added and the structure of the API was refined.

During development of the 2.5 series of Linux kernel, ALSA was merged into the official kernel source. With the release of the 2.6 kernel, ALSA will be part of the stable Linux kernel and should be in wide use.

Digital Audio Basics

Sound, consisting of waves of varying air pressure, is converted to its electrical form by a transducer, such as a microphone. An analog-to-digital converter (ADC) converts the analog voltages into discrete values, called samples, at regular intervals in time, known as the sampling rate. By sending the samples to a digital-to-analog converter and an output transducer, such as a loudspeaker, the original sound can be reproduced.

The size of the samples, expressed in bits, is one factor that determines how accurately the sound is represented in digital form. The other major factor affecting sound quality is the sampling rate. The Nyquist Theorem states that the highest frequency that can be represented accurately is at most one-half the sampling rate.

ALSA Basics

ALSA consists of a series of kernel device drivers for many different sound cards, and it also provides an API library, libasound. Application developers are encouraged to program using the library API and not the kernel interface. The library provides a higher-level and more developer-friendly programming interface along with a logical naming of devices so that developers do not need to be aware of low-level details such as device files.

In contrast, OSS/Free drivers are programmed at the kernel system call level and require the developer to specify device filenames and perform many functions using ioctl calls. For backward compatibility, ALSA provides kernel modules that emulate the OSS/Free sound drivers, so most existing sound applications continue to run unchanged. An emulation wrapper library, libaoss, is available to emulate the OSS/Free API without kernel modules.

ALSA has a capability called plugins that allows extension to new devices, including virtual devices implemented entirely in software. ALSA provides a number of command-line utilities, including a mixer, sound file player and tools for controlling special features of specific sound cards.

ALSA Architecture

The ALSA API can be broken down into the major interfaces it supports:

  • Control interface: a general-purpose facility for managing registers of sound cards and querying the available devices.

  • PCM interface: the interface for managing digital audio capture and playback. The rest of this article focuses on this interface, as it is the one most commonly used for digital audio applications.

  • Raw MIDI interface: supports MIDI (Musical Instrument Digital Interface), a standard for electronic musical instruments. This API provides access to a MIDI bus on a sound card. The raw interface works directly with the MIDI events, and the programmer is responsible for managing the protocol and timing.

  • Timer interface: provides access to timing hardware on sound cards used for synchronizing sound events.

  • Sequencer interface: a higher-level interface for MIDI programming and sound synthesis than the raw MIDI interface. It handles much of the MIDI protocol and timing.

  • Mixer interface: controls the devices on sound cards that route signals and control volume levels. It is built on top of the control interface.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

question about one command of the program

manu's picture

hello,

I run your 4 programs coding in C with linux but i don't understand how does it work the next command :

snd_pcm_hw_params_get_period_time(params, &Val, &dir) who is used like that in Example 3 for example:

/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params,
&val, &dir);
/* 5 seconds in microseconds divided by
* period time */
loops = 5000000 / val;

If I add some lines of code, I can read the value of the variable "val" after and before the function " snd_pcm_hw_params_get_period_time(params, &val, &dir); ".

I can read that "val" before this function as a value 44099 (sample frequency minus 1), and after, "val" has the value 21333 at each time, whatever the others values enter as a parameters.

I don't understand how this function works and i would to know.

Would someone help me?

Thanks

Manu

Appreciation of the article

Anonymous's picture

This is the way articles should be written, it is an introductory note, tutorial, call it whatever, simple terse and clear.

I am sure that there will be some people who will like ALSA because of this article, like me.

5 sec

MK's picture

/* 5 seconds in microseconds divided by
* period time */
loops = 5000000 / val;

How did he calculate this?

how to read & play a sound file

Shubham's picture

hi,

I am new to ALSA programing. Could anyone tell how i can read & play a sound file using above example code for playback?

Converting to ALSA

Anonymous's picture

Firstly, this article is really good.

I need to write an application for FXS interface. the drivers are implements using ioctl system calls. How difficult is it to convert to ALSA API.
Or the otherway, what is to be done if i need to access these drivers in an application which is already supporting ALSA.

ok I just found it. for code

Elie's picture

ok I just found it. for code called 'alsa.c' it's

gcc alsa.c -lasound

Linking Libraries

Elie's picture

Umm... what libraries do i need to link? I tried the code (listings 3 & 4) and it compiled but didn't link. What gcc parameters need to be added?

(currently using Ubuntu Feisty 7.04)

undocumented bs wtf!

mark manning's picture

your second piece of example code has the following funciton call..

rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);

looking at the API reference for this call we see that the last parameter in this call is int mode. Mode may be one of the following values.

SND_PCM_NONBLOCK or SND_PCM_ASYNC. The first of these is equal to 0x01 and the second is equal to 0x02. WHAT is this undocumented mode zero that your example code AND the alsa example code uses.

undocumented == BAD MOJO!

great article tho :)

i am working on a project

Anonymous's picture

i am working on a project that needs a simple voice recording to be saved to a file, before further processing can be done on it... i am new to alsa (sound programming in general).
i believe alsa and oss can accomplish what i want but i need further guidance.
thanks.

ALSA Duplex Working code

Santoshkumar's picture

Does any body tested ALSA duplex (Record and playback)? Can you please point the place where i can get some reference source code?

Thank you,
Santosh
santosh.pattar@gmail.com

Updated code for FC6?

William Estrada's picture

I read this article and tried to run the sample code. It did compile and run but the record program produces garbage/noise. Is there an update for a Fedora Core 6 kernel? What kind of sound file does this code produce, wav, au, etc?

I don't know if anyone is

Anonymous's picture

I don't know if anyone is reading this.

But there are a few problems.

It records as far as I can see, but I get a lot of random garbage data which I don't want when recording. Anything I record is messed up in random data.

I´ve tried Sound

David's picture

I´ve tried Sound Programming with ALSA in a seminar, but i´m afraid, it is just too difficult for me. But i have to point out that i´m progging since one year, so i think i need just a bit more time. patience is all :-)

in listing 2, dir should be initialized

Fabrice Pardo's picture

Very useful article.

In listing 2, you should replace
int dir;
by
int dir = 0;
or replace &dir by NULL later

In my first test case, dir was randomly initialized to a negative
value, giving as a result
rate = 44099 bps

Actually, the snd_pcm_hw_params_set_rate_near function is poorly documented.

Display format

MikeW's picture

It would be nice if the (fixed) page width were a little greater, without having to shrink the browser text size.

Hard to read code that's full of line wraps.

Problem in opening default device in listing2 of this article

Nagaraja S's picture

Hi guys am new to this ALSA, I downloaded all driver, library utils from www.alsa-project.com and i installed.

when i run the listing1 of this doc it went fine and when i tried to run the second listing it says like this.
Unable to open the default device No such file.And i am using slackware with linux 2.4.22
Please help me out guys.

Thanx in advance for help

Re: Introduction to Sound Programming with ALSA

JKCunningham's picture

Great article. I tried the first source and it worked fine, but the second bombed:

> ./listing_2
unable to set hw parameters: Invalid argument

Any idea why this would be? I didn't modify the source.

-Jeff

Problem in listing2 program Unable to open default device

Nagaraja's picture

Hi guys am new to this ALSA, I downloaded all driver, library utils from www.alsa-project.com and i installed.

when i run the listing1 of this doc it went fine and when i tried to run the second listing it says like this.
Unable to open the default device No such file.And i am using slackware with linux 2.4.22
Please help me out guys.

Thanx in advance for help

Re: Introduction to Sound Programming with ALSA

Pablo's picture

It happened the same to me. When I change the sampling frequency value to 48 kHz then I got the error message:
unable to set hw parameters: Invalid argument
For any other sampling frequency it works ok.

Pablo

It happened the same to me. W

Pablo's picture

It happened the same to me. When I change the sampling frequency value to 48 kHz then I got the error message:
unable to set hw parameters: Invalid argument
For any other sampling frequency it works ok.

Pablo

Re: Introduction to Sound Programming with ALSA

Anonymous's picture

Someone has posted a note at the Resources page
,
saying that the variable "dir" needs to be initialized to 0 (zero).
That seemed to solve my problems, at least.

Re: Introduction to Sound Programming with ALSA

Anonymous's picture

I am having the same problems with listing 4. (And it seems that others have too, see the Resources page.) In my case the problem seems to be the sampling frequency, if I change 44100 til 88200 the program will run.

What I really would like to know is whether this is a problem with the example program from the article (I did not modify it), a problem with ALSA (I use Debian Sarge with 2.6-kernel) or a problem with my sound card (SoundBlaster Live).

Re: Introduction to Sound Programming with ALSA

Anonymous's picture

A simple question: Why can't the ALSA developers make sure that ALSA provides sound support at least as good as OSS? After having tested it on a number of boards, it was always a pain in the neck to configure properly, and getting it to work, an adventure. I will stay with OSS until ALSA becomes much more user-friendly.

Re: Introduction to Sound Programming with ALSA

Anonymous's picture

Excellent informatiom. Now lets have a simple real time mixer with chnangeable effect to really show what ALSA + 2.6 Linux can do

Re: Introduction to Sound Programming with ALSA

Anonymous's picture

Yes, I agree, excellent article and a brilliant introduction to ALSA.

Keep up the good work!

Re: Introduction to Sound Programming with ALSA

Anonymous's picture

Informative article. More intros to popular or obscure libraries please!

Post new comment

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.
  • Use to create page breaks.

More information about formatting options