diff options
| author | Lais Andrade <lsandrade@google.com> | 2022-01-24 20:09:04 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2022-01-24 20:09:04 +0000 |
| commit | b5ac864874f57ad2c2d8b888f60f8d8d586e28bc (patch) | |
| tree | cea208adbd663e9f04ef133b112fb789a3ea25d4 /core/java | |
| parent | 0caffbfc38a6e8002bb5856f00ceb8e81a4b3de6 (diff) | |
| parent | 136ca7e3fd253059943614fb35e6d123be7933e8 (diff) | |
Merge "Use linear interpolator in VibratorInfo bandwidth curve"
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/android/os/VibratorInfo.java | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/core/java/android/os/VibratorInfo.java b/core/java/android/os/VibratorInfo.java index d162c74aeea2..00ce14fdfd28 100644 --- a/core/java/android/os/VibratorInfo.java +++ b/core/java/android/os/VibratorInfo.java @@ -573,21 +573,29 @@ public class VibratorInfo implements Parcelable { * supported frequency range is empty. */ public float getMaxAmplitude(float frequencyHz) { - if (isEmpty() || Float.isNaN(frequencyHz)) { + if (isEmpty() || Float.isNaN(frequencyHz) || !mFrequencyRangeHz.contains(frequencyHz)) { // Unsupported frequency requested, vibrator cannot play at this frequency. return 0; } - float position = (frequencyHz - mMinFrequencyHz) / mFrequencyResolutionHz; - int floorIndex = (int) Math.floor(position); - int ceilIndex = (int) Math.ceil(position); - if (floorIndex < 0 || floorIndex >= mMaxAmplitudes.length) { - return 0; - } - if (floorIndex != ceilIndex && ceilIndex < mMaxAmplitudes.length) { - // Value in between two mapped frequency values, use the lowest supported one. - return MathUtils.min(mMaxAmplitudes[floorIndex], mMaxAmplitudes[ceilIndex]); - } - return mMaxAmplitudes[floorIndex]; + + // Subtract minFrequencyHz to simplify offset calculations. + float mappingFreq = frequencyHz - mMinFrequencyHz; + + // Find the bucket to interpolate within. + // Any calculated index should be safe, except exactly equal to max amplitude can be + // one step too high, so constrain it to guarantee safety. + int startIdx = MathUtils.constrain( + /* amount= */ (int) Math.floor(mappingFreq / mFrequencyResolutionHz), + /* low= */ 0, /* high= */ mMaxAmplitudes.length - 1); + int nextIdx = MathUtils.constrain( + /* amount= */ startIdx + 1, + /* low= */ 0, /* high= */ mMaxAmplitudes.length - 1); + + // Linearly interpolate the amplitudes based on the frequency range of the bucket. + return MathUtils.constrainedMap( + mMaxAmplitudes[startIdx], mMaxAmplitudes[nextIdx], + startIdx * mFrequencyResolutionHz, nextIdx * mFrequencyResolutionHz, + mappingFreq); } /** Returns the raw list of maximum relative output accelerations from the vibrator. */ |
