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

Commit 16d588f4 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add limit parameter to be applied to scaled audio haptic data" into sc-v2-dev

parents 559098d2 d945111b
Loading
Loading
Loading
Loading
+34 −14
Original line number Diff line number Diff line
@@ -56,6 +56,36 @@ float getHapticMaxAmplitudeRatio(HapticScale scale) {
    }
}

void applyHapticScale(float* buffer, size_t length, HapticScale scale) {
    if (scale == HapticScale::MUTE) {
        memset(buffer, 0, length * sizeof(float));
        return;
    }
    if (scale == HapticScale::NONE) {
        return;
    }
    float gamma = getHapticScaleGamma(scale);
    float maxAmplitudeRatio = getHapticMaxAmplitudeRatio(scale);
    for (size_t i = 0; i < length; i++) {
        float sign = buffer[i] >= 0 ? 1.0 : -1.0;
        buffer[i] = powf(fabsf(buffer[i] / HAPTIC_MAX_AMPLITUDE_FLOAT), gamma)
                * maxAmplitudeRatio * HAPTIC_MAX_AMPLITUDE_FLOAT * sign;
    }
}

void clipHapticData(float* buffer, size_t length, float limit) {
    if (isnan(limit) || limit == 0) {
        return;
    }
    limit = fabsf(limit);
    for (size_t i = 0; i < length; i++) {
        float sign = buffer[i] >= 0 ? 1.0 : -1.0;
        if (fabsf(buffer[i]) > limit) {
            buffer[i] = limit * sign;
        }
    }
}

} // namespace

bool isValidHapticScale(HapticScale scale) {
@@ -71,21 +101,11 @@ bool isValidHapticScale(HapticScale scale) {
    return false;
}

void scaleHapticData(float* buffer, size_t length, HapticScale scale) {
    if (!isValidHapticScale(scale) || scale == HapticScale::NONE) {
        return;
    }
    if (scale == HapticScale::MUTE) {
        memset(buffer, 0, length * sizeof(float));
        return;
    }
    float gamma = getHapticScaleGamma(scale);
    float maxAmplitudeRatio = getHapticMaxAmplitudeRatio(scale);
    for (size_t i = 0; i < length; i++) {
        float sign = buffer[i] >= 0 ? 1.0 : -1.0;
        buffer[i] = powf(fabsf(buffer[i] / HAPTIC_MAX_AMPLITUDE_FLOAT), gamma)
                * maxAmplitudeRatio * HAPTIC_MAX_AMPLITUDE_FLOAT * sign;
void scaleHapticData(float* buffer, size_t length, HapticScale scale, float limit) {
    if (isValidHapticScale(scale)) {
        applyHapticScale(buffer, length, scale);
    }
    clipHapticData(buffer, length, limit);
}

} // namespace android::os
+5 −1
Original line number Diff line number Diff line
@@ -32,7 +32,11 @@ enum class HapticScale {

bool isValidHapticScale(HapticScale scale);

void scaleHapticData(float* buffer, size_t length, HapticScale scale);
/* Scales the haptic data in given buffer using the selected HapticScale and ensuring no absolute
 * value will be larger than the absolute of given limit.
 * The limit will be ignored if it is NaN or zero.
 */
void scaleHapticData(float* buffer, size_t length, HapticScale scale, float limit);

} // namespace android::os