blob: 761ac1bf3bbfa006450286886b3e8bcbb41806e6 [file] [log] [blame]
jiabin06871bb2020-06-30 21:27:52 -07001/*
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
22namespace android::os {
23
24namespace {
25static constexpr float HAPTIC_SCALE_VERY_LOW_RATIO = 2.0f / 3.0f;
26static constexpr float HAPTIC_SCALE_LOW_RATIO = 3.0f / 4.0f;
27static constexpr float HAPTIC_MAX_AMPLITUDE_FLOAT = 1.0f;
28
Ahmad Khalil2cd80352024-02-05 12:15:28 +000029float getHapticScaleGamma(HapticLevel level) {
30 switch (level) {
31 case HapticLevel::VERY_LOW:
jiabin06871bb2020-06-30 21:27:52 -070032 return 2.0f;
Ahmad Khalil2cd80352024-02-05 12:15:28 +000033 case HapticLevel::LOW:
jiabin06871bb2020-06-30 21:27:52 -070034 return 1.5f;
Ahmad Khalil2cd80352024-02-05 12:15:28 +000035 case HapticLevel::HIGH:
jiabin06871bb2020-06-30 21:27:52 -070036 return 0.5f;
Ahmad Khalil2cd80352024-02-05 12:15:28 +000037 case HapticLevel::VERY_HIGH:
jiabin06871bb2020-06-30 21:27:52 -070038 return 0.25f;
39 default:
40 return 1.0f;
41 }
42}
43
Ahmad Khalil2cd80352024-02-05 12:15:28 +000044float getHapticMaxAmplitudeRatio(HapticLevel level) {
45 switch (level) {
46 case HapticLevel::VERY_LOW:
jiabin06871bb2020-06-30 21:27:52 -070047 return HAPTIC_SCALE_VERY_LOW_RATIO;
Ahmad Khalil2cd80352024-02-05 12:15:28 +000048 case HapticLevel::LOW:
jiabin06871bb2020-06-30 21:27:52 -070049 return HAPTIC_SCALE_LOW_RATIO;
Ahmad Khalil2cd80352024-02-05 12:15:28 +000050 case HapticLevel::NONE:
51 case HapticLevel::HIGH:
52 case HapticLevel::VERY_HIGH:
jiabin06871bb2020-06-30 21:27:52 -070053 return 1.0f;
54 default:
55 return 0.0f;
56 }
57}
58
Lais Andraded9451112021-07-02 00:15:33 +010059void applyHapticScale(float* buffer, size_t length, HapticScale scale) {
Ahmad Khalil2cd80352024-02-05 12:15:28 +000060 if (scale.isScaleMute()) {
Lais Andraded9451112021-07-02 00:15:33 +010061 memset(buffer, 0, length * sizeof(float));
62 return;
63 }
Ahmad Khalil2cd80352024-02-05 12:15:28 +000064 if (scale.isScaleNone()) {
Lais Andraded9451112021-07-02 00:15:33 +010065 return;
66 }
Ahmad Khalil2cd80352024-02-05 12:15:28 +000067 HapticLevel hapticLevel = scale.getLevel();
68 float adaptiveScaleFactor = scale.getAdaptiveScaleFactor();
69 float gamma = getHapticScaleGamma(hapticLevel);
70 float maxAmplitudeRatio = getHapticMaxAmplitudeRatio(hapticLevel);
71
Lais Andraded9451112021-07-02 00:15:33 +010072 for (size_t i = 0; i < length; i++) {
Ahmad Khalil2cd80352024-02-05 12:15:28 +000073 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 }
Lais Andraded9451112021-07-02 00:15:33 +010082 }
83}
84
85void 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
jiabin06871bb2020-06-30 21:27:52 -070098} // namespace
99
100bool isValidHapticScale(HapticScale scale) {
Ahmad Khalil2cd80352024-02-05 12:15:28 +0000101 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:
jiabin06871bb2020-06-30 21:27:52 -0700108 return true;
109 }
110 return false;
111}
112
Lais Andraded9451112021-07-02 00:15:33 +0100113void scaleHapticData(float* buffer, size_t length, HapticScale scale, float limit) {
114 if (isValidHapticScale(scale)) {
115 applyHapticScale(buffer, length, scale);
jiabin06871bb2020-06-30 21:27:52 -0700116 }
Lais Andraded9451112021-07-02 00:15:33 +0100117 clipHapticData(buffer, length, limit);
jiabin06871bb2020-06-30 21:27:52 -0700118}
119
120} // namespace android::os