PCM software mixer

The PCM software mixer allows multiple application streams to play to a single hardware device concurrently by mixing together the audio samples of all of the application streams. When you enable PCM software mixer reference streams in the audio configuration file (using sw_mixer_max_references), it also creates a capture device that provides the mixed playback audio stream as a reference audio stream/signal.

A PCM software mixer instance is automatically created for each hardware PCM playback device, even if the hardware supports multiple hardware subchannels. Requests from client applications to the hardware device are routed to the PCM software mixer automatically. You can disable the PCM sofware mixer using the -o disable_sw_mixer option.

Note: When the PCM software mixer is not disabled, the CPU is used more than if you use the hardware device without the mixer, even when there is only one stream.

The PCM software mixer appears as a new PCM playback device that supports many subchannels. When the PCM software mixer is started, it opens a connection to the real hardware device. If the real hardware device is already in use, the PCM software mixer can't run. Likewise, if the PCM software mixer is running, the real hardware device is in use and is unavailable.

The PCM software mixer is specifically attached to a single hardware PCM device. This one-to-one mapping allows for an API call to identify the PCM software-mixing device associated with its hardware device.

Client application fragment size

Client applications that attach to a PCM software mixer device can use a fragment size (in bytes) that is any multiple of the PCM software mixer's underlying fragment size. For example, if the PCM software mixer's fragment size is 4 KB, then the application's fragment can be a multiple of 4 KB.

You can use one of the following options to size the PCM software mixer fragments:

When you use this option, io-audio calculates a fragment size based on the requested period and the audio controller configuration. You can only specify this option in the audio configuration file. Use the following format:
sw_mixer_ms=<milliseconds per fragment>
<milliseconds per fragment> must be an integer.
Use this option to specify a fragment size using samples instead of a value in millseconds. Use the following format:
sw_mixer_samples=<samples per fragment>
Use the following formula to calculate <samples per fragment>:

Audio controller sample rate * Desired time period / Audio controller number of channels

For example, if the audio controller sample rate is 48 kHz, the time period is 16 ms, and the number of audio controller channels is two, <samples per fragment> is 768:

48000Hz * 16ms / 1000 = 768

You cannot use sw_mixer_samples if you are using the PCM input splitter.

Avoiding distortion with a predictive limiter

Tip: If you have QNX Software Development Platform 6.6 io-audio patch (patch ID 4687) or a later update installed, the following behavior applies:

The PCM software mixer operates on 16-bit PCM data. When mixing multiple audio streams, the accumulated audio data can overflow and may need to be clamped to a minimum/maximum of 16-bit values. This clamping causes distortion, which is more noticeable the more the signal has overflowed into an out-of-bounds region. The degree and frequency of the overflow increases with the number of audio streams that are concurrently mixed.

To avoid the distortion, you can enable a predictive limiter (sw_mixer_limiter) on the accumulated mixer output that uses a one millisecond delay to look ahead and observe the impending amplitude of the output audio signal. Using that information, it calculates an attenuation that reduces the signal below the PCM limit. If the output signal is below the limit, the attenuation is gradually reduced back to zero. This provides a much smoother signal response than direct clamping, and allows many audio streams to be mixed concurrently without any noticeable audible distortion.

The use of a limiter is recommended on target systems where multiple audio streams are played concurrently and where audio streams use the full 16-bit PCM range. If there's normally only one audio stream playing or the audio streams that are being mixed have low amplitude, then the limiter isn't required. It's important to note that the playback output incurs a one millisecond delay if the limiter is enabled. For more information, see the sw_mixer_limiter configuration options.