0

I've got a question While I'm studying in operating system.

As I learned, Operating System is a kind of resource manager and audio program works in my PC will use the speaker as a resource. So Audio program will use allocated speaker by OS.

When I executes more than two processes of audio program in PC, Sounds of them come out from speaker simultaneously.

I wonder what is the mechanism for this. Are they processes hold & release the resource when they are in running & ready state? Or the Processes share the resource by OS?

Dani Vijay
  • 2,188
  • 2
  • 22
  • 37
Ilak
  • 21
  • 1

1 Answers1

1

Multiple sounds can be mixed together additively. For software, this mostly means a small buffer of samples where you add (with saturation) the samples from 2 or more streams of digitized audio before sending the result to the speaker/s. Of course sound cards are also likely to be capable of doing this mixing themselves (with some hardware specific limit on the max. number of streams that can be handled).

For the "PC speaker" there's no support for digitized sound or much else (it only supports "one fixed frequency tone at a time"). If that's what you're asking about then you can (with a relatively high amount of overhead) use pulse-width modulation (using a pair of timers) to force it to play digitized sound and still do the mixing in software. Alternatively you can nerf the audio such that only one tone occurs at a time (e.g. if there's 2, pick the highest frequency tone or make one wait until the other is finished).

Brendan
  • 35,656
  • 2
  • 39
  • 66
  • Really thank's for your answer. Then Is the small buffer exists in sound card? Or If small buffer is in software, then does that mean processes(not threads) shares the small buffer? And who adds the audio samples in the small buffer? Does OS perform or speaker(mcu?) does? – Ilak May 17 '19 at 07:08
  • Sound card probably has a tiny buffer (especially if the streams have different sample rates and it needs to hold values from one stream for longer than values from another stream). For software, because CPU can't give sound its undivided attention (and has to deal with things like IRQs and run normal programs) the buffer has to be large enough to cover however long it takes between CPU giving it attention (e.g. if sound has a 22 KHz sample rate or 22000 samples per second, and CPU does the mixing every 100 milliseconds, then CPU would need a buffer large enough for at least 2200 samples). – Brendan May 17 '19 at 07:13
  • For software mixing; who does it/where it happens depends on the OS design. Myself (as a fan of micro-kernels), I'd be tempted to have a "sound service" running as a process that does this kind of thing and then sends streams of audio to each different sound card driver. Other possibility include getting each independent sound card driver to do it, or doing it in a library, or doing it in each application. – Brendan May 17 '19 at 07:17