Loudness Information in Audio Signals

in

However, because we want to open the buffer in shared mode, we cannot order it to use a specific sample format as this is subject to system-level configuration. Instead, we must rely on the most robust way to get the right audio format by using the default or specified device’s current settings.
To configure the device for the desired audio format, we pass the value we want to use to ioctl() and it updates it with the actual value that the device driver has set. Of course, in real code, we must detect such cases and notify the user about the format change or exit with an error.
To set the buffer length, we first get “fragment size” property for our device using ioctl(). Then we use this value to convert the buffer length into the number of fragments. Fragments are not audio frames, fragment size is not the size of a sample! We then set the number of fragments with SNDCTL_DSP_SETFRAGMENT control code. Note that we can skip this section if we don’t want to set our own buffer length and use the default buffer length instead.

To drain our ring buffer we first get the free space available in our buffer region where we write new audio samples. When this space is empty, we stop the stream with AudioDeviceStop() if it’s not already stopped. If it’s still running and there’s no more data to be written, we wait for a period of time (100ms) before continuing our loop.
In CoreAudio, draining involves waiting until our ring buffer is empty. When this happens, we stop the stream with AudioDeviceStop() and exit our main I/O loop. If there’s still data to be written when we start our loop, we wait for a period of time (100ms) before continuing.
In both cases, if the input data is less than the size of our buffer, we don’t start the stream yet and continue waiting until it’s ready. Once it’s started, we write new audio samples to our ring buffer using memcpy() function. If there’s not enough space available in our buffer region, we pass silence (data region filled with zeros) so that there are no audible surprises when this data is played.

SICORPS