10

If processors can only execute one thing at a time, how come I can play music continuously and still be able to run other tasks?

I understand the interrupt system, but isn't it needed that CPU continuously process audio for it to not sound jittery/laggy?

I am asking about the underlying implementation, is this question related to multi-threading? How does a 1-core, 1-threaded CPU be able to achieve this multitasking?

DreamIT
  • 103
  • 4

2 Answers2

9

Since the CPU works in fixed clock cycles, nothing is really continuous, only seems so because the discretization is sensitive enough.

Suppose your CPU clock rate is $1\text{GHz}=10^9Hz$. If the CPU only devotes one in $t$ clock cycles to processing audio (and utilizes the remaining clock cycles for unrelated tasks) then you have a delay of $\approx t\cdot 10^{-9}s$ between every "audio processing tasks" preformed by the cpu (for simplicity, we assume the cpu preforms this processing using only one clock cycle).

Lets say we allow a delay of $10^{-5}s$ (humans hear frequencies in the range of 20Hz to 20Khz, so the human hearing will not be sensitive to such delay), then we need to require $t<10^4$, so the CPU can handle simultaneously $10^4$ more tasks while keeping a delay of $10^{-5}s$.

Ariel
  • 13,614
  • 1
  • 22
  • 39
9

40 years ago, you might have had a computer where the CPU controlled the speaker directly. Those times are over, long ago.

You may have a computer with a primitive sound card. Such a sound card will have a buffer for stereo audio samples, that buffer can be filled, the output function will be started, and the sound card starts generating audio from the samples in its buffers, without the CPU having to do anything. All the CPU needs to do is fill the buffers with more audio samples before it runs out. If you have a one megabyte buffer, that's 250,000 stereo samples in CD quality, that's about six seconds. So every few seconds, the CPU has to fill these buffers again.

In reality, your computer will have something much more advanced. In principle the same, but the buffers can be filled directly with sound in mp3 or aac format, for example, and the sound card will decode this data to stereo samples on its own. Most likely it can be programmed to produce all kinds of different effects, from sound volume, improving sound quality, changing pitch or speed independently, generate surround sound and so on.

The CPU doesn't do very much, just filling the sound buffers from time to time. The rest is done by something else. Of course when I say "sound card", these have shrunk from sound cards to chips to a tiny fleck of transistors on a massive chip with lots of different functionalities.

For one maker of such cards, look at https://en.wikipedia.org/wiki/Wolfson_Microelectronics as a starting point.

gnasher729
  • 32,238
  • 36
  • 56