I've started using miniaudio sound library written in C, which is an impressive single-header roughly 100k lines of code library for playing audio and reading microphone in a cross_platform way. I'm wondering, what it seems to do is launch a worker thread, and loop, and calls your callback (I'm not exactly sure what it does), but this means that a single thread is using all of the CPU core on which it runs, because that's what endless loops do, it doesn't sleep or anything like that. Smooth playback/reading is said to be around 44khz, whereas looping is running at max speed of the CPU. How is sampling supposed to be done generally speaking? How is it done in for example a game engine? What's the proper way to do this?

  • \$\begingroup\$ Please add more details. what do you understand by the "sound sampling" specifically? Also, what is your gamedev endgoal with it? \$\endgroup\$
    – Kromster
    Mar 28 at 6:56
  • \$\begingroup\$ @Kromster Well I'm wondering in a typical game engine, the sound obviously runs on another thread, but does it just loop sampling whenever it's appropriate from the sound source audio? It seems to me doing this is devoting an entire thread at 100% CPU utilisation even though you may not have to sample that often. What I want to know is how this is supposed to be done. \$\endgroup\$
    – Zebrafish
    Mar 28 at 8:08
  • 1
    \$\begingroup\$ I'd be surprised if the call to sample doesn't block / yield time on that hyperthread until a hardware signal that new data is available. Just because code is running doesn't mean the CPU can't preempt it and switch to another thread for a bit. But we can't investigate that without seeing the worker thread loop in question and how it's kicked off. \$\endgroup\$
    – DMGregory
    Mar 28 at 11:34


You must log in to answer this question.

Browse other questions tagged .