| 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 |  | 
|  | 29 | float 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 |  | 
|  | 44 | float 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 |  | 
|  | 59 | } // namespace | 
|  | 60 |  | 
|  | 61 | bool isValidHapticScale(HapticScale scale) { | 
|  | 62 | switch (scale) { | 
|  | 63 | case HapticScale::MUTE: | 
|  | 64 | case HapticScale::VERY_LOW: | 
|  | 65 | case HapticScale::LOW: | 
|  | 66 | case HapticScale::NONE: | 
|  | 67 | case HapticScale::HIGH: | 
|  | 68 | case HapticScale::VERY_HIGH: | 
|  | 69 | return true; | 
|  | 70 | } | 
|  | 71 | return false; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | void scaleHapticData(float* buffer, size_t length, HapticScale scale) { | 
|  | 75 | if (!isValidHapticScale(scale) || scale == HapticScale::NONE) { | 
|  | 76 | return; | 
|  | 77 | } | 
|  | 78 | if (scale == HapticScale::MUTE) { | 
|  | 79 | memset(buffer, 0, length * sizeof(float)); | 
|  | 80 | return; | 
|  | 81 | } | 
|  | 82 | float gamma = getHapticScaleGamma(scale); | 
|  | 83 | float maxAmplitudeRatio = getHapticMaxAmplitudeRatio(scale); | 
|  | 84 | for (size_t i = 0; i < length; i++) { | 
|  | 85 | float sign = buffer[i] >= 0 ? 1.0 : -1.0; | 
|  | 86 | buffer[i] = powf(fabsf(buffer[i] / HAPTIC_MAX_AMPLITUDE_FLOAT), gamma) | 
|  | 87 | * maxAmplitudeRatio * HAPTIC_MAX_AMPLITUDE_FLOAT * sign; | 
|  | 88 | } | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | } // namespace android::os |