• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Audio Effect Management (ArkTS)
2
3You can manage the audio effect of a specific playback instance, for example, obtaining or setting the audio effect mode of the current audio playback stream. You can obtain the global audio effect, that is, the audio effect mode corresponding to a specific audio stream usage, which is specified by [StreamUsage](../reference/apis-audio-kit/js-apis-audio.md#streamusage). You can also obtain, set, and listen for spatial audio status and capabilities.
4
5## Managing the Audio Effect of a Playback Instance
6
7You can call [getAudioEffectMode()](../reference/apis-audio-kit/js-apis-audio.md#getaudioeffectmode10) and [setAudioEffectMode(mode: AudioEffectMode)](../reference/apis-audio-kit/js-apis-audio.md#setaudioeffectmode10) to obtain and set the [audio effect mode](../reference/apis-audio-kit/js-apis-audio.md#audioeffectmode10) of the current audio playback stream. The audio effect mode can be disabled (**EFFECT_NONE**) or default (**EFFECT_DEFAULT**). In the default audio effect mode, the audio effect of the corresponding scenario is automatically loaded based on [StreamUsage](../reference/apis-audio-kit/js-apis-audio.md#streamusage) of the audio stream.
8
9### Creating a Playback Instance
10
11Before the management, you must call [createAudioRenderer(options: AudioRendererOptions)](../reference/apis-audio-kit/js-apis-audio.md#audiocreateaudiorenderer8) to create an **AudioRenderer** instance.
12
131. Import the audio module.
14
15    ```ts
16    import audio from '@ohos.multimedia.audio';
17    ```
18
192. Configure audio rendering parameters and create an **AudioRenderer** instance. For details about the audio rendering parameters, see [AudioRendererOptions](../reference/apis-audio-kit/js-apis-audio.md#audiorendereroptions8). For the **AudioRenderer** instance, the audio effect mode **EFFECT_DEFAULT** is used by default.
20
21    ```ts
22    import { BusinessError } from '@ohos.base';
23
24    let audioStreamInfo: audio.AudioStreamInfo = {
25      samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
26      channels: audio.AudioChannel.CHANNEL_1,
27      sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
28      encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
29    };
30
31    let audioRendererInfo: audio.AudioRendererInfo = {
32      usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,
33      rendererFlags: 0
34    };
35
36    let audioRendererOptions: audio.AudioRendererOptions = {
37      streamInfo: audioStreamInfo,
38      rendererInfo: audioRendererInfo
39    };
40    let audioRenderer: audio.AudioRenderer | undefined = undefined;
41
42    audio.createAudioRenderer(audioRendererOptions, (err: BusinessError, data: audio.AudioRenderer) => {
43      if (err) {
44        console.error(`Invoke createAudioRenderer failed, code is ${err.code}, message is ${err.message}`);
45        return;
46      } else {
47        console.info('Invoke createAudioRenderer succeeded.');
48        audioRenderer = data;
49      }
50    });
51    ```
52
53### Obtaining the Audio Effect Mode of the Playback Instance
54
55  ```ts
56  import audio from '@ohos.multimedia.audio';
57  import { BusinessError } from '@ohos.base';
58
59  audioRenderer.getAudioEffectMode((err: BusinessError, effectMode: audio.AudioEffectMode) => {
60    if (err) {
61      console.error(`Failed to get params, code is ${err.code}, message is ${err.message}`);
62      return;
63    } else {
64      console.info(`getAudioEffectMode: ${effectMode}`);
65    }
66  });
67  ```
68
69### Setting an Audio Effect Mode for the Playback Instance
70
71Disable the system audio effect.
72
73  ```ts
74  import audio from '@ohos.multimedia.audio';
75  import { BusinessError } from '@ohos.base';
76
77  audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_NONE, (err: BusinessError) => {
78    if (err) {
79      console.error(`Failed to set params, code is ${err.code}, message is ${err.message}`);
80      return;
81    } else {
82      console.info('Callback invoked to indicate a successful audio effect mode setting.');
83    }
84  });
85  ```
86
87Enable the default system audio effect.
88
89  ```ts
90  import audio from '@ohos.multimedia.audio';
91  import { BusinessError } from '@ohos.base';
92
93  audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_DEFAULT, (err: BusinessError) => {
94    if (err) {
95      console.error(`Failed to set params, code is ${err.code}, message is ${err.message}`);
96      return;
97    } else {
98      console.info('Callback invoked to indicate a successful audio effect mode setting.');
99    }
100  });
101  ```
102
103## Obtaining the Global Audio Effect Mode
104
105Obtain the global audio effect mode corresponding to a specific audio stream usage (specified by [StreamUsage](../reference/apis-audio-kit/js-apis-audio.md#streamusage)).
106
107For an audio playback application, pay attention to the audio effect mode used by the audio stream of the application and perform corresponding operations. For example, for a music application, select the audio effect mode for the music scenario. Before obtaining the global audio effect mode, call **getStreamManager()** to create an **AudioStreamManager** instance.
108
109### Creating an AudioStreamManager Instance
110
111Before using **AudioStreamManager** APIs, you must use **getStreamManager()** to create an **AudioStreamManager** instance.
112
113   ```ts
114   import audio from '@ohos.multimedia.audio';
115
116   let audioManager = audio.getAudioManager();
117   let audioStreamManager = audioManager.getStreamManager();
118   ```
119
120### Querying the Audio Effect Mode of the Corresponding Scenario
121
122  ```ts
123  import audio from '@ohos.multimedia.audio';
124  import { BusinessError } from '@ohos.base';
125
126  audioStreamManager.getAudioEffectInfoArray(audio.StreamUsage.STREAM_USAGE_MEDIA, async (err: BusinessError, audioEffectInfoArray: audio.AudioEffectInfoArray) => {
127    if (err) {
128      console.error('Failed to get effect info array');
129      return;
130    } else {
131      console.info(`getAudioEffectInfoArray: ${audioEffectInfoArray}`);
132    }
133  });
134  ```
135
136## Spatial Audio Management (for System Applications Only)
137
138Spatial audio management is available only for system applications. It enables a system application to obtain, set, and listen for spatial audio status (enabled/disabled status of spatial audio rendering or head tracking), obtain spatial audio capabilities (support for spatial audio rendering and head tracking), and update the state information of spatial devices.
139
140A system application that plays audio can check whether the system or a device supports spatial audio rendering or head tracking and whether spatial audio rendering or head tracking is enabled.
141
142A system application with spatial audio setting capabilities (for example, a setting screen where users can change the spatial audio status) can enable or disable spatial audio rendering or head tracking, and update the state information of a spatial device, in addition to the query operation.
143
144To use this feature, the application must request the **ohos.permission.MANAGE_SYSTEM_AUDIO_EFFECTS** permission. For details, see [Requesting Permissions for system_basic Applications](../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications).
145
146### Obtaining an AudioSpatializationManager Instance
147
148Before using **AudioSpatializationManager** APIs, you must use **getSpatializationManager()** to obtain an **AudioSpatializationManager** instance.
149
150  ```ts
151  import audio from '@ohos.multimedia.audio';
152
153  let audioManager = audio.getAudioManager();
154  let audioSpatializationManager = audioManager.getSpatializationManager();
155  ```
156
157### Checking Whether the System Supports Spatial Audio Rendering
158
159Call [isSpatializationSupported](../reference/apis-audio-kit/js-apis-audio-sys.md#isspatializationsupported11) to check whether the system supports spatial audio rendering.
160
161  ```ts
162  import { BusinessError } from '@ohos.base';
163
164  try {
165    let isSpatializationSupported: boolean = audioSpatializationManager.isSpatializationSupported();
166    console.info(`AudioSpatializationManager isSpatializationSupported: ${isSpatializationSupported}`);
167  } catch (err) {
168    let error = err as BusinessError;
169    console.error(`ERROR: ${error}`);
170  }
171  ```
172
173### Checking Whether a Device Supports Spatial Audio Rendering
174
175Call [isSpatializationSupportedForDevice](../reference/apis-audio-kit/js-apis-audio-sys.md#isspatializationsupportedfordevice11) to check whether a device (specified by **AudioDeviceDescriptor**) supports spatial audio rendering. You are advised to use other audio APIs to obtain **AudioDeviceDescriptor** of a connected device or the current audio device. For details, see [AudioDeviceDescriptor](../reference/apis-audio-kit/js-apis-audio.md#audiodevicedescriptor).
176
177  ```ts
178  import audio from '@ohos.multimedia.audio';
179  import { BusinessError } from '@ohos.base';
180
181  let deviceDescriptor: audio.AudioDeviceDescriptor = {
182    deviceRole : audio.DeviceRole.OUTPUT_DEVICE,
183    deviceType : audio.DeviceType.BLUETOOTH_A2DP,
184    id : 1,
185    name : "",
186    address : "123",
187    sampleRates : [44100],
188    channelCounts : [2],
189    channelMasks : [0],
190    networkId : audio.LOCAL_NETWORK_ID,
191    interruptGroupId : 1,
192    volumeGroupId : 1,
193    displayName : ""
194  }
195  try {
196    let isSpatializationSupportedForDevice: boolean = audioSpatializationManager.isSpatializationSupportedForDevice(deviceDescriptor);
197    console.info(`AudioSpatializationManager isSpatializationSupportedForDevice: ${isSpatializationSupportedForDevice}`);
198  } catch (err) {
199    let error = err as BusinessError;
200    console.error(`ERROR: ${error}`);
201  }
202  ```
203
204### Checking Whether the System Supports Head Tracking
205
206Call [isHeadTrackingSupported](../reference/apis-audio-kit/js-apis-audio-sys.md#isheadtrackingsupported11) to check whether the system supports head tracking.
207
208  ```ts
209  import { BusinessError } from '@ohos.base';
210
211  try {
212    let isHeadTrackingSupported: boolean = audioSpatializationManager.isHeadTrackingSupported();
213    console.info(`AudioSpatializationManager isHeadTrackingSupported: ${isHeadTrackingSupported}`);
214  } catch (err) {
215    let error = err as BusinessError;
216    console.error(`ERROR: ${error}`);
217  }
218  ```
219
220### Checking Whether a Device Supports Head Tracking
221
222Call [isHeadTrackingSupportedForDevice](../reference/apis-audio-kit/js-apis-audio-sys.md#isheadtrackingsupportedfordevice11) to check whether a device (specified by **AudioDeviceDescriptor**) supports head tracking. You are advised to use other audio APIs to obtain **AudioDeviceDescriptor** of a connected device or the current audio device. For details, see [AudioDeviceDescriptor](../reference/apis-audio-kit/js-apis-audio.md#audiodevicedescriptor).
223
224  ```ts
225  import audio from '@ohos.multimedia.audio';
226  import { BusinessError } from '@ohos.base';
227
228  let deviceDescriptor: audio.AudioDeviceDescriptor = {
229    deviceRole : audio.DeviceRole.OUTPUT_DEVICE,
230    deviceType : audio.DeviceType.BLUETOOTH_A2DP,
231    id : 1,
232    name : "",
233    address : "123",
234    sampleRates : [44100],
235    channelCounts : [2],
236    channelMasks : [0],
237    networkId : audio.LOCAL_NETWORK_ID,
238    interruptGroupId : 1,
239    volumeGroupId : 1,
240    displayName : ""
241  }
242  try {
243    let isHeadTrackingSupportedForDevice: boolean = audioSpatializationManager.isHeadTrackingSupportedForDevice(deviceDescriptor);
244    console.info(`AudioSpatializationManager isHeadTrackingSupportedForDevice: ${isHeadTrackingSupportedForDevice}`);
245  } catch (err) {
246    let error = err as BusinessError;
247    console.error(`ERROR: ${error}`);
248  }
249  ```
250
251### Enabling or Disabling Spatial Audio Rendering
252
253Call [setSpatializationEnabled](../reference/apis-audio-kit/js-apis-audio-sys.md#setspatializationenabled11) to enable or disable spatial audio rendering. Pass in **true** to enable spatial audio rendering, and pass in **false** to disable it.
254
255To use this feature, the application must request the **ohos.permission.MANAGE_SYSTEM_AUDIO_EFFECTS** permission. For details, see [Requesting Permissions for system_basic Applications](../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications).
256
257Before enabling spatial audio rendering, ensure that both the system and the current audio device support spatial audio rendering. In addition, if **AudioEffectMode** of the audio stream is set to **EFFECT_NONE**, spatial audio rendering does not take effect regardless of whether it is enabled.
258
259  ```ts
260  import { BusinessError } from '@ohos.base';
261
262  let enable: boolean = true
263  audioSpatializationManager.setSpatializationEnabled(enable, (err: BusinessError) => {
264    if (err) {
265      console.error(`Result ERROR: ${err}`);
266    } else {
267      console.info(`setSpatializationEnabled success`);
268    }
269  });
270  ```
271
272### Checking the Status of Spatial Audio Rendering
273
274Call [isSpatializationEnabled](../reference/apis-audio-kit/js-apis-audio-sys.md#isspatializationenabled11) to check whether spatial audio rendering is enabled. If **true** is returned, spatial audio rendering is enabled. If **false** is returned, it is disabled. This API returns the value passed in **setSpatializationEnabled()**. The default value is **true**. Note that spatial audio rendering takes effect only when the system and the current audio device support spatial audio rendering and **AudioEffectMode** of the audio stream is not set to **EFFECT_NONE**.
275
276  ```ts
277  import { BusinessError } from '@ohos.base';
278
279  try {
280    let isSpatializationEnabled: boolean = audioSpatializationManager.isSpatializationEnabled();
281    console.info(`AudioSpatializationManager isSpatializationEnabled: ${isSpatializationEnabled}`);
282  } catch (err) {
283    let error = err as BusinessError;
284    console.error(`ERROR: ${error}`);
285  }
286  ```
287
288### Subscribing to Spatial Audio Rendering Status Changes
289
290Call [on('spatializationEnabledChange')](../reference/apis-audio-kit/js-apis-audio-sys.md#onspatializationenabledchange11) to subscribe to spatial audio rendering status changes. In the callback, the value **true** means that spatial audio rendering is enabled, and **false** means the opposite. The callback is triggered when spatial audio rendering is enabled or disabled through **setSpatializationEnabled()**.
291
292  ```ts
293  import { BusinessError } from '@ohos.base';
294
295  audioSpatializationManager.on('spatializationEnabledChange', (isSpatializationEnabled: boolean) => {
296    console.info(`isSpatializationEnabled: ${isSpatializationEnabled}`);
297  });
298  ```
299
300### Unsubscribing from Spatial Audio Rendering Status Changes
301
302Call [off('spatializationEnabledChange')](../reference/apis-audio-kit/js-apis-audio-sys.md#offspatializationenabledchange11) to unsubscribe from spatial audio rendering status changes.
303
304  ```ts
305  import { BusinessError } from '@ohos.base';
306
307  audioSpatializationManager.off('spatializationEnabledChange');
308  ```
309
310### Enabling or Disabling Head Tracking
311
312Call [setHeadTrackingEnabled](../reference/apis-audio-kit/js-apis-audio-sys.md#setheadtrackingenabled11) to enable or disable head tracking. Pass in **true** to enable head tracking, and pass in **false** to disable it.
313
314To use this feature, the application must request the **ohos.permission.MANAGE_SYSTEM_AUDIO_EFFECTS** permission. For details, see [Requesting Permissions for system_basic Applications](../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications).
315
316Before enabling head tracking, ensure that both the system and the current audio device support head tracking. In addition, if **AudioEffectMode** of the audio stream is set to **EFFECT_NONE**, head tracking does not take effect regardless of whether it is enabled.
317
318  ```ts
319  import { BusinessError } from '@ohos.base';
320
321  let enable: boolean = true;
322  audioSpatializationManager.setHeadTrackingEnabled(enable, (err: BusinessError) => {
323    if (err) {
324      console.error(`Result ERROR: ${err}`);
325    } else {
326      console.info(`setHeadTrackingEnabled success`);
327    }
328  });
329  ```
330
331### Checking the Status of Head tracking
332
333Call [isHeadTrackingEnabled](../reference/apis-audio-kit/js-apis-audio-sys.md#isheadtrackingenabled11) to check whether head tracking is enabled. If **true** is returned, head tracking is enabled. If **false** is returned, it is disabled. This API returns the value passed in **setHeadTrackingEnabled()**. The default value is **false**. Note that head tracking takes effect only when the system and the current audio device support head tracking and **AudioEffectMode** of the audio stream is not set to **EFFECT_NONE**.
334
335  ```ts
336  import { BusinessError } from '@ohos.base';
337
338  try {
339    let isHeadTrackingEnabled: boolean = audioSpatializationManager.isHeadTrackingEnabled();
340    console.info(`AudioSpatializationManager isHeadTrackingEnabled: ${isHeadTrackingEnabled}`);
341  } catch (err) {
342    let error = err as BusinessError;
343    console.error(`ERROR: ${error}`);
344  }
345  ```
346
347### Subscribing to Head Tracking Status Changes
348
349Call [on('headTrackingEnabledChange')](../reference/apis-audio-kit/js-apis-audio-sys.md#onheadtrackingenabledchange11) to subscribe to head tracking status changes. In the callback, the value **true** means that head tracking is enabled, and **false** means the opposite. The callback is triggered when head tracking is enabled or disabled through **setHeadTrackingEnabled()**.
350
351  ```ts
352  audioSpatializationManager.on('headTrackingEnabledChange', (isHeadTrackingEnabled: boolean) => {
353    console.info(`isHeadTrackingEnabled: ${isHeadTrackingEnabled}`);
354  });
355  ```
356
357### Unsubscribing from Head Tracking Status Changes
358
359Call [off('headTrackingEnabledChange')](../reference/apis-audio-kit/js-apis-audio-sys.md#offheadtrackingenabledchange11) to unsubscribe from head tracking status changes.
360
361  ```ts
362  audioSpatializationManager.off('headTrackingEnabledChange');
363  ```
364
365### Updating the State Information of a Spatial Device
366
367Call [updateSpatialDeviceState](../reference/apis-audio-kit/js-apis-audio-sys.md#updatespatialdevicestate11) to update the state information of a spatial device. The state information includes the device address, support for spatial audio rendering and head tracking, and device form.
368
369To use this feature, the application must request the **ohos.permission.MANAGE_SYSTEM_AUDIO_EFFECTS** permission. For details, see [Requesting Permissions for system_basic Applications](../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications).
370
371For details about the state information, see [AudioSpatialDeviceState](../reference/apis-audio-kit/js-apis-audio-sys.md#audiospatialdevicestate).
372
373  ```ts
374  import audio from '@ohos.multimedia.audio';
375  import { BusinessError } from '@ohos.base';
376
377  let spatialDeviceState: audio.AudioSpatialDeviceState = {
378    address: "123",
379    isSpatializationSupported: true,
380    isHeadTrackingSupported: true,
381    spatialDeviceType: audio.AudioSpatialDeviceType.SPATIAL_DEVICE_TYPE_IN_EAR_HEADPHONE
382  }
383  try {
384    audioSpatializationManager.updateSpatialDeviceState(spatialDeviceState);
385    console.info(`AudioSpatializationManager updateSpatialDeviceState success`);
386  } catch (err) {
387    let error = err as BusinessError;
388    console.error(`ERROR: ${error}`);
389  }
390  ```
391