Fix for divide by zero in WASAPI device class


#1

Unmasking floating point exceptions reveals this bug in AudioDeviceManager::audioDeviceIOCallbackInt when switching to the WASAPI device type via the AudioDeviceSelectorComponent:

void AudioDeviceManager::audioDeviceIOCallbackInt (const float** inputChannelData,
                                                   int numInputChannels,
                                                   float** outputChannelData,
                                                   int numOutputChannels,
                                                   int numSamples)
{
    const ScopedLock sl (audioCallbackLock);

    if (inputLevelMeasurementEnabledCount > 0)
    {
        for (int j = 0; j < numSamples; ++j)
        {
            float s = 0;

            for (int i = 0; i < numInputChannels; ++i)
                s += std::abs (inputChannelData[i][j]);

            s /= numInputChannels; // divide by zero

The fix is easy, just add a test for numInputChannels > 0 and if it fails, set inputLevel to zero:

void AudioDeviceManager::audioDeviceIOCallbackInt (const float** inputChannelData,
                                                   int numInputChannels,
                                                   float** outputChannelData,
                                                   int numOutputChannels,
                                                   int numSamples)
{
    const ScopedLock sl (audioCallbackLock);

    if (inputLevelMeasurementEnabledCount > 0)
    {
        if (numInputChannels > 0)
        {
            for (int j = 0; j < numSamples; ++j)
            {
                float s = 0;

                for (int i = 0; i < numInputChannels; ++i)
                    s += std::abs (inputChannelData[i][j]);

                s /= numInputChannels;

                const double decayFactor = 0.99992;

                if (s > inputLevel)
                    inputLevel = s;
                else if (inputLevel > 0.001f)
                    inputLevel *= decayFactor;
                else
                    inputLevel = 0;
            }
        }
        else
        {
          inputLevel = 0;
        }
    }

#2

Thanks, I’ll sort that out!