10
$\begingroup$

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?

$\endgroup$
4
  • $\begingroup$ "Jittery" I can understand, but what does "laggy" sound like? (P.S., The FM tuner output from my iPod nano lags the output of an analog FM radio tuned to the same station by about a quarter of a second, but I can't hear the lag if I listen to the iPod alone.) $\endgroup$ Commented May 29, 2017 at 19:41
  • $\begingroup$ @jameslarge If you were playing a video game, something like a quarter second lag in sound would be extremely noticeable. Similarly for video conferencing. $\endgroup$ Commented May 29, 2017 at 20:48
  • 3
    $\begingroup$ As Ariel's answer points out, there is plenty of processing power in even a rather old CPU to handle this task. However, I'm pretty sure that this task has largely been and continues to be handled by audio coprocessors. The CPU's job is then to simply fill the audio coprocessors buffers which doesn't require any "continuous" processing by the CPU, especially for music where all the data is available upfront. $\endgroup$ Commented May 29, 2017 at 20:55
  • $\begingroup$ I would opt for DMAC, sound card processing units and buffers. $\endgroup$
    – Evil
    Commented May 30, 2017 at 7:03

2 Answers 2

9
$\begingroup$

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$.

$\endgroup$
9
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.