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 17 #ifndef ANDROID_EFFECTHAPTICGENERATOR_H_ 18 #define ANDROID_EFFECTHAPTICGENERATOR_H_ 19 20 #include <functional> 21 #include <vector> 22 #include <map> 23 24 #include <hardware/audio_effect.h> 25 #include <system/audio_effect.h> 26 #include <vibrator/ExternalVibrationUtils.h> 27 28 #include "Processors.h" 29 30 namespace android::audio_effect::haptic_generator { 31 32 //----------------------------------------------------------------------------- 33 // Definition 34 //----------------------------------------------------------------------------- 35 36 enum hapticgenerator_state_t { 37 HAPTICGENERATOR_STATE_UNINITIALIZED, 38 HAPTICGENERATOR_STATE_INITIALIZED, 39 HAPTICGENERATOR_STATE_ACTIVE, 40 }; 41 42 // parameters for each haptic generator 43 struct HapticGeneratorParam { 44 uint32_t hapticChannelSource[2]; // The audio channels used to generate haptic channels. 45 // The first channel will be used to generate HAPTIC_A, 46 // The second channel will be used to generate HAPTIC_B 47 // The value will be offset of audio channel 48 uint32_t audioChannelCount; 49 uint32_t hapticChannelCount; 50 51 // A map from track id to haptic scale. 52 std::map<int, os::HapticScale> id2HapticScale; 53 os::HapticScale maxHapticScale; // max haptic scale will be used to scale haptic data. 54 float maxHapticAmplitude; // max amplitude will be used to limit haptic data absolute values. 55 56 float resonantFrequency; 57 float bpfQ; 58 float slowEnvNormalizationPower; 59 float bsfZeroQ; 60 float bsfPoleQ; 61 float distortionCornerFrequency; 62 float distortionInputGain; 63 float distortionCubeThreshold; 64 float distortionOutputGain; 65 }; 66 67 // A structure to keep all shared pointers for all processors in HapticGenerator. 68 struct HapticGeneratorProcessorsRecord { 69 std::vector<std::shared_ptr<HapticBiquadFilter>> filters; 70 std::vector<std::shared_ptr<Ramp>> ramps; 71 std::vector<std::shared_ptr<SlowEnvelope>> slowEnvs; 72 std::vector<std::shared_ptr<Distortion>> distortions; 73 74 // Cache band-pass filter and band-stop filter for updating parameters 75 // according to vibrator info 76 std::shared_ptr<HapticBiquadFilter> bpf; 77 std::shared_ptr<HapticBiquadFilter> bsf; 78 }; 79 80 // A structure to keep all the context for HapticGenerator. 81 struct HapticGeneratorContext { 82 const struct effect_interface_s *itfe; 83 effect_config_t config; 84 hapticgenerator_state_t state; 85 struct HapticGeneratorParam param; 86 size_t audioDataBytesPerFrame; 87 88 // A cache for all shared pointers of the HapticGenerator 89 struct HapticGeneratorProcessorsRecord processorsRecord; 90 91 // Using a vector of functions to record the processing chain for haptic-generating algorithm. 92 // The three parameters of the processing functions are pointer to output buffer, pointer to 93 // input buffer and frame count. 94 std::vector<std::function<void(float*, const float*, size_t)>> processingChain; 95 96 // inputBuffer is where to keep input buffer for the generating algorithm. It will be 97 // constructed according to HapticGeneratorParam.hapticChannelSource. 98 std::vector<float> inputBuffer; 99 100 // outputBuffer is a buffer having the same length as inputBuffer. It can be used as 101 // intermediate buffer in the generating algorithm. 102 std::vector<float> outputBuffer; 103 }; 104 105 //----------------------------------------------------------------------------- 106 // Effect API 107 //----------------------------------------------------------------------------- 108 109 int32_t HapticGeneratorLib_Create(const effect_uuid_t *uuid, 110 int32_t sessionId, 111 int32_t ioId, 112 effect_handle_t *handle); 113 114 int32_t HapticGeneratorLib_Release(effect_handle_t handle); 115 116 int32_t HapticGeneratorLib_GetDescriptor(const effect_uuid_t *uuid, 117 effect_descriptor_t *descriptor); 118 119 int32_t HapticGenerator_Process(effect_handle_t self, 120 audio_buffer_t *inBuffer, 121 audio_buffer_t *outBuffer); 122 123 int32_t HapticGenerator_Command(effect_handle_t self, 124 uint32_t cmdCode, 125 uint32_t cmdSize, 126 void *cmdData, 127 uint32_t *replySize, 128 void *replyData); 129 130 int32_t HapticGenerator_GetDescriptor(effect_handle_t self, 131 effect_descriptor_t *descriptor); 132 133 } // namespace android::audio_effect::haptic_generator 134 135 #endif // ANDROID_EFFECTHAPTICGENERATOR_H_ 136