blob: 980b08ba3618e2a331899effeedbf8d61161c72f [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
29float getHapticScaleGamma(HapticScale scale) {
30 switch (scale) {
31 case HapticScale::VERY_LOW:
32 return 2.0f;
33 case HapticScale::LOW:
34 return 1.5f;
35 case HapticScale::HIGH:
36 return 0.5f;
37 case HapticScale::VERY_HIGH:
38 return 0.25f;
39 default:
40 return 1.0f;
41 }
42}
43
44float getHapticMaxAmplitudeRatio(HapticScale scale) {
45 switch (scale) {
46 case HapticScale::VERY_LOW:
47 return HAPTIC_SCALE_VERY_LOW_RATIO;
48 case HapticScale::LOW:
49 return HAPTIC_SCALE_LOW_RATIO;
50 case HapticScale::NONE:
51 case HapticScale::HIGH:
52 case HapticScale::VERY_HIGH:
53 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) {
60 if (scale == HapticScale::MUTE) {
61 memset(buffer, 0, length * sizeof(float));
62 return;
63 }
64 if (scale == HapticScale::NONE) {
65 return;
66 }
67 float gamma = getHapticScaleGamma(scale);
68 float maxAmplitudeRatio = getHapticMaxAmplitudeRatio(scale);
69 for (size_t i = 0; i < length; i++) {
70 float sign = buffer[i] >= 0 ? 1.0 : -1.0;
71 buffer[i] = powf(fabsf(buffer[i] / HAPTIC_MAX_AMPLITUDE_FLOAT), gamma)
72 * maxAmplitudeRatio * HAPTIC_MAX_AMPLITUDE_FLOAT * sign;
73 }
74}
75
76void clipHapticData(float* buffer, size_t length, float limit) {
77 if (isnan(limit) || limit == 0) {
78 return;
79 }
80 limit = fabsf(limit);
81 for (size_t i = 0; i < length; i++) {
82 float sign = buffer[i] >= 0 ? 1.0 : -1.0;
83 if (fabsf(buffer[i]) > limit) {
84 buffer[i] = limit * sign;
85 }
86 }
87}
88
jiabin06871bb2020-06-30 21:27:52 -070089} // namespace
90
91bool isValidHapticScale(HapticScale scale) {
92 switch (scale) {
93 case HapticScale::MUTE:
94 case HapticScale::VERY_LOW:
95 case HapticScale::LOW:
96 case HapticScale::NONE:
97 case HapticScale::HIGH:
98 case HapticScale::VERY_HIGH:
99 return true;
100 }
101 return false;
102}
103
Lais Andraded9451112021-07-02 00:15:33 +0100104void scaleHapticData(float* buffer, size_t length, HapticScale scale, float limit) {
105 if (isValidHapticScale(scale)) {
106 applyHapticScale(buffer, length, scale);
jiabin06871bb2020-06-30 21:27:52 -0700107 }
Lais Andraded9451112021-07-02 00:15:33 +0100108 clipHapticData(buffer, length, limit);
jiabin06871bb2020-06-30 21:27:52 -0700109}
110
111} // namespace android::os