Huy's Notes
Computer Audio Under the hood

Computer Audio Under the hood

I always curious how could the computer able to play music, and how could one implemented an audio player, in a GUI app, or in a termial process like mpsyt.

Let's assume we already have an audio file (in mp3, wav, ogg,... format), and we want to build a program to read the file, play it on our computer's speakers, the process would be:

  • Decode the audio file to appropriate data
  • Create an audio buffer from that data
  • Send this audio buffer to the audio output devices

For decoding, there's a lot of decoders available so we can use, each format need a different decoder. More about this later.

To actually play these decoded data on the device, we need to communicate with the device via the OS's audio device interface. There are different interface depends on the OS you're using, for example:

  • ALSA on Linux
  • WASAPI on Windows
  • CoreAudio on macOS and iOS
  • Oboe on Android

There are some cross platform implementation to provide an easier to use API across platforms, one of which is Rust's cpal.

Referred in

If you think this note resonated, be it positive or negative, please feel free to send me an email and we can talk.