Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 51f07177 authored by Phil Burk's avatar Phil Burk
Browse files

aaudio: use fmin and fmax to block Nan

This prevents NaN from creating noise that does not respond to
Volume changes.

Bug: 69224661
Test: adb shell aaudio_loopback -c2 -C2 -tm -m2 -s20 -Pl -pl
Change-Id: I8604f5af42cbf5637f7ea4e975f53db990cdf9de
parent 6f451291
Loading
Loading
Loading
Loading
+10 −7
Original line number Original line Diff line number Diff line
@@ -59,10 +59,15 @@ static inline int16_t clamp16_from_float(float f) {
    return (int16_t) roundf(fmaxf(fminf(f * scale, scale - 1.f), -scale));
    return (int16_t) roundf(fmaxf(fminf(f * scale, scale - 1.f), -scale));
}
}


// Clip to valid range of a float sample to prevent excessive volume.
// By using fmin and fmax we also protect against NaN.
static float clipToMinMaxHeadroom(float input) {
    return fmin(MAX_HEADROOM, fmax(MIN_HEADROOM, input));
}

static float clipAndClampFloatToPcm16(float sample, float scaler) {
static float clipAndClampFloatToPcm16(float sample, float scaler) {
    // Clip to valid range of a float sample to prevent excessive volume.
    // Clip to valid range of a float sample to prevent excessive volume.
    if (sample > MAX_HEADROOM) sample = MAX_HEADROOM;
    sample = clipToMinMaxHeadroom(sample);
    else if (sample < MIN_HEADROOM) sample = MIN_HEADROOM;


    // Scale and convert to a short.
    // Scale and convert to a short.
    float fval = sample * scaler;
    float fval = sample * scaler;
@@ -127,6 +132,7 @@ void AAudioConvert_pcm16ToFloat(const int16_t *source,
    }
    }
}
}



// This code assumes amplitude1 and amplitude2 are between 0.0 and 1.0
// This code assumes amplitude1 and amplitude2 are between 0.0 and 1.0
void AAudio_linearRamp(const float *source,
void AAudio_linearRamp(const float *source,
                       float *destination,
                       float *destination,
@@ -139,10 +145,8 @@ void AAudio_linearRamp(const float *source,
    for (int frameIndex = 0; frameIndex < numFrames; frameIndex++) {
    for (int frameIndex = 0; frameIndex < numFrames; frameIndex++) {
        for (int sampleIndex = 0; sampleIndex < samplesPerFrame; sampleIndex++) {
        for (int sampleIndex = 0; sampleIndex < samplesPerFrame; sampleIndex++) {
            float sample = *source++;
            float sample = *source++;

            // Clip to valid range of a float sample to prevent excessive volume.
            // Clip to valid range of a float sample to prevent excessive volume.
            if (sample > MAX_HEADROOM) sample = MAX_HEADROOM;
            sample = clipToMinMaxHeadroom(sample);
            else if (sample < MIN_HEADROOM) sample = MIN_HEADROOM;


            *destination++ = sample * scaler;
            *destination++ = sample * scaler;
        }
        }
@@ -240,8 +244,7 @@ void AAudio_linearRampMonoToStereo(const float *source,
        float sample = *source++;
        float sample = *source++;


        // Clip to valid range of a float sample to prevent excessive volume.
        // Clip to valid range of a float sample to prevent excessive volume.
        if (sample > MAX_HEADROOM) sample = MAX_HEADROOM;
        sample = clipToMinMaxHeadroom(sample);
        else if (sample < MIN_HEADROOM) sample = MIN_HEADROOM;


        const float scaler = amplitude1 + (frameIndex * delta);
        const float scaler = amplitude1 + (frameIndex * delta);
        float sampleScaled = sample * scaler;
        float sampleScaled = sample * scaler;