Ethan Winer said:
Benny nailed it, and I'll clarify just one point: DMA and buffers work together, both with sound cards and hard drives. As data arrives from either the sound card or hard drive it is placed into the buffer automatically via DMA, bypassing the CPU which may be off doing other things like handling an EQ plug-in or reformatting a Word document. Then, when the program has a moment and "comes up for air," it can read that data from the buffer. The key is that the initial transfer of data from sound card to buffer happens in the background, since it's critical that no audio be lost as it arrives.
That's very close, but subtly off.
As you said, when data is being transferred via DMA, the hardware is doing the moving of data from a device into RAM for you. However, when it finishes, the DMA engine signals the CPU that the data is ready and immediately interrupts whatever is happening on the system.
The sample buffer being scribbled into by the DMA engine is a ring buffer of a certain size. Basically, the DMA hardware starts writing at the beginning, and when it gets to the end, it wraps back to the beginning of the buffer. The CPU then copies data from that buffer to the application (by various mechanisms, depending on the OS). Essentially, the CPU and the DMA engine are constantly chasing each other.
If the CPU doesn't get the audio data from that buffer to the app in time, the DMA engine will wrap around and begin overwriting data that hasn't been read yet by the CPU. Thus, it is still possible to lose audio data.
There may be multiple layers of software buffers between the sample buffer (the dumping ground for the DMA engine's output) and the app, all of which are of a fixed size, hence the reason OS audio architectures are such a pain in the backside to get right.
The advantage to DMA is that the actual effort of reading data from the device is avoided, thus saving CPU cycles for other things.