Chromium Chromium - 1 year ago 88
Linux Question

How does linux play a piece of music (ALSA)

I'm interested in 'how to play a tone in linux' recently, because I want to link math and music together. I want to use system calls to do that because that way I don't have to use a music file like

etc. I've done researches on the internet, but there are only answers for things like 'how to play a music file in a program'.

I thought that there is a device file on linux like leds (
) or usbs (
). But there is no
on my computer.

So I want to know how linux play a music file, and start from there to achieve my goal.

My question is NOT "how to play music [file] in linux", but INSTEAD "how linux plays music (ALSA)". Answer on "how to play a tone in program" is also acceptable.

Answer Source

ALSA is a kernel driver that provides support for numerous sound cards. It is usually used by low-level applications that want to interact with the sound system quite directly.

ALSA offers a library API that you can use. Take a look at the documentation for some examples and help in the right direction.

With ALSA you can access a buffer, and put samples into it, that will be played by the sound device. This is done with PCM (Pulse-code modulation). With ALSA you have a lot to configure (as seen here). You want to configure the amount of channels (mono, stereo, etc.), size of the samples (8 bit, 16 bit, etc.), the rate (8000 Hz, 16000 Hz, 44100 Hz, etc.). You write those samples to the PCM device with snd_pcm_writei, for example.

The definitions of the ALSA library are located in alsa/asoundlib.h. If you're using GCC, then you can link with the ALSA library with -lasound.

Not all music players would use these low-level interactions. Much software is build on top of ALSA to give more generic interfaces to the sound system (platform independent even). Examples of sound servers include JACK and PulseAudio. The advantage of these sound servers is that they are usually easier to set-up and use, but do not give the fine control you would have with ALSA.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download