jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 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 | |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 29 | float getHapticScaleGamma(HapticLevel level) { |
| 30 | switch (level) { |
| 31 | case HapticLevel::VERY_LOW: |
jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 32 | return 2.0f; |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 33 | case HapticLevel::LOW: |
jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 34 | return 1.5f; |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 35 | case HapticLevel::HIGH: |
jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 36 | return 0.5f; |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 37 | case HapticLevel::VERY_HIGH: |
jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 38 | return 0.25f; |
| 39 | default: |
| 40 | return 1.0f; |
| 41 | } |
| 42 | } |
| 43 | |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 44 | float getHapticMaxAmplitudeRatio(HapticLevel level) { |
| 45 | switch (level) { |
| 46 | case HapticLevel::VERY_LOW: |
jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 47 | return HAPTIC_SCALE_VERY_LOW_RATIO; |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 48 | case HapticLevel::LOW: |
jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 49 | return HAPTIC_SCALE_LOW_RATIO; |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 50 | case HapticLevel::NONE: |
| 51 | case HapticLevel::HIGH: |
| 52 | case HapticLevel::VERY_HIGH: |
jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 53 | return 1.0f; |
| 54 | default: |
| 55 | return 0.0f; |
| 56 | } |
| 57 | } |
| 58 | |
Lais Andrade | d945111 | 2021-07-02 00:15:33 +0100 | [diff] [blame] | 59 | void applyHapticScale(float* buffer, size_t length, HapticScale scale) { |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 60 | if (scale.isScaleMute()) { |
Lais Andrade | d945111 | 2021-07-02 00:15:33 +0100 | [diff] [blame] | 61 | memset(buffer, 0, length * sizeof(float)); |
| 62 | return; |
| 63 | } |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 64 | if (scale.isScaleNone()) { |
Lais Andrade | d945111 | 2021-07-02 00:15:33 +0100 | [diff] [blame] | 65 | return; |
| 66 | } |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 67 | HapticLevel hapticLevel = scale.getLevel(); |
| 68 | float adaptiveScaleFactor = scale.getAdaptiveScaleFactor(); |
| 69 | float gamma = getHapticScaleGamma(hapticLevel); |
| 70 | float maxAmplitudeRatio = getHapticMaxAmplitudeRatio(hapticLevel); |
| 71 | |
Lais Andrade | d945111 | 2021-07-02 00:15:33 +0100 | [diff] [blame] | 72 | for (size_t i = 0; i < length; i++) { |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 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 | } |
Lais Andrade | d945111 | 2021-07-02 00:15:33 +0100 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 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 | |
jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 98 | } // namespace |
| 99 | |
| 100 | bool isValidHapticScale(HapticScale scale) { |
Ahmad Khalil | 2cd8035 | 2024-02-05 12:15:28 +0000 | [diff] [blame] | 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: |
jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
Lais Andrade | d945111 | 2021-07-02 00:15:33 +0100 | [diff] [blame] | 113 | void scaleHapticData(float* buffer, size_t length, HapticScale scale, float limit) { |
| 114 | if (isValidHapticScale(scale)) { |
| 115 | applyHapticScale(buffer, length, scale); |
jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 116 | } |
Lais Andrade | d945111 | 2021-07-02 00:15:33 +0100 | [diff] [blame] | 117 | clipHapticData(buffer, length, limit); |
jiabin | 06871bb | 2020-06-30 21:27:52 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | } // namespace android::os |