README.md
1# Audio<a name="EN-US_TOPIC_0000001146901937"></a>
2
3 - [Introduction](#introduction)
4 - [Basic Concepts](#basic-concepts)
5 - [Directory Structure](#directory-structure)
6 - [Usage Guidelines](#usage-guidelines)
7 - [Audio Playback](#audio-playback)
8 - [Audio Recording](#audio-recording)
9 - [Audio Management](#audio-management)
10 - [Supported Devices](#supported-devices)
11 - [Repositories Involved](#repositories-involved)
12
13## Introduction<a name="introduction"></a>
14The **audio\_standard** repository is used to implement audio-related features, including audio playback, recording, volume management and device management.
15
16**Figure 1** Position in the subsystem architecture<a name="fig483116248288"></a>
17
18
19![](figures/en-us_image_0000001152315135.png)
20
21### Basic Concepts<a name="basic-concepts"></a>
22
23- **Sampling**
24
25Sampling is a process to obtain discrete-time signals by extracting samples from analog signals in a continuous time domain at a specific interval.
26
27- **Sampling rate**
28
29Sampling rate is the number of samples extracted from a continuous signal per second to form a discrete signal. It is measured in Hz. Generally, human hearing range is from 20 Hz to 20 kHz. Common audio sampling rates include 8 kHz, 11.025 kHz, 22.05 kHz, 16 kHz, 37.8 kHz, 44.1 kHz, 48 kHz, and 96 kHz.
30
31- **Channel**
32
33Channels refer to different spatial positions where independent audio signals are recorded or played. The number of channels is the number of audio sources used during audio recording, or the number of speakers used for audio playback.
34
35- **Audio frame**
36
37Audio data is in stream form. For the convenience of audio algorithm processing and transmission, it is generally agreed that a data amount in a unit of 2.5 to 60 milliseconds is one audio frame. This unit is called sampling time, and its length is specific to codecs and the application requirements.
38
39- **PCM**
40
41Pulse code modulation \(PCM\) is a method used to digitally represent sampled analog signals. It converts continuous-time analog signals into discrete-time digital signal samples.
42
43## Directory Structure<a name="directory-structure"></a>
44
45The structure of the repository directory is as follows:
46
47```
48/foundation/multimedia/audio_framework # Audio code
49├── frameworks # Framework code
50│ ├── native # Internal Native API Implementation.
51| | Pulseaudio, libsndfile build configuration and pulseaudio-hdi modules
52│ └── js # External JS API Implementation
53 └── napi # JS NAPI API Implementation
54├── interfaces # Interfaces
55│ ├── inner_api # Internal Native APIs
56│ └── kits # External JS APIs
57├── sa_profile # Service configuration profile
58├── services # Service code
59├── LICENSE # License file
60└── ohos.build # Build file
61```
62
63## Usage Guidelines<a name="usage-guidelines"></a>
64### Audio Playback<a name="audio-playback"></a>
65You can use APIs provided in this repository to convert audio data into audible analog signals, play the audio signals using output devices, and manage playback tasks. The following steps describe how to use **AudioRenderer** to develop the audio playback function:
661. Use **Create** API with required renderer configuration to get **AudioRenderer** instance.
67 ```
68 AudioRendererOptions rendererOptions;
69 rendererOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
70 rendererOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
71 rendererOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
72 rendererOptions.streamInfo.channels = AudioChannel::STEREO;
73 rendererOptions.rendererInfo.contentType = ContentType::CONTENT_TYPE_MUSIC;
74 rendererOptions.rendererInfo.streamUsage = StreamUsage::STREAM_USAGE_MEDIA;
75 rendererOptions.rendererInfo.rendererFlags = 0;
76
77 unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(rendererOptions);
78 ```
792. (Optional) Static APIs **GetSupportedFormats**(), **GetSupportedChannels**(), **GetSupportedEncodingTypes**(), **GetSupportedSamplingRates**() can be used to get the supported values of the params.
80
813. (Optional) use audioRenderer->**GetRendererInfo**(AudioRendererInfo &) and audioRenderer->**GetStreamInfo**(AudioStreamInfo &) to retrieve the current renderer configuration values.
82
834. Inorder to listen to Audio Interrupt and state change events, it would be required to register to **RendererCallbacks** using audioRenderer->**SetRendererCallback**
84 ```
85 class AudioRendererCallbackImpl : public AudioRendererCallback {
86 void OnInterrupt(const InterruptEvent &interruptEvent) override
87 {
88 if (interruptEvent.forceType == INTERRUPT_FORCE) { // Forced actions taken by the framework
89 switch (interruptEvent.hintType) {
90 case INTERRUPT_HINT_PAUSE:
91 // Force paused. Pause Writing.
92 isRenderPaused_ = true;
93 case INTERRUPT_HINT_STOP:
94 // Force stopped. Stop Writing.
95 isRenderStopped_ = true;
96 }
97 }
98 if (interruptEvent.forceType == INTERRUPT_SHARE) { // Actions not forced, apps can choose to handle.
99 switch (interruptEvent.hintType) {
100 case INTERRUPT_HINT_PAUSE:
101 // Do Pause, if required.
102 case INTERRUPT_HINT_RESUME:
103 // After force pause, resume if needed when this hint is received.
104 audioRenderer->Start();
105 }
106 }
107 }
108
109 void OnStateChange(const RendererState state, const StateChangeCmdType cmdType) override
110 {
111 switch (state) {
112 case RENDERER_PREPARED:
113 // Renderer prepared
114 case RENDERER_RUNNING:
115 // Renderer in running state
116 case RENDERER_STOPPED:
117 // Renderer stopped
118 case RENDERER_RELEASED:
119 // Renderer released
120 case RENDERER_PAUSED:
121 // Renderer paused
122 }
123 }
124 }
125
126 std::shared_ptr<AudioRendererCallback> audioRendererCB = std::make_shared<AudioRendererCallbackImpl>();
127 audioRenderer->SetRendererCallback(audioRendererCB);
128 ```
129
130 Implement **AudioRendererCallback** class, override **OnInterrupt** function and register this instance using **SetRendererCallback** API.
131 On registering to the callback, the application would receive the interrupt events.
132
133 This will have information on the audio interrupt forced action taken by the Audio framework and also the action hints to be handled by the application. Refer to **audio_renderer.h** and **audio_info.h** for more details.
134
135 Similarly, renderer state change callbacks can be received by overriding **OnStateChange** function in **AudioRendererCallback** class. Refer to **audio_renderer.h** for the list of renderer states.
136
1375. In order to get callbacks for frame mark position and/or frame period position, register for the corresponding callbacks in audio renderer using audioRenderer->**SetRendererPositionCallback** and/or audioRenderer->**SetRendererPeriodPositionCallback** functions respectively.
138 ```
139 class RendererPositionCallbackImpl : public RendererPositionCallback {
140 void OnMarkReached(const int64_t &framePosition) override
141 {
142 // frame mark reached
143 // framePosition is the frame mark number
144 }
145 }
146
147 std::shared_ptr<RendererPositionCallback> framePositionCB = std::make_shared<RendererPositionCallbackImpl>();
148 //markPosition is the frame mark number for which callback is requested.
149 audioRenderer->SetRendererPositionCallback(markPosition, framePositionCB);
150
151 class RendererPeriodPositionCallbackImpl : public RendererPeriodPositionCallback {
152 void OnPeriodReached(const int64_t &frameNumber) override
153 {
154 // frame period reached
155 // frameNumber is the frame period number
156 }
157 }
158
159 std::shared_ptr<RendererPeriodPositionCallback> periodPositionCB = std::make_shared<RendererPeriodPositionCallbackImpl>();
160 //framePeriodNumber is the frame period number for which callback is requested.
161 audioRenderer->SetRendererPeriodPositionCallback(framePeriodNumber, periodPositionCB);
162 ```
163 For unregistering the position callbacks, call the corresponding audioRenderer->**UnsetRendererPositionCallback** and/or audioRenderer->**UnsetRendererPeriodPositionCallback** APIs.
164
1656. Call audioRenderer->**Start**() function on the AudioRenderer instance to start the playback task.
1667. Get the buffer length to be written, using **GetBufferSize** API.
167 ```
168 audioRenderer->GetBufferSize(bufferLen);
169 ```
1708. Read the audio data to be played from the source(for example, an audio file) and transfer it into the bytes stream. Call the **Write** function repeatedly to write the render data.
171 ```
172 bytesToWrite = fread(buffer, 1, bufferLen, wavFile);
173 while ((bytesWritten < bytesToWrite) && ((bytesToWrite - bytesWritten) > minBytes)) {
174 int32_t retBytes = audioRenderer->Write(buffer + bytesWritten, bytesToWrite - bytesWritten);
175 if (bytesWritten < 0)
176 break;
177 bytesWritten += retBytes;
178 }
179 ```
1809. In case of audio interrupts, application can encounter write failures. Interrupt unaware applications can check the renderer state using **GetStatus** API before writing audio data further.
181Interrupt aware applications will have more details accessible via AudioRendererCallback..
182 ```
183 while ((bytesWritten < bytesToWrite) && ((bytesToWrite - bytesWritten) > minBytes)) {
184 int32_t retBytes = audioRenderer->Write(buffer.get() + bytesWritten, bytesToWrite - bytesWritten);
185 if (retBytes < 0) { // Error occurred
186 if (audioRenderer_->GetStatus() == RENDERER_PAUSED) { // Query the state and take appropriate action
187 isRenderPaused_ = true;
188 int32_t seekPos = bytesWritten - bytesToWrite;
189 fseek(wavFile, seekPos, SEEK_CUR))
190 }
191 break;
192 }
193 bytesWritten += retBytes;
194 }
195 ```
19610. Call audioRenderer->**Drain**() to drain the playback stream.
197
19811. Call audioRenderer->**Stop**() function to Stop rendering.
19912. After the playback task is complete, call the audioRenderer->**Release**() function on the AudioRenderer instance to release the stream resources.
200
20113. Use audioRenderer->**SetVolume(float)** and audioRenderer->**GetVolume()** to set and get Track Volume. Value ranges from 0.0 to 1.0
202
203Provided the basic playback usecase above.
204
205Please refer [**audio_renderer.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audiorenderer/include/audio_renderer.h) and [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h) for more such useful APIs.
206
207
208### Audio Recording<a name="audio-recording"></a>
209You can use the APIs provided in this repository for your application to record voices using input devices, convert the voices into audio data, and manage recording tasks. The following steps describe how to use **AudioCapturer** to develop the audio recording function:
210
2111. Use **Create** API with required capturer configuration to get **AudioCapturer** instance.
212 ```
213 AudioCapturerOptions capturerOptions;
214 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_48000;
215 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
216 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
217 capturerOptions.streamInfo.channels = AudioChannel::MONO;
218 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
219 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;;
220
221 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
222 ```
2232. (Optional) Static APIs **GetSupportedFormats**(), **GetSupportedChannels**(), **GetSupportedEncodingTypes**(), **GetSupportedSamplingRates()** can be used to get the supported values of the params.
224
2254. (Optional) use audioCapturer->**GetCapturerInfo**(AudioCapturerInfo &) and audioCapturer->**GetStreamInfo**(AudioStreamInfo &) to retrieve the current capturer configuration values.
226
2275. Capturer state change callbacks can be received by overriding **OnStateChange** function in **AudioCapturerCallback** class, and registering the callback instance using audioCapturer->**SetCapturerCallback** API.
228 ```
229 class AudioCapturerCallbackImpl : public AudioCapturerCallback {
230 void OnStateChange(const CapturerState state) override
231 {
232 switch (state) {
233 case CAPTURER_PREPARED:
234 // Capturer prepared
235 case CAPTURER_RUNNING:
236 // Capturer in running state
237 case CAPTURER_STOPPED:
238 // Capturer stopped
239 case CAPTURER_RELEASED:
240 // Capturer released
241 }
242 }
243 }
244
245 std::shared_ptr<AudioCapturerCallback> audioCapturerCB = std::make_shared<AudioCapturerCallbackImpl>();
246 audioCapturer->SetCapturerCallback(audioCapturerCB);
247 ```
248
2496. In order to get callbacks for frame mark position and/or frame period position, register for the corresponding callbacks in audio capturer using audioCapturer->**SetCapturerPositionCallback** and/or audioCapturer->**SetCapturerPeriodPositionCallback** functions respectively.
250 ```
251 class CapturerPositionCallbackImpl : public CapturerPositionCallback {
252 void OnMarkReached(const int64_t &framePosition) override
253 {
254 // frame mark reached
255 // framePosition is the frame mark number
256 }
257 }
258
259 std::shared_ptr<CapturerPositionCallback> framePositionCB = std::make_shared<CapturerPositionCallbackImpl>();
260 //markPosition is the frame mark number for which callback is requested.
261 audioCapturer->SetCapturerPositionCallback(markPosition, framePositionCB);
262
263 class CapturerPeriodPositionCallbackImpl : public CapturerPeriodPositionCallback {
264 void OnPeriodReached(const int64_t &frameNumber) override
265 {
266 // frame period reached
267 // frameNumber is the frame period number
268 }
269 }
270
271 std::shared_ptr<CapturerPeriodPositionCallback> periodPositionCB = std::make_shared<CapturerPeriodPositionCallbackImpl>();
272 //framePeriodNumber is the frame period number for which callback is requested.
273 audioCapturer->SetCapturerPeriodPositionCallback(framePeriodNumber, periodPositionCB);
274 ```
275 For unregistering the position callbacks, call the corresponding audioCapturer->**UnsetCapturerPositionCallback** and/or audioCapturer->**UnsetCapturerPeriodPositionCallback** APIs.
276
2777. Call audioCapturer->**Start**() function on the AudioCapturer instance to start the recording task.
278
2798. Get the buffer length to be read, using **GetBufferSize** API.
280 ```
281 audioCapturer->GetBufferSize(bufferLen);
282 ```
2839. Read the captured audio data and convert it to a byte stream. Call the read function repeatedly to read data until you want to stop recording
284 ```
285 // set isBlocking = true/false for blocking/non-blocking read
286 bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlocking);
287 while (numBuffersToCapture) {
288 bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
289 if (bytesRead < 0) {
290 break;
291 } else if (bytesRead > 0) {
292 fwrite(buffer, size, bytesRead, recFile); // example shows writes the recorded data into a file
293 numBuffersToCapture--;
294 }
295 }
296 ```
29710. (Optional) Call audioCapturer->**Flush**() to flush the capture buffer of this stream.
29811. Call the audioCapturer->**Stop**() function on the AudioCapturer instance to stop the recording.
29912. After the recording task is complete, call the audioCapturer->**Release**() function on the AudioCapturer instance to release the stream resources.
300
301Provided the basic recording usecase above. Please refer [**audio_capturer.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audiocapturer/include/audio_capturer.h) and [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h) for more APIs.
302
303### Audio Management<a name="audio-management"></a>
304You can use the APIs provided in [**audio_system_manager.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h) to control volume and device.
3051. Use **GetInstance** API to get **AudioSystemManager** instance.
306 ```
307 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
308 ```
309#### Volume Control
3102. Use **GetMaxVolume** and **GetMinVolume** APIs to query the Maximum & Minimum volume level allowed for the stream. Use this volume range to set the volume.
311 ```
312 AudioVolumeType streamType = AudioVolumeType::STREAM_MUSIC;
313 int32_t maxVol = audioSystemMgr->GetMaxVolume(streamType);
314 int32_t minVol = audioSystemMgr->GetMinVolume(streamType);
315 ```
3163. Use **SetVolume** and **GetVolume** APIs to set and get the volume level of the stream.
317 ```
318 int32_t result = audioSystemMgr->SetVolume(streamType, 10);
319 int32_t vol = audioSystemMgr->GetVolume(streamType);
320 ```
3214. Use **SetMute** and **IsStreamMute** APIs to set and get the mute status of the stream.
322 ```
323 int32_t result = audioSystemMgr->SetMute(streamType, true);
324 bool isMute = audioSystemMgr->IsStreamMute(streamType);
3255. Use **SetRingerMode** and **GetRingerMode** APIs to set and get ringer modes. Refer **AudioRingerMode** enum in [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h) for supported ringer modes.
326 ```
327 int32_t result = audioSystemMgr->SetRingerMode(RINGER_MODE_SILENT);
328 AudioRingerMode ringMode = audioSystemMgr->GetRingerMode();
329 ```
3306. Use **SetMicrophoneMute** and **IsMicrophoneMute** APIs to mute/unmute the mic and to check if mic muted.
331 ```
332 int32_t result = audioSystemMgr->SetMicrophoneMute(true);
333 bool isMicMute = audioSystemMgr->IsMicrophoneMute();
334 ```
335#### Device control
3367. Use **GetDevices**, **deviceType_** and **deviceRole_** APIs to get audio I/O devices information. For DeviceFlag, DeviceType and DeviceRole enums refer [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h).
337 ```
338 DeviceFlag deviceFlag = ALL_DEVICES_FLAG;
339 vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors = audioSystemMgr->GetDevices(deviceFlag);
340 for (auto &audioDeviceDescriptor : audioDeviceDescriptors) {
341 cout << audioDeviceDescriptor->deviceType_ << endl;
342 cout << audioDeviceDescriptor->deviceRole_ << endl;
343 }
344 ```
3458. Use **SetDeviceActive** and **IsDeviceActive** APIs to Actiavte/Deactivate the device and to check if the device is active.
346 ```
347 ActiveDeviceType deviceType = SPEAKER;
348 int32_t result = audioSystemMgr->SetDeviceActive(deviceType, true);
349 bool isDevActive = audioSystemMgr->IsDeviceActive(deviceType);
350 ```
351
3529. Use **SetDeviceChangeCallback** API to register for device change events. Clients will receive callback when a device is connected/disconnected. Currently audio subsystem supports sending device change events for WIRED_HEADSET, USB_HEADSET and BLUETOOTH_A2DP device.
353**OnDeviceChange** function will be called and client will receive **DeviceChangeAction** object, which will contain following parameters:\
354*type* : **DeviceChangeType** which specifies whether device is connected or disconnected.\
355*deviceDescriptors* : Array of **AudioDeviceDescriptor** object which specifies the type of device and its role(input/output device).
356 ```
357 class DeviceChangeCallback : public AudioManagerDeviceChangeCallback {
358 public:
359 DeviceChangeCallback = default;
360 ~DeviceChangeCallback = default;
361 void OnDeviceChange(const DeviceChangeAction &deviceChangeAction) override
362 {
363 cout << deviceChangeAction.type << endl;
364 for (auto &audioDeviceDescriptor : deviceChangeAction.deviceDescriptors) {
365 switch (audioDeviceDescriptor->deviceType_) {
366 case DEVICE_TYPE_WIRED_HEADSET: {
367 if (deviceChangeAction.type == CONNECT) {
368 cout << wired headset connected << endl;
369 } else {
370 cout << wired headset disconnected << endl;
371 }
372 break;
373 }
374 case DEVICE_TYPE_USB_HEADSET:{
375 if (deviceChangeAction.type == CONNECT) {
376 cout << usb headset connected << endl;
377 } else {
378 cout << usb headset disconnected << endl;
379 }
380 break;
381 }
382 case DEVICE_TYPE_BLUETOOTH_A2DP:{
383 if (deviceChangeAction.type == CONNECT) {
384 cout << Bluetooth device connected << endl;
385 } else {
386 cout << Bluetooth device disconnected << endl;
387 }
388 break;
389 }
390 default: {
391 cout << "Unsupported device" << endl;
392 break;
393 }
394 }
395 }
396 }
397 };
398
399 auto callback = std::make_shared<DeviceChangeCallback>();
400 audioSystemMgr->SetDeviceChangeCallback(callback);
401 ```
402
40310. Other useful APIs such as **IsStreamActive**, **SetAudioParameter** and **GetAudioParameter** are also provided. Please refer [**audio_system_manager.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h) for more details
404
40511. Applications can register for change in system volume using **AudioManagerNapi::On**. Here when an application registers to volume change event, whenever there is change in volume, the application is notified with following parameters:
406volumeType : The AudioVolumeType for which volume is updated
407volume : The curret volume level set.
408updateUi : Whether the volume change details need to be shown or not. (If volume is updated through volume key up/down we set the updateUi flag to true, in other scenarios the updateUi is set as false).
409 ```
410 const audioManager = audio.getAudioManager();
411
412 export default {
413 onCreate() {
414 audioManager.on('volumeChange', (volumeChange) ==> {
415 console.info('volumeType = '+volumeChange.volumeType);
416 console.info('volume = '+volumeChange.volume);
417 console.info('updateUi = '+volumeChange.updateUi);
418 }
419 }
420 }
421 ```
422
423#### Audio Scene
42412. Use **SetAudioscene** and **getAudioScene** APIs to change and check the audio strategy, respectively.
425 ```
426 int32_t result = audioSystemMgr->SetAudioScene(AUDIO_SCENE_PHONE_CALL);
427 AudioScene audioScene = audioSystemMgr->GetAudioScene();
428 ```
429Please refer **AudioScene** enum in [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h) for supported audio scenes.
430
431#### JavaScript Usage:
432JavaScript apps can use the APIs provided by audio manager to control the volume and the device.\
433Please refer [**js-apis-audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md#audiomanager) for complete JavaScript APIs available for audio manager.
434
435### Ringtone Management<a name="ringtone-management"></a>
436You can use the APIs provided in [**iringtone_sound_manager.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audioringtone/include/iringtone_sound_manager.h) and [**iringtone_player.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audioringtone/include/iringtone_player.h) for ringtone playback functions.
4371. Use **CreateRingtoneManager** API to get **IRingtoneSoundManager** instance.
438 ```
439 std::shared_ptr<IRingtoneSoundManager> ringtoneManagerClient = RingtoneFactory::CreateRingtoneManager();
440 ```
4412. Use **SetSystemRingtoneUri** API to set the system ringtone uri.
442 ```
443 std::string uri = "/data/media/test.wav";
444 RingtoneType ringtoneType = RINGTONE_TYPE_DEFAULT;
445 ringtoneManagerClient->SetSystemRingtoneUri(context, uri, ringtoneType);
446 ```
4473. Use **GetRingtonePlayer** API to get **IRingtonePlayer** instance.
448 ```
449 std::unique_ptr<IRingtonePlayer> ringtonePlayer = ringtoneManagerClient->GetRingtonePlayer(context, ringtoneType);
450 ```
4514. Use **Configure** API to configure the ringtone player.
452 ```
453 float volume = 1;
454 bool loop = true;
455 ringtonePlayer.Configure(volume, loop);
456 ```
4575. Use **Start**, **Stop**, and **Release** APIs on ringtone player instance to control playback states.
458 ```
459 ringtonePlayer.Start();
460 ringtonePlayer.Stop();
461 ringtonePlayer.Release();
462 ```
4636. Use **GetTitle** API to get the title of current system ringtone.
4647. Use **GetRingtoneState** to the the ringtone playback state - **RingtoneState**
4658. Use **GetAudioRendererInfo** to get the **AudioRendererInfo** to check the content type and stream usage.
466
467
468## Supported devices<a name="supported-devices"></a>
469Currently following are the list of device types supported by audio subsystem.
470
4711. **USB Type-C Headset**\
472 Digital headset which includes their own DAC (Digital to Analogue Converter) and amp as part of the headset.
4732. **WIRED Headset**\
474 Analog headset which doesn't contain any DAC inside. It can have 3.5mm jack or Type-C jack without DAC.
4753. **Bluetooth Headset**\
476 Bluetooth A2DP(Advanced Audio Distribution Profile) headset used for streaming audio wirelessly.
4774. **Internal Speaker and MIC**\
478 Internal speaker and mic is supported and will be used as default device for playback and record respectively.
479
480## Repositories Involved<a name="repositories-involved"></a>
481
482[multimedia\_audio\_framework](https://gitee.com/openharmony/multimedia_audio_framework)
483
README_zh.md
1# 音频组件<a name="ZH-CN_TOPIC_0000001146901937"></a>
2
3- [简介](#section119mcpsimp)
4 - [基本概念](#section122mcpsimp)
5
6- [目录](#section179mcpsimp)
7- [使用说明](#section112738505318)
8 - [音频播放](#section1147510562812)
9 - [音频录制](#section295162052813)
10 - [音频管理](#section645572311287)
11 - [音量控制](#section645572311287_001)
12 - [设备控制](#section645572311287_002)
13 - [音频场景](#section645572311287_003)
14 - [音频流管理](#section645572311287_004)
15 - [JavaScript 用法](#section645572311287_005)
16 - [铃声管理](#section645572311287_006)
17 - [蓝牙SCO呼叫](#section645572311287_007)
18- [支持设备](#section645572311287_008)
19- [相关仓](#section340mcpsimp)
20
21## 简介<a name="section119mcpsimp"></a>
22
23音频组件用于实现音频相关的功能,包括音频播放,录制,音量管理和设备管理。
24
25**图 1** 音频组件架构图<a name="fig483116248288"></a>
26
27
28![](figures/zh-cn_image_0000001152315135.png)
29
30### 基本概念<a name="section122mcpsimp"></a>
31
32- **采样**
33
34采样是指将连续时域上的模拟信号按照一定的时间间隔采样,获取到离散时域上离散信号的过程。
35
36- **采样率**
37
38采样率为每秒从连续信号中提取并组成离散信号的采样次数,单位用赫兹(Hz)来表示。通常人耳能听到频率范围大约在20Hz~20kHz之间的声音。常用的音频采样频率有:8kHz、11.025kHz、22.05kHz、16kHz、37.8kHz、44.1kHz、48kHz、96kHz、192kHz等。
39
40- **声道**
41
42声道是指声音在录制或播放时在不同空间位置采集或回放的相互独立的音频信号,所以声道数也就是声音录制时的音源数量或回放时相应的扬声器数量。
43
44- **音频帧**
45
46音频数据是流式的,本身没有明确的一帧帧的概念,在实际的应用中,为了音频算法处理/传输的方便,一般约定俗成取2.5ms\~60ms为单位的数据量为一帧音频。这个时间被称之为“采样时间”,其长度没有特别的标准,它是根据编解码器和具体应用的需求来决定的。
47
48- **PCM**
49
50PCM(Pulse Code Modulation),即脉冲编码调制,是一种将模拟信号数字化的方法,是将时间连续、取值连续的模拟信号转换成时间离散、抽样值离散的数字信号的过程。
51
52## 目录<a name="section179mcpsimp"></a>
53
54仓目录结构如下:
55
56```
57/foundation/multimedia/audio_standard # 音频组件业务代码
58├── frameworks # 框架代码
59│ ├── native # 内部接口实现
60│ └── js # 外部接口实现
61│ └── napi # napi 外部接口实现
62├── interfaces # 接口代码
63│ ├── inner_api # 内部接口
64│ └── kits # 外部接口
65├── sa_profile # 服务配置文件
66├── services # 服务代码
67├── LICENSE # 证书文件
68└── ohos.build # 编译文件
69```
70
71## 使用说明<a name="section112738505318"></a>
72
73### 音频播放<a name="section1147510562812"></a>
74
75可以使用此仓库内提供的接口将音频数据转换为音频模拟信号,使用输出设备播放音频信号,以及管理音频播放任务。以下步骤描述了如何使用 **AudioRenderer** 开发音频播放功能:
76
771. 使用 **Create** 接口和所需流类型来获取 **AudioRenderer** 实例。
78
79 ```
80 AudioStreamType streamType = STREAM_MUSIC; // 流类型示例
81 std::unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(streamType);
82 ```
83
842. (可选)静态接口 **GetSupportedFormats**(), **GetSupportedChannels**(), **GetSupportedEncodingTypes**(), **GetSupportedSamplingRates**() 可用于获取支持的参数。
853. 准备设备,调用实例的 **SetParams** 。
86
87 ```
88 AudioRendererParams rendererParams;
89 rendererParams.sampleFormat = SAMPLE_S16LE;
90 rendererParams.sampleRate = SAMPLE_RATE_44100;
91 rendererParams.channelCount = STEREO;
92 rendererParams.encodingType = ENCODING_PCM;
93
94 audioRenderer->SetParams(rendererParams);
95 ```
96
974. (可选)使用 audioRenderer->**GetParams**(rendererParams) 来验证 SetParams。
985. AudioRenderer 实例调用 audioRenderer->**Start**() 函数来启动播放任务。
996. 使用 **GetBufferSize** 接口获取要写入的缓冲区长度。
100
101 ```
102 audioRenderer->GetBufferSize(bufferLen);
103 ```
104
1057. 从源(例如音频文件)读取要播放的音频数据并将其传输到字节流中。重复调用Write函数写入渲染数据。
106
107 ```
108 bytesToWrite = fread(buffer, 1, bufferLen, wavFile);
109 while ((bytesWritten < bytesToWrite) && ((bytesToWrite - bytesWritten) > minBytes)) {
110 bytesWritten += audioRenderer->Write(buffer + bytesWritten, bytesToWrite - bytesWritten);
111 if (bytesWritten < 0)
112 break;
113 }
114 ```
115
1168. 调用audioRenderer->**Drain**()来清空播放流。
1179. 调用audioRenderer->**Stop**()来停止输出。
11810. 播放任务完成后,调用AudioRenderer实例的audioRenderer->**Release**()函数来释放资源。
119
120以上提供了基本音频播放使用场景。
121
122
12311. 使用 audioRenderer->**SetVolume(float)** 和 audioRenderer->**GetVolume()** 来设置和获取当前音频流音量, 可选范围为 0.0 到 1.0。
124
125提供上述基本音频播放使用范例。更多接口说明请参考[**audio_renderer.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiorenderer/include/audio_renderer.h) 和 [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h)。
126
127### 音频录制<a name="section295162052813"></a>
128
129可以使用此仓库内提供的接口,让应用程序可以完成使用输入设备进行声音录制,将语音转换为音频数据,并管理录制的任务。以下步骤描述了如何使用 **AudioCapturer** 开发音频录制功能:
130
1311. 使用Create接口和所需流类型来获取 **AudioCapturer** 实例。
132
133 ```
134 AudioStreamType streamType = STREAM_MUSIC;
135 std::unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(streamType);
136 ```
137
1382. (可选)静态接口 **GetSupportedFormats**(), **GetSupportedChannels**(), **GetSupportedEncodingTypes**(), **GetSupportedSamplingRates**() 可用于获取支持的参数。
1393. 准备设备,调用实例的 **SetParams** 。
140
141 ```
142 AudioCapturerParams capturerParams;
143 capturerParams.sampleFormat = SAMPLE_S16LE;
144 capturerParams.sampleRate = SAMPLE_RATE_44100;
145 capturerParams.channelCount = STEREO;
146 capturerParams.encodingType = ENCODING_PCM;
147
148 audioCapturer->SetParams(capturerParams);
149 ```
150
1514. (可选)使用 audioCapturer->**GetParams**(capturerParams) 来验证 SetParams()。
1525. AudioCapturer 实例调用 AudioCapturer->**Start**() 函数来启动录音任务。
1536. 使用 **GetBufferSize** 接口获取要写入的缓冲区长度。
154
155 ```
156 audioCapturer->GetBufferSize(bufferLen);
157 ```
158
1597. 读取录制的音频数据并将其转换为字节流。重复调用read函数读取数据直到主动停止。
160
161 ```
162 // set isBlocking = true/false for blocking/non-blocking read
163 bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlocking);
164 while (numBuffersToCapture) {
165 bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
166 if (bytesRead <= 0) {
167 break;
168 } else if (bytesRead > 0) {
169 fwrite(buffer, size, bytesRead, recFile); // example shows writes the recorded data into a file
170 numBuffersToCapture--;
171 }
172 }
173 ```
174
1758. (可选)audioCapturer->**Flush**() 来清空录音流缓冲区。
1769. AudioCapturer 实例调用 audioCapturer->**Stop**() 函数停止录音。
17710. 录音任务完成后,调用 AudioCapturer 实例的 audioCapturer->**Release**() 函数释放资源。
178
179提供上述基本音频录制使用范例。更多API请参考[**audio_capturer.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiocapturer/include/audio_capturer.h)和[**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h)。
180
181### 音频管理<a name="section645572311287"></a>
182可以使用 [**audio_system_manager.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h) 内的接口来控制音量和设备。
1831. 使用 **GetInstance** 接口获取 **AudioSystemManager** 实例.
184 ```
185 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
186 ```
187#### 音量控制<a name="section645572311287_001"></a>
1882. 使用 **GetMaxVolume** 和 **GetMinVolume** 接口去查询音频流支持的最大和最小音量等级,在此范围内设置音量。
189 ```
190 AudioVolumeType streamType = AudioVolumeType::STREAM_MUSIC;
191 int32_t maxVol = audioSystemMgr->GetMaxVolume(streamType);
192 int32_t minVol = audioSystemMgr->GetMinVolume(streamType);
193 ```
1943. 使用 **SetVolume** 和 **GetVolume** 接口来设置和获取指定音频流的音量等级。
195 ```
196 int32_t result = audioSystemMgr->SetVolume(streamType, 10);
197 int32_t vol = audioSystemMgr->GetVolume(streamType);
198 ```
1994. 使用 **SetMute** 和 **IsStreamMute** 接口来设置和获取指定音频流的静音状态。
200 ```
201 int32_t result = audioSystemMgr->SetMute(streamType, true);
202 bool isMute = audioSystemMgr->IsStreamMute(streamType);
2035. 使用 **SetRingerMode** 和 **GetRingerMode** 接口来设置和获取铃声模式。参考在 [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h) 定义的 **AudioRingerMode** 枚举来获取支持的铃声模式。
204 ```
205 int32_t result = audioSystemMgr->SetRingerMode(RINGER_MODE_SILENT);
206 AudioRingerMode ringMode = audioSystemMgr->GetRingerMode();
207 ```
2086. 使用 **SetMicrophoneMute** 和 **IsMicrophoneMute** 接口来设置和获取麦克风的静音状态。
209 ```
210 int32_t result = audioSystemMgr->SetMicrophoneMute(true);
211 bool isMicMute = audioSystemMgr->IsMicrophoneMute();
212 ```
213#### 设备控制<a name="section645572311287_002"></a>
2147. 使用 **GetDevices**, **deviceType_** 和 **deviceRole_** 接口来获取音频输入输出设备信息。 参考 [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h) 内定义的DeviceFlag, DeviceType 和 DeviceRole 枚举。
215 ```
216 DeviceFlag deviceFlag = OUTPUT_DEVICES_FLAG;
217 vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors
218 = audioSystemMgr->GetDevices(deviceFlag);
219 sptr<AudioDeviceDescriptor> audioDeviceDescriptor = audioDeviceDescriptors[0];
220 cout << audioDeviceDescriptor->deviceType_;
221 cout << audioDeviceDescriptor->deviceRole_;
222 ```
2238. 使用 **SetDeviceActive** 和 **IsDeviceActive** 接口去激活/去激活音频设备和获取音频设备激活状态。
224 ```
225 ActiveDeviceType deviceType = SPEAKER;
226 int32_t result = audioSystemMgr->SetDeviceActive(deviceType, true);
227 bool isDevActive = audioSystemMgr->IsDeviceActive(deviceType);
228 ```
2299. 提供其他用途的接口如 **IsStreamActive**, **SetAudioParameter** and **GetAudioParameter**, 详细请参考 [**audio_system_manager.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h)
23010. 应用程序可以使用 **AudioManagerNapi::On**注册系统音量的更改。 在此,如果应用程序监听到系统音量更改的事件,就会用以下参数通知应用程序:
231volumeType : 更改的系统音量的类型
232volume : 当前的音量等级
233updateUi : 是否需要显示变化详细信息。(如果音量被增大/减小,将updateUi标志设置为true,在其他情况下,updateUi设置为false)。
234 ```
235 const audioManager = audio.getAudioManager();
236
237 export default {
238 onCreate() {
239 audioManager.on('volumeChange', (volumeChange) ==> {
240 console.info('volumeType = '+volumeChange.volumeType);
241 console.info('volume = '+volumeChange.volume);
242 console.info('updateUi = '+volumeChange.updateUi);
243 }
244 }
245 }
246 ```
247
248#### 音频场景<a name="section645572311287_003"></a>
24911. 使用 **SetAudioScene** 和 **getAudioScene** 接口去更改和检查音频策略。
250 ```
251 int32_t result = audioSystemMgr->SetAudioScene(AUDIO_SCENE_PHONE_CALL);
252 AudioScene audioScene = audioSystemMgr->GetAudioScene();
253 ```
254有关支持的音频场景,请参阅 **AudioScene** 中的枚举[**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h)。
255#### 音频流管理<a name="section645572311287_004"></a>
256可以使用[**audio_stream_manager.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiomanager/include/audio_stream_manager.h)提供的接口用于流管理功能。
2571. 使用 **GetInstance** 接口获得 **AudioSystemManager** 实例。
258 ```
259 AudioStreamManager *audioStreamMgr = AudioStreamManager::GetInstance();
260 ```
261
2622. 使用 **RegisterAudioRendererEventListener** 为渲染器状态更改注册侦听器。渲染器状态更改回调,该回调将在渲染器流状态更改时调用, 通过重写 **AudioRendererStateChangeCallback** 类中的函数 **OnRendererStateChange** 。
263 ```
264 const int32_t clientUID;
265
266 class RendererStateChangeCallback : public AudioRendererStateChangeCallback {
267 public:
268 RendererStateChangeCallback = default;
269 ~RendererStateChangeCallback = default;
270 void OnRendererStateChange(
271 const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos) override
272 {
273 cout<<"OnRendererStateChange entered"<<endl;
274 }
275 };
276
277 std::shared_ptr<AudioRendererStateChangeCallback> callback = std::make_shared<RendererStateChangeCallback>();
278 int32_t state = audioStreamMgr->RegisterAudioRendererEventListener(clientUID, callback);
279 int32_t result = audioStreamMgr->UnregisterAudioRendererEventListener(clientUID);
280 ```
281
2823. 使用 **RegisterAudioCapturerEventListener** 为捕获器状态更改注册侦听器。 捕获器状态更改回调,该回调将在捕获器流状态更改时调用, 通过重写 **AudioCapturerStateChangeCallback** 类中的函数 **OnCapturerStateChange** 。
283 ```
284 const int32_t clientUID;
285
286 class CapturerStateChangeCallback : public AudioCapturerStateChangeCallback {
287 public:
288 CapturerStateChangeCallback = default;
289 ~CapturerStateChangeCallback = default;
290 void OnCapturerStateChange(
291 const std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos) override
292 {
293 cout<<"OnCapturerStateChange entered"<<endl;
294 }
295 };
296
297 std::shared_ptr<AudioCapturerStateChangeCallback> callback = std::make_shared<CapturerStateChangeCallback>();
298 int32_t state = audioStreamMgr->RegisterAudioCapturerEventListener(clientUID, callback);
299 int32_t result = audioStreamMgr->UnregisterAudioCapturerEventListener(clientUID);
300 ```
3014. 使用 **GetCurrentRendererChangeInfos** 获取所有当前正在运行的流渲染器信息,包括clientuid、sessionid、renderinfo、renderstate和输出设备详细信息。
302 ```
303 std::vector<std::unique_ptr<AudioRendererChangeInfo>> audioRendererChangeInfos;
304 int32_t currentRendererChangeInfo = audioStreamMgr->GetCurrentRendererChangeInfos(audioRendererChangeInfos);
305 ```
306
3075. 使用 **GetCurrentCapturerChangeInfos** 获取所有当前正在运行的流捕获器信息,包括clientuid、sessionid、capturerInfo、capturerState和输入设备详细信息。
308 ```
309 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> audioCapturerChangeInfos;
310 int32_t currentCapturerChangeInfo = audioStreamMgr->GetCurrentCapturerChangeInfos(audioCapturerChangeInfos);
311 ```
312 有关结构,请参阅[**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h) **audioRendererChangeInfos** 和 **audioCapturerChangeInfos**.
313
3146. 使用 **IsAudioRendererLowLatencySupported** 检查低延迟功能是否支持。
315 ```
316 const AudioStreamInfo &audioStreamInfo;
317 bool isLatencySupport = audioStreamMgr->IsAudioRendererLowLatencySupported(audioStreamInfo);
318 ```
319#### JavaScript 用法:<a name="section645572311287_005"></a>
320JavaScript应用可以使用系统提供的音频管理接口,来控制音量和设备。\
321请参考 [**js-apis-audio.md**](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-audio.md#audiomanager) 来获取音量和设备管理相关JavaScript接口的用法。
322
323### 铃声管理<a name="section645572311287_006"></a>
324可以使用提供的接口[**iringtone_sound_manager.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audioringtone/include/iringtone_sound_manager.h) 和 [**iringtone_player.h**](https://gitee.com/openharmony/multimedia_audio_framework/blob/master/interfaces/inner_api/native/audioringtone/include/iringtone_player.h)实现铃声播放功能。
3251. 使用 **CreateRingtoneManager** 接口创建 **IRingtoneSoundManager** 实例。
326 ```
327 std::shared_ptr<IRingtoneSoundManager> ringtoneManagerClient = RingtoneFactory::CreateRingtoneManager();
328 ```
3292. 使用 **SetSystemRingtoneUri** 接口设置系统铃声Uri
330 ```
331 std::string uri = "/data/media/test.wav";
332 RingtoneType ringtoneType = RINGTONE_TYPE_DEFAULT;
333 ringtoneManagerClient->SetSystemRingtoneUri(context, uri, ringtoneType);
334 ```
3353. 使用 **GetRingtonePlayer** 接口获取 **IRingtonePlayer** 实例。
336 ```
337 std::unique_ptr<IRingtonePlayer> ringtonePlayer = ringtoneManagerClient->GetRingtonePlayer(context, ringtoneType);
338 ```
3394. 使用 **Configure** 接口配置铃声播放器。
340 ```
341 float volume = 1;
342 bool loop = true;
343 ringtonePlayer.Configure(volume, loop);
344 ```
3455. 使用 **Start**, **Stop**, 和 **Release** 接口在铃声播放器实例上控制播放状态。
346 ```
347 ringtonePlayer.Start();
348 ringtonePlayer.Stop();
349 ringtonePlayer.Release();
350 ```
3516. 使用 **GetTitle** 接口获取当前系统铃声的标题。
3527. 使用 **GetRingtoneState** 接口获取铃声播放状态 - **RingtoneState**
3538. 使用 **GetAudioRendererInfo** 获取 **AudioRendererInfo** 检查内容类型和流使用情况。
354### 蓝牙SCO呼叫<a name="section645572311287_007"></a>
355可以使用提供的接口 [**audio_bluetooth_manager.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/services/include/audio_bluetooth/client/audio_bluetooth_manager.h) 实现同步连接导向链路(SCO)的蓝牙呼叫。
356
3571. 为监听SCO状态更改,您可以使用 **OnScoStateChanged**.
358```
359const BluetoothRemoteDevice &device;
360int state;
361void OnScoStateChanged(const BluetoothRemoteDevice &device, int state);
362```
363
3642. (可选) 静态接口 **RegisterBluetoothScoAgListener**(), **UnregisterBluetoothScoAgListener**(), 可用于注册蓝牙SCO的侦听器。
365## 支持设备<a name="section645572311287_008"></a>
366以下是音频子系统支持的设备类型列表。
367
3681. **USB Type-C Headset**\
369 数字耳机,包括自己的DAC(数模转换器)和作为耳机一部分的放大器。
3702. **WIRED Headset**\
371 模拟耳机内部不包含任何DAC。它可以有3.5mm插孔或不带DAC的C型插孔。
3723. **Bluetooth Headset**\
373 蓝牙A2DP(高级音频分配模式)耳机,用于无线传输音频。
3744. **Internal Speaker and MIC**\
375 支持内置扬声器和麦克风,并将分别用作播放和录制的默认设备。
376
377## 相关仓<a name="section340mcpsimp"></a>
378
379[multimedia\_audio\_framework](https://gitee.com/openharmony/multimedia_audio_framework)
380