Skip to content

Instantly share code, notes, and snippets.

@mohan43u
Created August 14, 2021 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohan43u/90c18c71a395346d1371dee2bb826ccb to your computer and use it in GitHub Desktop.
Save mohan43u/90c18c71a395346d1371dee2bb826ccb to your computer and use it in GitHub Desktop.
2021-08-14 16:04:55 mohan43u the third talk is about ALSA
2021-08-14 16:05:07 mohan43u im the speaker of this tal
2021-08-14 16:05:14 mohan43u ^s/tal/talk/g
2021-08-14 16:05:31 mohan43u ALSA - Advanced Linux Sound Architecture
2021-08-14 16:06:14 mohan43u in this talk, Im going to explain about few commands
2021-08-14 16:06:35 mohan43u you can follow what I execute through https://meet.ilugc.in
2021-08-14 16:06:52 mohan43u this website is also relayed through jitsi
2021-08-14 16:07:20 mohan43u I hope everyone joined through either meet.ilugc.in or jitsi will not have problem following my commands
2021-08-14 16:07:39 mohan43u so lets start talk about alsa
2021-08-14 16:08:02 mohan43u ALSA is the subsystem in linux kernel responsible for audio
2021-08-14 16:09:00 mohan43u the device driver people basically need to write audio driver which uses ALSA in order to make their audio device work in linux
2021-08-14 16:09:38 mohan43u ALSA have all the infra to write audio drivers easily, nowadays, most of the audio devices are USB based
2021-08-14 16:10:22 mohan43u so kernel have simple standard audio to utilize most of the audio devices in the market
2021-08-14 16:10:39 mohan43u wen you plug a audio usb device, it is first detected by udev
2021-08-14 16:11:15 mohan43u then that device will be classified as audio device and respoective drivers will be loaded
2021-08-14 16:12:12 mohan43u-we once the correct drivers got loaded, you will see the audio device like /dev/snd/pcmC0D0p
2021-08-14 16:13:04 mohan43u-we you can see the commands which Im executin in the presenter terminal
2021-08-14 16:13:22 mohan43u-we I just connected to my laptop and listed all the audio devices
2021-08-14 16:13:51 mohan43u-we in my laptop, there are 5 audio devices
2021-08-14 16:14:16 <-- test68 (~test@42.110.136.190) has quit (Quit: Connection closed)
2021-08-14 16:14:30 mohan43u-we the command which I executed shows all the playback devices
2021-08-14 16:14:53 mohan43u-we if you look carefully, you can see that all the playback devices ends with 'p'
2021-08-14 16:15:15 mohan43u-we p means playback
2021-08-14 16:15:36 mohan43u-we as you see, I just listed the capture device
2021-08-14 16:15:43 mohan43u-we c means capture
2021-08-14 16:15:52 --> Guest (~Guest@42.110.136.190) has joined #ilugc
2021-08-14 16:16:48 mohan43u-we now I listed another device called control device
2021-08-14 16:17:19 mohan43u-we so when you plug your usb headset, that usb headset will appear as a audio sound card
2021-08-14 16:17:45 mohan43u-we you can see all the cards in your linux using /proc/asound/cards
2021-08-14 16:18:07 mohan43u-we in my current laptop, I only have the built in sound card
2021-08-14 16:18:14 mohan43u-we so it appears a single ards
2021-08-14 16:18:23 mohan43u-we ^s/ards/card/g
2021-08-14 16:18:50 mohan43u-we this single cards will create those three type of virtual devices in linux
2021-08-14 16:19:03 mohan43u-we namely playback, capture and control
2021-08-14 16:19:48 mohan43u-we see, these are the virtual devices my builtin sound card created
2021-08-14 16:20:24 mohan43u-we any audio device plugged in into linux will atlease have one playback and one control
2021-08-14 16:20:41 mohan43u-we capture depends on the type of audio device you plug
2021-08-14 16:20:52 mohan43u-we any questions till now?
2021-08-14 16:21:21 mohan43u-we ok, im continuing
2021-08-14 16:21:52 mohan43u-we this is the raw devices, in order to use this devices, you need to open this devices and use ioctls to talk to them
2021-08-14 16:22:12 mohan43u-we this is where the libasound comes in
2021-08-14 16:22:46 mohan43u-we libasound is a library which talk to these devices properly
2021-08-14 16:23:05 mohan43u-we if you want to use these devices, libasound is the easiest way to talk to them
2021-08-14 16:23:25 mohan43u-we based on libasound, there are alsa utilities which help us to configure these devices
2021-08-14 16:24:01 mohan43u-we some of the utilities are aplay, amixer
2021-08-14 16:24:25 mohan43u-we as you see in the presenter terminal, aplay can also list the audio devices
2021-08-14 16:24:45 mohan43u-we if you look at the output, you can see that there is only one card (card0)
2021-08-14 16:25:00 mohan43u-we but there are four sub devices
2021-08-14 16:25:19 mohan43u-we each subdevices send the output through different ways
2021-08-14 16:25:31 mohan43u-we the first sub device (device 0) is for the headset
2021-08-14 16:25:36 mohan43u-we second is for hdmi 0
2021-08-14 16:25:47 mohan43u-we third is for hdmi 1 and fourth is for hdmi 2
2021-08-14 16:26:22 mohan43u-we so, if I want to play an audio through 3.5mm audio jack, them I have to send the audio data to card 0, device 0
2021-08-14 16:26:43 mohan43u-we because that is where the 3.5mm jack is available
2021-08-14 16:27:00 mohan43u-we in other words, I have to open /dev/snd/pcmC0D0p
2021-08-14 16:27:19 mohan43u-we card0, device0 in the aplay -l output corresponds to pcmC0D0p
2021-08-14 16:27:42 mohan43u-we you can say which card and which device from the pcm name itself
2021-08-14 16:27:52 mohan43u-we pcm<C0><D0>p
2021-08-14 16:28:00 mohan43u-we the first pcm is literal pcm
2021-08-14 16:28:06 mohan43u-we the C0 means card 0
2021-08-14 16:28:14 mohan43u-we and D0 means device 0
2021-08-14 16:28:21 mohan43u-we and p means its a playback device
2021-08-14 16:28:51 mohan43u-we any questions? are you guys able to understand how ALSA naming works?
2021-08-14 16:29:11 nihaal what is pcm?
2021-08-14 16:29:34 mohan43u-we pcm is the format of the data which device is able to understand
2021-08-14 16:30:02 mohan43u-we Im not sure, but it expands to something like pulse code modulation or something. i may be wrong
2021-08-14 16:30:18 nihaal ok, got it
2021-08-14 16:30:48 mohan43u-we I hope everyone understand how alsa names its devices
2021-08-14 16:31:08 <-- sandeepk (~sandeepk@223.130.30.29) has quit (Ping timeout: 256 seconds)
2021-08-14 16:31:23 mohan43u-we now, the another command called arecord
2021-08-14 16:31:35 mohan43u-we arecord -l will show the capture devices available
2021-08-14 16:31:59 mohan43u-we in my machine, I have 3.5mm jack as capture
2021-08-14 16:32:09 mohan43u-we that is what is is showing now in the presenter terminal
2021-08-14 16:32:26 mohan43u-we card 0, device 0, but instead of playback this is capture
2021-08-14 16:32:38 mohan43u-we so this device corresponds to /dev/snd/pcmC0Dpc
2021-08-14 16:33:15 mohan43u-we so, the 3.5mm analog audio jack works as playback device, as well as works as capture device
2021-08-14 16:33:58 mohan43u-we but alsa seperates its functionality into two different virtual devices and expose it to us in /dev/snd
2021-08-14 16:34:21 mohan43u-we going next
2021-08-14 16:34:37 mohan43u-we to now more details about these pcm devices, we need to use the proc filesystem
2021-08-14 16:35:38 mohan43u-we see, I just go to /proc/asound, go to card0, then go to pcm0p and printed its property
2021-08-14 16:35:52 mohan43u-we as you can see, this is CX20590 Analog
2021-08-14 16:36:47 mohan43u-we in order to talk to this device, we have to follow the hardware parameters provided in this hw_params file
2021-08-14 16:37:03 mohan43u-we this fill provides what kind of data this device accepts
2021-08-14 16:37:24 mohan43u-we as you can see we have to give data to this device through mmap (MMAP_INTERLEAVED)
2021-08-14 16:37:47 mohan43u-we and the exact digital audio format it uses is called S32_LE
2021-08-14 16:37:59 mohan43u-we this is the very important parameter
2021-08-14 16:38:28 mohan43u-we this parameter says, it uses 32 bit for audio sample
2021-08-14 16:39:26 mohan43u-we so that means, there are 2^32 values for a single sample
2021-08-14 16:39:42 mohan43u-we those values can be in +ve or in -ve
2021-08-14 16:39:57 mohan43u-we then the next important hardware parameter
2021-08-14 16:40:00 mohan43u-we the channels
2021-08-14 16:40:12 mohan43u-we here there are two channels for this devices
2021-08-14 16:40:35 mohan43u-we if we see two channels, that means, this device can do stereo playback
2021-08-14 16:40:57 mohan43u-we stereo (left, right) playback requires two channels
2021-08-14 16:41:13 mohan43u-we there are devices which provides 5 channel or 7 channels
2021-08-14 16:41:32 mohan43u-we those are DTS specifications
2021-08-14 16:41:42 mohan43u-we the next important parameter is rate
2021-08-14 16:41:48 mohan43u-we very important parameter
2021-08-14 16:41:52 mohan43u-we 48000
2021-08-14 16:42:15 mohan43u-we that means, this devices supports upto 48khz audio samplerate
2021-08-14 16:42:59 mohan43u-we we usually use 44100Hz, or 44.1khz audio in our video/audio files
2021-08-14 16:43:21 mohan43u-we but this device can support 48KHz
2021-08-14 16:43:47 mohan43u-we so these three parameters is very important, format, channels and samplerate
2021-08-14 16:44:23 mohan43u-we based on these parameters, we have to send the data to this devices
2021-08-14 16:44:49 mohan43u-we so when we write an audio application in linux, first we need to open the correct playback device
2021-08-14 16:45:01 mohan43u-we read these important hardware parameters
2021-08-14 16:45:19 mohan43u-we then send the audio data correctly to this device, so that it can play that data.
2021-08-14 16:45:40 mohan43u-we one important thing about ALSA devices
2021-08-14 16:46:35 mohan43u-we is when one application open the alsa device (let say /dev/snd/pcmC0D0p), than no other application will be able to open simulteniously
2021-08-14 16:47:02 mohan43u-we that means, in ALSA, only one application can open the alsa device.
2021-08-14 16:47:29 mohan43u-we but you can ask me how come in our linux, we are able to use two audio applications simulteniously?
2021-08-14 16:47:45 mohan43u-we this is where mixers comes into place
2021-08-14 16:47:54 mohan43u-we in other words , pulseaudio comes into place
2021-08-14 16:48:11 mohan43u-we pulseaudio is a daemon which sits in between alsa and our audio application
2021-08-14 16:48:33 mohan43u-we when the audio application wants to send data to and audio device, instead of opening the alsa device directly,
2021-08-14 16:48:48 mohan43u-we it communicates to pulseaudio and asks pulseaudio to open the device
2021-08-14 16:49:12 mohan43u-we so pulseaudio happily open that device and whatever the audio application gives, it sends to alsa
2021-08-14 16:49:30 mohan43u-we in the mean time, another audio application also want to send data to the audio device
2021-08-14 16:50:03 mohan43u-we that application also ask pulse instead of directly opening the alsa device, becuase that application know that the second time if it opens, then it will fail
2021-08-14 16:50:14 mohan43u-we so it asks the pulseaudio to also send its data
2021-08-14 16:50:41 mohan43u-we now pulseaudio get data from both audio applications, then it mixes those audio data into a single stream
2021-08-14 16:50:47 mohan43u-we then send that data to alsa
2021-08-14 16:51:02 mohan43u-we so this is how audio works in linux
2021-08-14 16:51:14 mohan43u-we pulseaudio plays very important role here
2021-08-14 16:51:38 mohan43u-we also pulseaudio manages all the audio devices through alsa
2021-08-14 16:52:08 mohan43u-we there are lot of things to talk about the sound system in linux
2021-08-14 16:52:29 mohan43u-we and ther are latest applications came up like pipewire to replace pulseaudio
2021-08-14 16:52:41 <-- dhanesh (~Dhanesh@117.223.140.227) has quit (Quit: Leaving)
2021-08-14 16:52:48 mohan43u-we if you use archlinux, you can use pipewire instead of pulseaudio
2021-08-14 16:53:06 mohan43u-we im finishing my talk here, any questions?
2021-08-14 16:54:38 @mbuf mohan43u-we, very informative, thanks!
2021-08-14 16:55:00 mohan43u-we I hope from this talk, all of you are able to understand where alsa sits, where pulseaudio sits and how audio applications communicate to the audio device.
2021-08-14 16:55:28 mohan43u-we ALSA is a beast when it comes to audio, you can do wonders if you know how to play with it.
2021-08-14 16:56:17 mohan43u-we unfortunately, because it is not very user friendly, people hesitated to use it directly, people wanted a simple way to do audio instead of doing the ALSA way.
2021-08-14 16:56:47 mohan43u-we so linux community created layers and layers on top of ALSA to simplify the audio in linux.
2021-08-14 16:57:27 mohan43u-we anyway, because of the layers, now we have easy going with audio
2021-08-14 16:57:57 mohan43u-we thanks to pulseaudio for simplifying ALSA and taking care of mixing
2021-08-14 16:58:37 mohan43u-we otherwise, new linux users would have left to deal with the beast called ALSA
2021-08-14 16:59:14 mohan43u-we im finishing my talk.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment