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