summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
authorLais Andrade <lsandrade@google.com>2022-01-18 23:55:32 +0000
committerLais Andrade <lsandrade@google.com>2022-01-24 12:15:45 +0000
commit136ca7e3fd253059943614fb35e6d123be7933e8 (patch)
treed508c81b9a331548b8f36cb4fcf3709e86c2177c /core/java/android
parentcb3718bfd48357a25301535632793951d67aaa04 (diff)
Use linear interpolator in VibratorInfo bandwidth curve
Change VibratorInfo.FrequencyProfile to use a linear interpolation for the max amplitude supported by frequency values between two measurement entries in the HAL bandwidth returned. Bug: 203785430 Test: VibratorInfoTest Change-Id: I56e998df40698be87a713ca9013380c0e938e165
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/os/VibratorInfo.java32
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. */