3 ms·
I don't have the time and these days don't really have the expertise to take you on a tour of the ALSA codebase. All audio interfaces that I'm familiar with wo
by PaulDavisThe1st 7y ago
I don't have the time and these days don't really have the expertise to take you on a tour of the ALSA codebase.
All audio interfaces that I'm familiar with work on basically the same principles (except for a couple that I think should be thrown on the junk pile for design reasons). They have a memory buffer which they treat as a ringbuffer, and periodically as they traverse the ringbuffer they raise an IRQ with the CPU. The CPU reads/writes from/to the buffer, and assuming that it does so on time, things continue on smoothly. These days most devices can function as DMA bus masters, meaning that the CPU does not need to explicitly move data to/from the device - the device is given an address in system memory and uses that.
Very few devices have a register that can be read to determine the read/write pointer position within the buffer. Therefore a design predicated on this idea (which you'd think would be a no brainer) is going to fail for the majority of devices.
So the "pcm" zone of an ALSA driver is the part that takes care of handling interrupts and data transfer. ALSA represents each direction as a separate device, a decision I was never in love with, but CoreAudio and most Windows driver APIs do the same so what can you do ...
Then there's an entirely separate aspect to many audio interfaces which is far more complex and difficult. In ALSA it is represented by the "control" API, in which ALSA tries to present as many of the controls present in the device and allow them to be manipulated. Since there is a massive variation in the number, nature and interdependence of these controls, this is very challenging to present to the user. This is partly why there are often dedicated control applications for high end cards on Windows and macOS.
There's no particular connection between the PCM and the control aspects of an ALSA driver, although obviously they are related and implemented together.
Sample clock is not visible to the CPU. Sample clock exists only at the device hardware level. Deciding which clock source to use is an option on a few pro-level cards, and ALSA "exports" this functionality via the control API.
USB audio device support is particularly problematic because the USB bus is uniquely unsuited to this purpose, and also because despite the standard, many/most devices have "quirks" that need to be accomodated in order to function fully. As a result, although there is really just a single driver per USB class, there is quite a bit of code associated with dealing with quirks of specific devices. This is decreasing for reasons I alluded to early: if you want to sell a device to iPad users, you cannot tell them to install a driver, and so manufacturers have been getting steadily more and more conformant.
- hootbootscoot 7y agoThank you, Paul, for taking the time to explain this.