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 #ifndef NAPI_AUDIO_ENUM_H 16 #define NAPI_AUDIO_ENUM_H 17 18 #include <map> 19 #include <string> 20 #include "napi/native_api.h" 21 #include "napi/native_common.h" 22 #include "napi/native_node_api.h" 23 #include "tone_player.h" 24 25 namespace OHOS { 26 namespace AudioStandard { 27 const int32_t REFERENCE_CREATION_COUNT = 1; 28 29 class NapiAudioEnum { 30 public: 31 NapiAudioEnum(); 32 ~NapiAudioEnum(); 33 enum AudioSampleFormatNapi { 34 SAMPLE_FORMAT_INVALID = -1, 35 SAMPLE_FORMAT_U8 = 0, 36 SAMPLE_FORMAT_S16LE = 1, 37 SAMPLE_FORMAT_S24LE = 2, 38 SAMPLE_FORMAT_S32LE = 3, 39 SAMPLE_FORMAT_F32LE = 4 40 }; 41 42 enum AudioJsVolumeType { 43 VOLUMETYPE_DEFAULT = -1, 44 VOICE_CALL = 0, 45 RINGTONE = 2, 46 MEDIA = 3, 47 ALARM = 4, 48 ACCESSIBILITY = 5, 49 SYSTEM = 6, 50 VOICE_ASSISTANT = 9, 51 ULTRASONIC = 10, 52 VOLUMETYPE_MAX, 53 ALL = 100 54 }; 55 56 enum AudioJsVolumeMode { 57 SYSTEM_GLOBAL = 0, 58 APP_INDIVIDUAL 59 }; 60 61 enum AudioRingMode { 62 RINGER_MODE_SILENT = 0, 63 RINGER_MODE_VIBRATE, 64 RINGER_MODE_NORMAL 65 }; 66 67 enum InterruptMode { 68 SHARE_MODE = 0, 69 INDEPENDENT_MODE = 1 70 }; 71 72 enum FocusType { 73 FOCUS_TYPE_RECORDING 74 }; 75 76 enum CapturerType { 77 TYPE_INVALID = -1, 78 TYPE_MIC = 0, 79 TYPE_VOICE_RECOGNITION = 1, 80 TYPE_WAKEUP = 3, 81 TYPE_VOICE_CALL = 4, 82 TYPE_PLAYBACK_CAPTURE = 2, 83 TYPE_COMMUNICATION = 7, 84 TYPE_MESSAGE = 10, 85 TYPE_REMOTE_CAST = 11, 86 TYPE_VOICE_TRANSCRIPTION = 12, 87 TYPE_CAMCORDER = 13, 88 TYPE_UNPROCESSED = 14 89 }; 90 91 enum AudioDataCallbackResult { 92 CALLBACK_RESULT_INVALID = -1, 93 CALLBACK_RESULT_VALID = 0, 94 }; 95 96 static napi_value Init(napi_env env, napi_value exports); 97 static bool IsLegalInputArgumentInterruptMode(int32_t interruptMode); 98 static bool IsLegalInputArgumentAudioEffectMode(int32_t audioEffectMode); 99 static bool IsLegalInputArgumentVolumeMode(int32_t volumeMode); 100 static bool IsLegalInputArgumentChannelBlendMode(int32_t blendMode); 101 static bool IsLegalCapturerType(int32_t type); 102 static bool IsLegalInputArgumentVolType(int32_t inputType); 103 static bool IsLegalInputArgumentRingMode(int32_t ringMode); 104 static bool IsLegalInputArgumentVolumeAdjustType(int32_t adjustType); 105 static bool IsLegalInputArgumentDeviceType(int32_t deviceType); 106 static bool IsLegalInputArgumentDefaultOutputDeviceType(int32_t deviceType); 107 static bool IsLegalInputArgumentDeviceFlag(int32_t deviceFlag); 108 static bool IsLegalInputArgumentActiveDeviceType(int32_t activeDeviceFlag); 109 static int32_t GetJsAudioVolumeType(AudioStreamType volumeType); 110 static int32_t GetJsAudioVolumeMode(AudioVolumeMode volumeMode); 111 static bool IsLegalInputArgumentCommunicationDeviceType(int32_t communicationDeviceType); 112 static bool IsValidSourceType(int32_t intValue); 113 static bool IsLegalDeviceUsage(int32_t usage); 114 static bool IsLegalInputArgumentStreamUsage(int32_t streamUsage); 115 static bool IsLegalOutputDeviceType(int32_t deviceType); 116 static AudioVolumeType GetNativeAudioVolumeType(int32_t volumeType); 117 static AudioRingerMode GetNativeAudioRingerMode(int32_t ringMode); 118 static AudioRingMode GetJsAudioRingMode(int32_t ringerMode); 119 static AudioStandard::FocusType GetNativeFocusType(int32_t focusType); 120 static AudioStandard::InterruptMode GetNativeInterruptMode(int32_t interruptMode); 121 static bool IsLegalInputArgumentSpatializationSceneType(int32_t spatializationSceneType); 122 123 private: 124 static void Destructor(napi_env env, void *nativeObject, void *finalizeHint); 125 static napi_status InitAudioEnum(napi_env env, napi_value exports); 126 static napi_status InitAudioExternEnum(napi_env env, napi_value exports); 127 static napi_value Construct(napi_env env, napi_callback_info info); 128 static NapiAudioEnum* SetValue(napi_env env, napi_callback_info info, napi_value *args, napi_value &result); 129 static NapiAudioEnum* GetValue(napi_env env, napi_callback_info info); 130 static napi_value GetAudioSampleFormat(napi_env env, napi_callback_info info); 131 static napi_value SetAudioSampleFormat(napi_env env, napi_callback_info info); 132 static napi_value GetAudioChannel(napi_env env, napi_callback_info info); 133 static napi_value SetAudioChannel(napi_env env, napi_callback_info info); 134 static napi_value GetAudioSamplingRate(napi_env env, napi_callback_info info); 135 static napi_value SetAudioSamplingRate(napi_env env, napi_callback_info info); 136 static napi_value GetAudioEncodingType(napi_env env, napi_callback_info info); 137 static napi_value SetAudioEncodingType(napi_env env, napi_callback_info info); 138 static napi_value GetContentType(napi_env env, napi_callback_info info); 139 static napi_value SetContentType(napi_env env, napi_callback_info info); 140 static napi_value GetStreamUsage(napi_env env, napi_callback_info info); 141 static napi_value SetStreamUsage(napi_env env, napi_callback_info info); 142 static napi_value GetDeviceRole(napi_env env, napi_callback_info info); 143 static napi_value SetDeviceRole(napi_env env, napi_callback_info info); 144 static napi_value GetDeviceType(napi_env env, napi_callback_info info); 145 static napi_value SetDeviceType(napi_env env, napi_callback_info info); 146 static napi_value GetVolumeMode(napi_env env, napi_callback_info info); 147 static napi_value SetVolumeMode(napi_env env, napi_callback_info info); 148 149 static napi_value CreateEnumObject(const napi_env &env, const std::map<std::string, int32_t> &map, 150 napi_ref &ref); 151 static napi_value CreateEnumInt64Object(const napi_env &env, const std::map<std::string, uint64_t> &map, 152 napi_ref &ref); 153 static napi_value CreateLocalNetworkIdObject(napi_env env); 154 static napi_value CreateDefaultVolumeGroupIdObject(napi_env env); 155 static napi_value CreateDefaultInterruptIdObject(napi_env env); 156 157 static napi_ref sConstructor_; 158 static napi_ref audioChannel_; 159 static napi_ref samplingRate_; 160 static napi_ref encodingType_; 161 static napi_ref contentType_; 162 static napi_ref streamUsage_; 163 static napi_ref audioVolumeMode_; 164 static napi_ref deviceRole_; 165 static napi_ref deviceType_; 166 static napi_ref sourceType_; 167 static napi_ref volumeAdjustType_; 168 static napi_ref channelBlendMode_; 169 static napi_ref audioRendererRate_; 170 static napi_ref interruptEventType_; 171 static napi_ref interruptForceType_; 172 static napi_ref interruptHintType_; 173 static napi_ref audioState_; 174 static napi_ref sampleFormat_; 175 static napi_ref audioEffectMode_; 176 static napi_ref audioPrivacyType_; 177 static napi_ref audioVolumeTypeRef_; 178 static napi_ref deviceFlagRef_; 179 static napi_ref activeDeviceTypeRef_; 180 static napi_ref audioRingModeRef_; 181 static napi_ref deviceChangeType_; 182 static napi_ref interruptActionType_; 183 static napi_ref audioScene_; 184 static napi_ref interruptMode_; 185 static napi_ref focusType_; 186 static napi_ref connectTypeRef_; 187 static napi_ref audioErrors_; 188 static napi_ref communicationDeviceType_; 189 static napi_ref interruptRequestType_; 190 static napi_ref interruptRequestResultType_; 191 static napi_ref toneType_; 192 static napi_ref audioDviceUsage_; 193 static napi_ref audioSpatialDeivceType_; 194 static napi_ref audioChannelLayout_; 195 static napi_ref audioStreamDeviceChangeReason_; 196 static napi_ref spatializationSceneType_; 197 static napi_ref asrNoiseSuppressionMode_; 198 static napi_ref asrAecMode_; 199 static napi_ref asrWhisperDetectionMode_; 200 static napi_ref asrVoiceControlMode_; 201 static napi_ref asrVoiceMuteMode_; 202 static napi_ref policyType_; 203 static napi_ref audioDataCallbackResult_; 204 static napi_ref concurrencyMode_; 205 static napi_ref reason_; 206 207 static const std::map<std::string, int32_t> audioChannelMap; 208 static const std::map<std::string, int32_t> samplingRateMap; 209 static const std::map<std::string, int32_t> encodingTypeMap; 210 static const std::map<std::string, int32_t> contentTypeMap; 211 static const std::map<std::string, int32_t> streamUsageMap; 212 static const std::map<std::string, int32_t> audioVolumeModeMap; 213 static const std::map<std::string, int32_t> deviceRoleMap; 214 static const std::map<std::string, int32_t> deviceTypeMap; 215 static const std::map<std::string, int32_t> sourceTypeMap; 216 static const std::map<std::string, int32_t> volumeAdjustTypeMap; 217 static const std::map<std::string, int32_t> channelBlendModeMap; 218 static const std::map<std::string, int32_t> rendererRateMap; 219 static const std::map<std::string, int32_t> interruptEventTypeMap; 220 static const std::map<std::string, int32_t> interruptForceTypeMap; 221 static const std::map<std::string, int32_t> interruptHintTypeMap; 222 static const std::map<std::string, int32_t> audioSampleFormatMap; 223 static const std::map<std::string, int32_t> audioStateMap; 224 static const std::map<std::string, int32_t> audioPrivacyTypeMap; 225 static const std::map<std::string, int32_t> effectModeMap; 226 static const std::map<std::string, int32_t> deviceChangeTypeMap; 227 static const std::map<std::string, int32_t> audioSceneMap; 228 static const std::map<std::string, int32_t> interruptActionTypeMap; 229 static const std::map<std::string, int32_t> audioVolumeTypeMap; 230 static const std::map<std::string, int32_t> activeDeviceTypeMap; 231 static const std::map<std::string, int32_t> interruptModeMap; 232 static const std::map<std::string, int32_t> focusTypeMap; 233 static const std::map<std::string, int32_t> audioErrorsMap; 234 static const std::map<std::string, int32_t> communicationDeviceTypeMap; 235 static const std::map<std::string, int32_t> interruptRequestTypeMap; 236 static const std::map<std::string, int32_t> interruptRequestResultTypeMap; 237 static const std::map<std::string, int32_t> deviceFlagMap; 238 static const std::map<std::string, int32_t> connectTypeMap; 239 static const std::map<std::string, int32_t> audioRingModeMap; 240 static const std::map<std::string, int32_t> toneTypeMap; 241 static const std::map<std::string, int32_t> audioDeviceUsageMap; 242 static const std::map<std::string, int32_t> audioSpatialDeivceTypeMap; 243 static const std::map<std::string, uint64_t> audioChannelLayoutMap; 244 static const std::map<std::string, int32_t> audioDeviceChangeReasonMap; 245 static const std::map<std::string, int32_t> spatializationSceneTypeMap; 246 static const std::map<std::string, int32_t> asrNoiseSuppressionModeMap; 247 static const std::map<std::string, int32_t> asrAecModeMap; 248 static const std::map<std::string, int32_t> asrWhisperDetectionModeMap; 249 static const std::map<std::string, int32_t> asrVoiceControlModeMap; 250 static const std::map<std::string, int32_t> asrVoiceMuteModeMap; 251 static const std::map<std::string, int32_t> policyTypeMap; 252 static const std::map<std::string, int32_t> audioDataCallbackResultMap; 253 static const std::map<std::string, int32_t> concurrencyModeMap; 254 static const std::map<std::string, int32_t> reasonMap; 255 static std::unique_ptr<AudioParameters> sAudioParameters_; 256 257 std::unique_ptr<AudioParameters> audioParameters_; 258 napi_env env_; 259 }; 260 } // namespace AudioStandard 261 } // namespace OHOS 262 #endif // OHOS_NAPI_AUDIO_ENUM_H 263