1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <cstring>
17
18 #include <math.h>
19
20 #include <vibrator/ExternalVibrationUtils.h>
21
22 namespace android::os {
23
24 namespace {
25 static constexpr float HAPTIC_SCALE_VERY_LOW_RATIO = 2.0f / 3.0f;
26 static constexpr float HAPTIC_SCALE_LOW_RATIO = 3.0f / 4.0f;
27 static constexpr float HAPTIC_MAX_AMPLITUDE_FLOAT = 1.0f;
28
getHapticScaleGamma(HapticLevel level)29 float getHapticScaleGamma(HapticLevel level) {
30 switch (level) {
31 case HapticLevel::VERY_LOW:
32 return 2.0f;
33 case HapticLevel::LOW:
34 return 1.5f;
35 case HapticLevel::HIGH:
36 return 0.5f;
37 case HapticLevel::VERY_HIGH:
38 return 0.25f;
39 default:
40 return 1.0f;
41 }
42 }
43
getHapticMaxAmplitudeRatio(HapticLevel level)44 float getHapticMaxAmplitudeRatio(HapticLevel level) {
45 switch (level) {
46 case HapticLevel::VERY_LOW:
47 return HAPTIC_SCALE_VERY_LOW_RATIO;
48 case HapticLevel::LOW:
49 return HAPTIC_SCALE_LOW_RATIO;
50 case HapticLevel::NONE:
51 case HapticLevel::HIGH:
52 case HapticLevel::VERY_HIGH:
53 return 1.0f;
54 default:
55 return 0.0f;
56 }
57 }
58
applyHapticScale(float * buffer,size_t length,HapticScale scale)59 void applyHapticScale(float* buffer, size_t length, HapticScale scale) {
60 if (scale.isScaleMute()) {
61 memset(buffer, 0, length * sizeof(float));
62 return;
63 }
64 if (scale.isScaleNone()) {
65 return;
66 }
67 HapticLevel hapticLevel = scale.getLevel();
68 float adaptiveScaleFactor = scale.getAdaptiveScaleFactor();
69 float gamma = getHapticScaleGamma(hapticLevel);
70 float maxAmplitudeRatio = getHapticMaxAmplitudeRatio(hapticLevel);
71
72 for (size_t i = 0; i < length; i++) {
73 if (hapticLevel != HapticLevel::NONE) {
74 float sign = buffer[i] >= 0 ? 1.0 : -1.0;
75 buffer[i] = powf(fabsf(buffer[i] / HAPTIC_MAX_AMPLITUDE_FLOAT), gamma)
76 * maxAmplitudeRatio * HAPTIC_MAX_AMPLITUDE_FLOAT * sign;
77 }
78
79 if (adaptiveScaleFactor != 1.0f) {
80 buffer[i] *= adaptiveScaleFactor;
81 }
82 }
83 }
84
clipHapticData(float * buffer,size_t length,float limit)85 void clipHapticData(float* buffer, size_t length, float limit) {
86 if (isnan(limit) || limit == 0) {
87 return;
88 }
89 limit = fabsf(limit);
90 for (size_t i = 0; i < length; i++) {
91 float sign = buffer[i] >= 0 ? 1.0 : -1.0;
92 if (fabsf(buffer[i]) > limit) {
93 buffer[i] = limit * sign;
94 }
95 }
96 }
97
98 } // namespace
99
isValidHapticScale(HapticScale scale)100 bool isValidHapticScale(HapticScale scale) {
101 switch (scale.getLevel()) {
102 case HapticLevel::MUTE:
103 case HapticLevel::VERY_LOW:
104 case HapticLevel::LOW:
105 case HapticLevel::NONE:
106 case HapticLevel::HIGH:
107 case HapticLevel::VERY_HIGH:
108 return true;
109 }
110 return false;
111 }
112
scaleHapticData(float * buffer,size_t length,HapticScale scale,float limit)113 void scaleHapticData(float* buffer, size_t length, HapticScale scale, float limit) {
114 if (isValidHapticScale(scale)) {
115 applyHapticScale(buffer, length, scale);
116 }
117 clipHapticData(buffer, length, limit);
118 }
119
120 } // namespace android::os
121