1# Audio Monitoring 2 3Audio monitoring enables real-time transmission of audio to headphones, allowing users to hear themselves or other relevant sounds in real time. 4 5This feature is commonly used in karaoke applications, where the recorded vocals and background music are sent to the headphones in real time. This allows users to adjust their performance based on the feedback, enhancing their experience. 6 7## Prerequisites 8 9- You can use the playback and recording capabilities provided by OHAudio to implement audio monitoring. The audio data captured during recording is used as the input for playback. 10 11 For details, see [Using OHAudio for Audio Playback](using-ohaudio-for-playback.md) and [Using OHAudio for Audio Recording](using-ohaudio-for-recording.md). 12 13- Currently, audio monitoring is only supported through wired headphones, where audio is both captured and played back. 14 15## How to Develop 16 17### Creating an Audio Recording Builder 18 19Use the **OH_AudioStreamBuilder** function provided by OHAudio to create an audio recording builder, following the builder design pattern. Set [OH_AudioStream_Type](../../reference/apis-audio-kit/capi-native-audiostream-base-h.md#oh_audiostream_type) to **AUDIOSTREAM_TYPE_CAPTURER**. 20 21```cpp 22OH_AudioStreamBuilder* builder; 23OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_CAPTURER); 24``` 25 26### Creating an Audio Playback Builder 27 28Use the **OH_AudioStreamBuilder** function provided by **OHAudio** to create an audio playback builder, following the builder design pattern. Set [OH_AudioStream_Type](../../reference/apis-audio-kit/capi-native-audiostream-base-h.md#oh_audiostream_type) to **AUDIOSTREAM_TYPE_RENDERER**. 29 30```cpp 31OH_AudioStreamBuilder* builder; 32OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_RENDERER); 33``` 34 35### Setting the Low Latency Mode 36 37To achieve better audio monitoring, it is essential to maintain low latency from recording to playback. When the device supports low-latency channels, you should use the low latency mode for both recording and playback. 38 39When creating the audio recording builder, call [OH_AudioStreamBuilder_SetLatencyMode()](../../reference/apis-audio-kit/capi-native-audiostreambuilder-h.md#oh_audiostreambuilder_setlatencymode) to set the low latency mode, and apply it to both recording and playback as follows: 40 41```cpp 42OH_AudioStream_LatencyMode latencyMode = AUDIOSTREAM_LATENCY_MODE_FAST; 43OH_AudioStreamBuilder_SetLatencyMode(builder, latencyMode); 44``` 45 46To implement real-time audio monitoring, create a shared buffer to store the captured data and promptly retrieve data from this buffer to write to the audio playback builder. 47 48### Defining the Shared Buffer and Recording/Playback Functions 49 50```cpp 51// Create a shared buffer to store captured data and retrieve playback data in a timely manner. 52 53// Customize a function to read captured data. 54 int32_t MyOnReadData( 55 OH_AudioCapturer* capturer, 56 void* userData, 57 void* buffer, 58 int32_t length) 59 { 60 // Extract captured data with the specified length from the buffer and place the data in the shared buffer for the renderer to read. 61 return 0; 62 } 63 64 // Customize a function to write data. 65 int32_t MyOnWriteData( 66 OH_AudioRenderer* renderer, 67 void* userData, 68 void* buffer, 69 int32_t length) 70 { 71 // Read data from the shared buffer and write the data with the specified length into the buffer. 72 return 0; 73 } 74``` 75 76### Setting Audio Stream Parameters 77 78The following provides an example of setting parameters for the audio recording stream: 79 80```cpp 81// Set the audio sampling rate. 82OH_AudioStreamBuilder_SetSamplingRate(builder, 48000); 83// Set the number of audio channels. 84OH_AudioStreamBuilder_SetChannelCount(builder, 2); 85// Set the audio sampling format. 86OH_AudioStreamBuilder_SetSampleFormat(builder, AUDIOSTREAM_SAMPLE_S16LE); 87// Set the encoding type of the audio stream. 88OH_AudioStreamBuilder_SetEncodingType(builder, AUDIOSTREAM_ENCODING_TYPE_RAW); 89// Set the usage scenario of the audio renderer. 90OH_AudioStreamBuilder_SetRendererInfo(builder, AUDIOSTREAM_SOURCE_TYPE_MIC); 91``` 92 93For the playback stream, set the same parameters as the recording stream, except for the working scenario. 94 95Set the working scenario parameter as follows: 96 97```cpp 98OH_AudioStreamBuilder_SetRendererInfo(builder, AUDIOSTREAM_USAGE_MUSIC); 99``` 100 101### Setting Recording Callback Functions 102 103```cpp 104// Customize a function to read data. 105int32_t MyOnReadData( 106 OH_AudioCapturer* capturer, 107 void* userData, 108 void* buffer, 109 int32_t length) 110{ 111 // Obtain captured data of the specified length from the buffer. 112 return 0; 113} 114// Customize an audio stream event function. 115int32_t MyOnStreamEvent( 116 OH_AudioCapturer* capturer, 117 void* userData, 118 OH_AudioStream_Event event) 119{ 120 // Update the capturer status and UI based on the audio stream event information indicated by the event. 121 return 0; 122} 123// Customize an audio interruption event function. 124int32_t MyOnInterruptEvent( 125 OH_AudioCapturer* capturer, 126 void* userData, 127 OH_AudioInterrupt_ForceType type, 128 OH_AudioInterrupt_Hint hint) 129{ 130 // Update the capturer status and UI based on the audio interruption information indicated by type and hint. 131 return 0; 132} 133// Customize an exception callback function. 134int32_t MyOnError( 135 OH_AudioCapturer* capturer, 136 void* userData, 137 OH_AudioStream_Result error) 138{ 139 // Perform operations based on the audio exception information indicated by error. 140 return 0; 141} 142 143OH_AudioCapturer_Callbacks callbacks; 144 145// Set the callbacks. 146callbacks.OH_AudioCapturer_OnReadData = MyOnReadData; 147callbacks.OH_AudioCapturer_OnStreamEvent = MyOnStreamEvent; 148callbacks.OH_AudioCapturer_OnInterruptEvent = MyOnInterruptEvent; 149callbacks.OH_AudioCapturer_OnError = MyOnError; 150 151// Set the callbacks for audio input streams. 152OH_AudioStreamBuilder_SetCapturerCallback(builder, callbacks, nullptr); 153``` 154 155### Setting Playback Callback Functions 156 157```cpp 158 // Customize a function to write data. 159 int32_t MyOnWriteData( 160 OH_AudioRenderer* renderer, 161 void* userData, 162 void* buffer, 163 int32_t length) 164 { 165 // Read data from the shared buffer and write the data with the specified length into the buffer. 166 return 0; 167 } 168 // Customize an audio stream event function. 169 int32_t MyOnStreamEvent( 170 OH_AudioRenderer* renderer, 171 void* userData, 172 OH_AudioStream_Event event) 173 { 174 // Update the player status and UI based on the audio stream event information indicated by the event. 175 return 0; 176 } 177 // Customize an audio interruption event function. 178 int32_t MyOnInterruptEvent( 179 OH_AudioRenderer* renderer, 180 void* userData, 181 OH_AudioInterrupt_ForceType type, 182 OH_AudioInterrupt_Hint hint) 183 { 184 // Update the player status and UI based on the audio interruption information indicated by type and hint. 185 return 0; 186 } 187 // Customize an exception callback function. 188 int32_t MyOnError( 189 OH_AudioRenderer* renderer, 190 void* userData, 191 OH_AudioStream_Result error) 192 { 193 // Perform operations based on the audio exception information indicated by error. 194 return 0; 195 } 196 197 OH_AudioRenderer_Callbacks callbacks; 198 199 // Set the callbacks. 200 callbacks.OH_AudioRenderer_OnWriteData = MyOnWriteData; 201 callbacks.OH_AudioRenderer_OnStreamEvent = MyOnStreamEvent; 202 callbacks.OH_AudioRenderer_OnInterruptEvent = MyOnInterruptEvent; 203 callbacks.OH_AudioRenderer_OnError = MyOnError; 204 205 // Set callbacks for the audio renderer. 206 OH_AudioStreamBuilder_SetRendererCallback(builder, callbacks, nullptr); 207 208``` 209 210### Creating an Audio Capturer 211 212```cpp 213OH_AudioCapturer* audioCapturer; 214OH_AudioStreamBuilder_GenerateCapturer(builder, &audioCapturer); 215``` 216 217### Creating an Audio Renderer 218 219```cpp 220OH_AudioRenderer* audioRenderer; 221OH_AudioStreamBuilder_GenerateRenderer(builder, &audioRenderer); 222``` 223 224### Using Audio Streams 225 226The following uses recording as an example. You can use the following APIs to control the start, pause, stop, and release of audio streams. 227 228> **NOTE** 229> 230> When implementing audio monitoring, you need to control both the recording and playback streams to ensure their synchronization. 231 232| API | Description | 233| ------------------------------------------------------------ | ------------ | 234| OH_AudioStream_Result [OH_AudioRenderer_Start](../../reference/apis-audio-kit/capi-native-audiorenderer-h.md#oh_audiorenderer_start)(OH_AudioRenderer* renderer) | Starts the audio renderer. | 235| OH_AudioStream_Result [OH_AudioRenderer_Pause](../../reference/apis-audio-kit/capi-native-audiorenderer-h.md#oh_audiorenderer_pause)(OH_AudioRenderer* renderer) | Pauses the audio renderer. | 236| OH_AudioStream_Result [OH_AudioRenderer_Stop](../../reference/apis-audio-kit/capi-native-audiorenderer-h.md#oh_audiorenderer_stop)(OH_AudioRenderer* renderer) | Stops the audio renderer. | 237| OH_AudioStream_Result [OH_AudioRenderer_Flush](../../reference/apis-audio-kit/capi-native-audiorenderer-h.md#oh_audiorenderer_flush)(OH_AudioRenderer* renderer) | Flushes obtained audio data.| 238| OH_AudioStream_Result [OH_AudioRenderer_Release](../../reference/apis-audio-kit/capi-native-audiorenderer-h.md#oh_audiorenderer_release)(OH_AudioRenderer* renderer) | Releases the audio renderer.| 239 240### Releasing the Builder 241 242When the builder is no longer required, release the resources as follows: 243 244```cpp 245OH_AudioStreamBuilder_Destroy(builder); 246``` 247