andrewrk / libsoundio

C library for cross-platform real-time audio input and output
http://libsound.io/
MIT License
1.94k stars 230 forks source link

Is there a high-level overview? #166

Closed idmitrievsky closed 7 years ago

idmitrievsky commented 7 years ago

I'm new not only to this library, but also to any kind of audio programming.

Can you recommend any resources about high-level concepts of working with sound and libsoundio in particular? Because right now I'm not even sure what calls a responsible for actually making sound 😄 And I'm just overall confused by the library's design just because I don't know what problems it solves.

Thanks!

Yepoleb commented 7 years ago

The basic concept is that your application writes an audio signal to a buffer and passes that to the sound server when it's ready to accept new data. So for example you have this very basic sine signal:

uint8_t signal[] = {0, 29, 47, 47, 29, 0, -29, -47, -47, -29};

Every few milliseconds the sound server will ask you to copy a certain length of it to its internal buffer, so it can mix the signal together with all other applications and send it to the audio hardware. Check the examples if you want to know which callbacks are required for sound output.

If you want to know more about the higher level concepts of audio encoding search for PCM, bit depth and sampling rates. Also maybe look into libao, it has a much simpler interface for getting started than libsoundio.

idmitrievsky commented 7 years ago

I'll research into these terms, thanks @Yepoleb!

One more potentially dumb question: my understanding is that every number in your signal array is a volume value, so what do negative values mean? Does sound server decide how to interpret this?

Yepoleb commented 7 years ago

This screenshot of a random song I opened in Audacity should give you a better idea.

audio signal

Audio signals are basically just sound waves in digital form and these waves move back and forth. This back and forth movement is indicated by positive and negative values. If you don't like negative values, you can also define 0 as the bottom of the wave instead of the middle. This is indicated by the 'U' for unsigned in the U16LE format. For most cases you should stick to the signed formats though, as they make processing much easier. Thinking of the numbers as volume make sense, if you keep in mind that our ears can sense positive pressure changes as well as negative ones.

idmitrievsky commented 7 years ago

if you keep in mind that our ears can sense positive pressure changes as well as negative ones

very helpful analogy, thanks again! 😃