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         TYPE_UNPROCESSED = 14
84     };
85 
86     enum AudioDataCallbackResult {
87         CALLBACK_RESULT_INVALID = -1,
88         CALLBACK_RESULT_VALID = 0,
89     };
90 
91     static napi_value Init(napi_env env, napi_value exports);
92     static bool IsLegalInputArgumentInterruptMode(int32_t interruptMode);
93     static bool IsLegalInputArgumentAudioEffectMode(int32_t audioEffectMode);
94     static bool IsLegalInputArgumentChannelBlendMode(int32_t blendMode);
95     static bool IsLegalCapturerType(int32_t type);
96     static bool IsLegalInputArgumentVolType(int32_t inputType);
97     static bool IsLegalInputArgumentRingMode(int32_t ringMode);
98     static bool IsLegalInputArgumentVolumeAdjustType(int32_t adjustType);
99     static bool IsLegalInputArgumentDeviceType(int32_t deviceType);
100     static bool IsLegalInputArgumentDefaultOutputDeviceType(int32_t deviceType);
101     static bool IsLegalInputArgumentDeviceFlag(int32_t deviceFlag);
102     static bool IsLegalInputArgumentActiveDeviceType(int32_t activeDeviceFlag);
103     static int32_t GetJsAudioVolumeType(AudioStreamType volumeType);
104     static bool IsLegalInputArgumentCommunicationDeviceType(int32_t communicationDeviceType);
105     static bool IsValidSourceType(int32_t intValue);
106     static bool IsLegalDeviceUsage(int32_t usage);
107     static bool IsLegalInputArgumentStreamUsage(int32_t streamUsage);
108     static bool IsLegalOutputDeviceType(int32_t deviceType);
109     static AudioVolumeType GetNativeAudioVolumeType(int32_t volumeType);
110     static AudioRingerMode GetNativeAudioRingerMode(int32_t ringMode);
111     static AudioRingMode GetJsAudioRingMode(int32_t ringerMode);
112     static AudioStandard::FocusType GetNativeFocusType(int32_t focusType);
113     static AudioStandard::InterruptMode GetNativeInterruptMode(int32_t interruptMode);
114     static bool IsLegalInputArgumentSpatializationSceneType(int32_t spatializationSceneType);
115 
116 private:
117     static void Destructor(napi_env env, void *nativeObject, void *finalizeHint);
118     static napi_status InitAudioEnum(napi_env env, napi_value exports);
119     static napi_status InitAudioExternEnum(napi_env env, napi_value exports);
120     static napi_value Construct(napi_env env, napi_callback_info info);
121     static NapiAudioEnum* SetValue(napi_env env, napi_callback_info info, napi_value *args, napi_value &result);
122     static NapiAudioEnum* GetValue(napi_env env, napi_callback_info info);
123     static napi_value GetAudioSampleFormat(napi_env env, napi_callback_info info);
124     static napi_value SetAudioSampleFormat(napi_env env, napi_callback_info info);
125     static napi_value GetAudioChannel(napi_env env, napi_callback_info info);
126     static napi_value SetAudioChannel(napi_env env, napi_callback_info info);
127     static napi_value GetAudioSamplingRate(napi_env env, napi_callback_info info);
128     static napi_value SetAudioSamplingRate(napi_env env, napi_callback_info info);
129     static napi_value GetAudioEncodingType(napi_env env, napi_callback_info info);
130     static napi_value SetAudioEncodingType(napi_env env, napi_callback_info info);
131     static napi_value GetContentType(napi_env env, napi_callback_info info);
132     static napi_value SetContentType(napi_env env, napi_callback_info info);
133     static napi_value GetStreamUsage(napi_env env, napi_callback_info info);
134     static napi_value SetStreamUsage(napi_env env, napi_callback_info info);
135     static napi_value GetDeviceRole(napi_env env, napi_callback_info info);
136     static napi_value SetDeviceRole(napi_env env, napi_callback_info info);
137     static napi_value GetDeviceType(napi_env env, napi_callback_info info);
138     static napi_value SetDeviceType(napi_env env, napi_callback_info info);
139 
140     static napi_value CreateEnumObject(const napi_env &env, const std::map<std::string, int32_t> &map,
141         napi_ref &ref);
142     static napi_value CreateEnumInt64Object(const napi_env &env, const std::map<std::string, uint64_t> &map,
143         napi_ref &ref);
144     static napi_value CreateLocalNetworkIdObject(napi_env env);
145     static napi_value CreateDefaultVolumeGroupIdObject(napi_env env);
146     static napi_value CreateDefaultInterruptIdObject(napi_env env);
147 
148     static napi_ref sConstructor_;
149     static napi_ref audioChannel_;
150     static napi_ref samplingRate_;
151     static napi_ref encodingType_;
152     static napi_ref contentType_;
153     static napi_ref streamUsage_;
154     static napi_ref deviceRole_;
155     static napi_ref deviceType_;
156     static napi_ref sourceType_;
157     static napi_ref volumeAdjustType_;
158     static napi_ref channelBlendMode_;
159     static napi_ref audioRendererRate_;
160     static napi_ref interruptEventType_;
161     static napi_ref interruptForceType_;
162     static napi_ref interruptHintType_;
163     static napi_ref audioState_;
164     static napi_ref sampleFormat_;
165     static napi_ref audioEffectMode_;
166     static napi_ref audioPrivacyType_;
167     static napi_ref audioVolumeTypeRef_;
168     static napi_ref deviceFlagRef_;
169     static napi_ref activeDeviceTypeRef_;
170     static napi_ref audioRingModeRef_;
171     static napi_ref deviceChangeType_;
172     static napi_ref interruptActionType_;
173     static napi_ref audioScene_;
174     static napi_ref interruptMode_;
175     static napi_ref focusType_;
176     static napi_ref connectTypeRef_;
177     static napi_ref audioErrors_;
178     static napi_ref communicationDeviceType_;
179     static napi_ref interruptRequestType_;
180     static napi_ref interruptRequestResultType_;
181     static napi_ref toneType_;
182     static napi_ref audioDviceUsage_;
183     static napi_ref audioSpatialDeivceType_;
184     static napi_ref audioChannelLayout_;
185     static napi_ref audioStreamDeviceChangeReason_;
186     static napi_ref spatializationSceneType_;
187     static napi_ref asrNoiseSuppressionMode_;
188     static napi_ref asrAecMode_;
189     static napi_ref asrWhisperDetectionMode_;
190     static napi_ref asrVoiceControlMode_;
191     static napi_ref asrVoiceMuteMode_;
192     static napi_ref policyType_;
193     static napi_ref audioDataCallbackResult_;
194     static napi_ref concurrencyMode_;
195     static napi_ref reason_;
196 
197     static const std::map<std::string, int32_t> audioChannelMap;
198     static const std::map<std::string, int32_t> samplingRateMap;
199     static const std::map<std::string, int32_t> encodingTypeMap;
200     static const std::map<std::string, int32_t> contentTypeMap;
201     static const std::map<std::string, int32_t> streamUsageMap;
202     static const std::map<std::string, int32_t> deviceRoleMap;
203     static const std::map<std::string, int32_t> deviceTypeMap;
204     static const std::map<std::string, int32_t> sourceTypeMap;
205     static const std::map<std::string, int32_t> volumeAdjustTypeMap;
206     static const std::map<std::string, int32_t> channelBlendModeMap;
207     static const std::map<std::string, int32_t> rendererRateMap;
208     static const std::map<std::string, int32_t> interruptEventTypeMap;
209     static const std::map<std::string, int32_t> interruptForceTypeMap;
210     static const std::map<std::string, int32_t> interruptHintTypeMap;
211     static const std::map<std::string, int32_t> audioSampleFormatMap;
212     static const std::map<std::string, int32_t> audioStateMap;
213     static const std::map<std::string, int32_t> audioPrivacyTypeMap;
214     static const std::map<std::string, int32_t> effectModeMap;
215     static const std::map<std::string, int32_t> deviceChangeTypeMap;
216     static const std::map<std::string, int32_t> audioSceneMap;
217     static const std::map<std::string, int32_t> interruptActionTypeMap;
218     static const std::map<std::string, int32_t> audioVolumeTypeMap;
219     static const std::map<std::string, int32_t> activeDeviceTypeMap;
220     static const std::map<std::string, int32_t> interruptModeMap;
221     static const std::map<std::string, int32_t> focusTypeMap;
222     static const std::map<std::string, int32_t> audioErrorsMap;
223     static const std::map<std::string, int32_t> communicationDeviceTypeMap;
224     static const std::map<std::string, int32_t> interruptRequestTypeMap;
225     static const std::map<std::string, int32_t> interruptRequestResultTypeMap;
226     static const std::map<std::string, int32_t> deviceFlagMap;
227     static const std::map<std::string, int32_t> connectTypeMap;
228     static const std::map<std::string, int32_t> audioRingModeMap;
229     static const std::map<std::string, int32_t> toneTypeMap;
230     static const std::map<std::string, int32_t> audioDeviceUsageMap;
231     static const std::map<std::string, int32_t> audioSpatialDeivceTypeMap;
232     static const std::map<std::string, uint64_t> audioChannelLayoutMap;
233     static const std::map<std::string, int32_t> audioDeviceChangeReasonMap;
234     static const std::map<std::string, int32_t> spatializationSceneTypeMap;
235     static const std::map<std::string, int32_t> asrNoiseSuppressionModeMap;
236     static const std::map<std::string, int32_t> asrAecModeMap;
237     static const std::map<std::string, int32_t> asrWhisperDetectionModeMap;
238     static const std::map<std::string, int32_t> asrVoiceControlModeMap;
239     static const std::map<std::string, int32_t> asrVoiceMuteModeMap;
240     static const std::map<std::string, int32_t> policyTypeMap;
241     static const std::map<std::string, int32_t> audioDataCallbackResultMap;
242     static const std::map<std::string, int32_t> concurrencyModeMap;
243     static const std::map<std::string, int32_t> reasonMap;
244     static std::unique_ptr<AudioParameters> sAudioParameters_;
245 
246     std::unique_ptr<AudioParameters> audioParameters_;
247     napi_env env_;
248 };
249 } // namespace AudioStandard
250 } // namespace OHOS
251 #endif // OHOS_NAPI_AUDIO_ENUM_H
252