• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef AUDIO_FRAMEWORK_AUDIO_EFFECT_H
17 #define AUDIO_FRAMEWORK_AUDIO_EFFECT_H
18 
19 #include <cassert>
20 #include <cstdint>
21 #include <stddef.h>
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 #include "audio_info.h"
27 
28 #define AUDIO_EFFECT_LIBRARY_INFO_SYM_AS_STR "AELI"
29 #define EFFECT_STRING_LEN_MAX 64
30 
31 namespace OHOS {
32 namespace AudioStandard {
33 // audio effect manager info
34 constexpr int32_t AUDIO_EFFECT_COUNT_UPPER_LIMIT = 20;
35 
36 enum HdiSetParamCommandCode {
37     HDI_INIT = 0,
38     HDI_BYPASS = 1,
39     HDI_HEAD_MODE = 2,
40     HDI_ROOM_MODE = 3,
41     HDI_BLUETOOTH_MODE = 4,
42     HDI_DESTROY = 5,
43     HDI_UPDATE_SPATIAL_DEVICE_TYPE = 6,
44     HDI_VOLUME = 7,
45     HDI_ROTATION = 8,
46     HDI_EXTRA_SCENE_TYPE = 9,
47     HDI_SPATIALIZATION_SCENE_TYPE = 10,
48     HDI_STREAM_USAGE = 11,
49     HDI_FOLD_STATE = 12,
50     HDI_LID_STATE = 13,
51     HDI_QUERY_CHANNELLAYOUT = 14,
52 };
53 
54 enum AudioSpatialDeviceType {
55     EARPHONE_TYPE_NONE = 0,
56     EARPHONE_TYPE_INEAR,
57     EARPHONE_TYPE_HALF_INEAR,
58     EARPHONE_TYPE_HEADPHONE,
59     EARPHONE_TYPE_GLASSES,
60     EARPHONE_TYPE_OTHERS,
61 };
62 
63 enum FoldState : uint32_t {
64     FOLD_STATE_EXPAND = 1,
65     FOLD_STATE_CLOSE = 2,
66     FOLD_STATE_MIDDLE = 3,
67 };
68 
69 struct AudioSpatialDeviceState {
70     std::string address;
71     bool isSpatializationSupported;
72     bool isHeadTrackingSupported;
73     AudioSpatialDeviceType spatialDeviceType;
74 };
75 
76 struct Library {
77     std::string name;
78     std::string path;
79 };
80 
81 struct Effect {
82     std::string name;
83     std::string libraryName;
84     std::vector<std::string> effectProperty;
85 };
86 
87 struct EffectChain {
88     std::string name;
89     std::vector<std::string> apply;
90     std::string label = "";
91 };
92 
93 struct Device {
94     std::string type;
95     std::string chain;
96 };
97 
98 struct PreStreamScene {
99     std::string stream;
100     std::vector<std::string> mode;
101     std::vector<std::vector<Device>> device;
102 };
103 
104 struct PostStreamScene {
105     std::string stream;
106     std::vector<std::string> mode;
107     std::vector<std::vector<Device>> device;
108 };
109 
110 struct SceneMappingItem {
111     std::string name;
112     std::string sceneType;
113 };
114 
115 struct PreProcessConfig {
116     uint32_t maxExtSceneNum;
117     std::vector<PreStreamScene> defaultScenes;
118     std::vector<PreStreamScene> priorScenes;
119     std::vector<PreStreamScene> normalScenes;
120 };
121 
122 struct PostProcessConfig {
123     uint32_t maxExtSceneNum;
124     std::vector<PostStreamScene> defaultScenes;
125     std::vector<PostStreamScene> priorScenes;
126     std::vector<PostStreamScene> normalScenes;
127     std::vector<SceneMappingItem> sceneMap;
128 };
129 
130 struct OriginalEffectConfig {
131     std::string version;
132     std::vector<Library> libraries;
133     std::vector<Effect> effects;
134     std::vector<EffectChain> effectChains;
135     PreProcessConfig preProcess;
136     PostProcessConfig postProcess;
137 };
138 
139 struct EffectChainManagerParam {
140     uint32_t maxExtraNum = 0;
141     std::string defaultSceneName;
142     std::vector<std::string> priorSceneList;
143     std::unordered_map<std::string, std::string> sceneTypeToChainNameMap;
144     std::unordered_map<std::string, std::string> effectDefaultProperty;
145 };
146 
147 struct StreamEffectMode {
148     std::string mode;
149     std::vector<Device> devicePort;
150 };
151 
152 enum ScenePriority {
153     DEFAULT_SCENE = 0,
154     PRIOR_SCENE = 1,
155     NORMAL_SCENE = 2
156 };
157 
158 struct Stream {
159     ScenePriority priority;
160     std::string scene;
161     std::vector<StreamEffectMode> streamEffectMode;
162 };
163 
164 struct ProcessNew {
165     std::vector<Stream> stream;
166 };
167 
168 struct SupportedEffectConfig {
169     std::vector<EffectChain> effectChains;
170     ProcessNew preProcessNew;
171     ProcessNew postProcessNew;
172     std::vector<SceneMappingItem> postProcessSceneMap;
173 };
174 
175 
176 /**
177 * Enumerates the audio scene effect type.
178 */
179 enum AudioEffectScene {
180     SCENE_OTHERS = 0,
181     SCENE_MUSIC = 1,
182     SCENE_MOVIE = 2,
183     SCENE_GAME = 3,
184     SCENE_SPEECH = 4,
185     SCENE_RING = 5,
186     SCENE_VOIP_DOWN = 6,
187 };
188 
189 /**
190 * Enumerates the audio enhance scene effect type.
191 */
192 enum AudioEnhanceScene {
193     SCENE_VOIP_UP = 0,
194     SCENE_RECORD = 1,
195     SCENE_PRE_ENHANCE = 2,
196     SCENE_ASR = 4,
197     SCENE_VOICE_MESSAGE = 5,
198 };
199 
200 /**
201 * Enumerates the audio scene effct mode.
202 */
203 enum AudioEffectMode {
204     EFFECT_NONE = 0,
205     EFFECT_DEFAULT = 1
206 };
207 
208 /**
209 * Enumerates the audio enhance scene effct mode.
210 */
211 enum AudioEnhanceMode {
212     ENHANCE_NONE = 0,
213     ENHANCE_DEFAULT = 1
214 };
215 
216 struct AudioSceneEffectInfo {
217     std::vector<AudioEffectMode> mode;
218 };
219 
220 enum EffectFlag { RENDER_EFFECT_FLAG = 0, CAPTURE_EFFECT_FLAG = 1};
221 
222 struct AudioEffectPropertyV3 {
223     std::string name;
224     std::string category;
225     EffectFlag flag;
226     friend bool operator==(const AudioEffectPropertyV3 &lhs, const AudioEffectPropertyV3 &rhs)
227     {
228         return (lhs.category == rhs.category && lhs.name == rhs.name && lhs.flag == rhs.flag);
229     };
230     friend bool operator<(const AudioEffectPropertyV3 &lhs, const AudioEffectPropertyV3 &rhs)
231     {
232         return ((lhs.name == rhs.name) || (lhs.name == rhs.name && lhs.category < rhs.category)
233             || (lhs.name == rhs.name && lhs.category == rhs.category && lhs.flag < rhs.flag));
234     };
MarshallingAudioEffectPropertyV3235     bool Marshalling(Parcel &parcel) const
236     {
237         return parcel.WriteString(name)&&
238             parcel.WriteString(category)&&
239             parcel.WriteInt32(flag);
240     };
UnmarshallingAudioEffectPropertyV3241     void Unmarshalling(Parcel &parcel)
242     {
243         name = parcel.ReadString();
244         category = parcel.ReadString();
245         flag = static_cast<EffectFlag>(parcel.ReadInt32());
246     };
247 };
248 
249 struct AudioEffectPropertyArrayV3 {
250     std::vector<AudioEffectPropertyV3> property;
251 };
252 
253 struct AudioEnhanceProperty {
254     std::string enhanceClass;
255     std::string enhanceProp;
256     friend bool operator==(const AudioEnhanceProperty &lhs, const AudioEnhanceProperty &rhs)
257     {
258         return lhs.enhanceClass == rhs.enhanceClass && lhs.enhanceProp == rhs.enhanceProp;
259     }
MarshallingAudioEnhanceProperty260     bool Marshalling(Parcel &parcel) const
261     {
262         return parcel.WriteString(enhanceClass)&&
263             parcel.WriteString(enhanceProp);
264     }
UnmarshallingAudioEnhanceProperty265     void Unmarshalling(Parcel &parcel)
266     {
267         enhanceClass = parcel.ReadString();
268         enhanceProp = parcel.ReadString();
269     }
270 };
271 
272 struct AudioEnhancePropertyArray {
273     std::vector<AudioEnhanceProperty> property;
274 };
275 
276 struct AudioEffectProperty {
277     std::string effectClass;
278     std::string effectProp;
279     friend bool operator==(const AudioEffectProperty &lhs, const AudioEffectProperty &rhs)
280     {
281         return lhs.effectClass == rhs.effectClass && lhs.effectProp == rhs.effectProp;
282     }
MarshallingAudioEffectProperty283     bool Marshalling(Parcel &parcel) const
284     {
285         return parcel.WriteString(effectClass)&&
286             parcel.WriteString(effectProp);
287     }
UnmarshallingAudioEffectProperty288     void Unmarshalling(Parcel &parcel)
289     {
290         effectClass = parcel.ReadString();
291         effectProp = parcel.ReadString();
292     }
293 };
294 
295 struct AudioEffectPropertyArray {
296     std::vector<AudioEffectProperty> property;
297 };
298 
299 enum AudioEffectCommandCode {
300     EFFECT_CMD_INIT = 0,
301     EFFECT_CMD_SET_CONFIG = 1,
302     EFFECT_CMD_ENABLE = 2,
303     EFFECT_CMD_DISABLE = 3,
304     EFFECT_CMD_SET_PARAM = 4,
305     EFFECT_CMD_GET_PARAM = 5,
306     EFFECT_CMD_GET_CONFIG = 6,
307     EFFECT_CMD_SET_IMU = 7,
308     EFFECT_CMD_SET_PROPERTY = 8
309 };
310 
311 enum AudioEffectParamSetCode {
312     EFFECT_SET_BYPASS = 1,
313     EFFECT_SET_PARAM = 2,
314 };
315 
316 enum AudioDataFormat {
317     DATA_FORMAT_S16 = SAMPLE_S16LE,
318     DATA_FORMAT_S24 = SAMPLE_S24LE,
319     DATA_FORMAT_S32 = SAMPLE_S32LE,
320     DATA_FORMAT_F32 = SAMPLE_F32LE,
321 };
322 
323 struct AudioEffectParam {
324     int32_t status;
325     uint32_t paramSize;
326     uint32_t valueSize;
327     int32_t data[];
328 };
329 
330 struct AudioBuffer {
331     size_t frameLength;
332     union {
333         void*     raw;
334         float*    f32;
335         int32_t*  s32;
336         int16_t*  s16;
337         uint8_t*  u8;
338     };
339     void *metaData = nullptr;
340 };
341 
342 struct AudioBufferConfig {
343     uint32_t samplingRate;
344     uint32_t channels;
345     uint8_t format;
346     uint64_t channelLayout;
347     AudioEncodingType encoding;
348 };
349 
350 struct AudioEffectConfig {
351     AudioBufferConfig inputCfg;
352     AudioBufferConfig outputCfg;
353 };
354 
355 struct AudioEffectTransInfo {
356     uint32_t size;
357     void *data;
358 };
359 
360 struct AudioEffectDescriptor {
361     std::string libraryName;
362     std::string effectName;
363 };
364 
365 typedef struct AudioEffectInterface **AudioEffectHandle;
366 
367 struct AudioEffectInterface {
368     int32_t (*process) (AudioEffectHandle self, AudioBuffer *inBuffer, AudioBuffer *outBuffer);
369     int32_t (*command) (AudioEffectHandle self, uint32_t cmdCode,
370         AudioEffectTransInfo *cmdInfo, AudioEffectTransInfo *replyInfo);
371 };
372 
373 struct AudioEffectLibrary {
374     uint32_t version;
375     const char *name;
376     const char *implementor;
377     bool (*checkEffect) (const AudioEffectDescriptor descriptor);
378     int32_t (*createEffect) (const AudioEffectDescriptor descriptor, AudioEffectHandle *handle);
379     int32_t (*releaseEffect) (AudioEffectHandle handle);
380 };
381 
382 struct AudioEffectLibEntry {
383     AudioEffectLibrary *audioEffectLibHandle;
384     std::string libraryName;
385     std::vector<std::string> effectName;
386 };
387 
388 struct AudioSpatializationState {
389     bool spatializationEnabled = false;
390     bool headTrackingEnabled = false;
391 };
392 
393 struct ConverterConfig {
394     std::string version;
395     Library library;
396     uint64_t outChannelLayout = 0;
397 };
398 
399 enum AudioSpatializationSceneType {
400     SPATIALIZATION_SCENE_TYPE_DEFAULT = 0,
401     SPATIALIZATION_SCENE_TYPE_MUSIC = 1,
402     SPATIALIZATION_SCENE_TYPE_MOVIE = 2,
403     SPATIALIZATION_SCENE_TYPE_AUDIOBOOK = 3,
404     SPATIALIZATION_SCENE_TYPE_MAX = SPATIALIZATION_SCENE_TYPE_AUDIOBOOK,
405 };
406 
407 struct AudioRendererInfoForSpatialization {
408     RendererState rendererState;
409     std::string deviceMacAddress;
410     StreamUsage streamUsage;
411 };
412 
413 struct AudioEnhanceParam {
414     uint32_t muteInfo;
415     uint32_t volumeInfo;
416     uint32_t foldState;
417     const char *preDevice;
418     const char *postDevice;
419     const char *sceneType;
420     const char *preDeviceName;
421 };
422 
423 struct AlgoConfig {
424     uint32_t frameLength;
425     uint32_t sampleRate;
426     uint32_t dataFormat;
427     uint32_t micNum;
428     uint32_t ecNum;
429     uint32_t micRefNum;
430     uint32_t outNum;
431 };
432 } // namespace AudioStandard
433 } // namespace OHOS
434 
435 #endif // AUDIO_FRAMEWORK_AUDIO_EFFECT_H