Raw audio processing
In some scenarios, raw audio captured through the microphone must be processed to achieve the desired functionality or to enhance the user experience. Voice SDK enables you to pre-process and post-process the captured audio for implementation of custom playback effects.
This article shows you how to pre-process and post-process collected raw audio data.
Understand the tech
For scenarios that require self-processing of audio data, Agora Voice SDK provides raw data processing functionality. You can perform pre-processing to modify the captured audio signal before sending the data to the encoder, or post-process data to modify the received audio signal after sending the data to the decoder.
To implement processing of raw audio data in your app, take the following steps.
- Register an instance of the audio frame observer before joining a channel.
- Set the format of audio frames captured by each callback.
- Implement callbacks in the frame observers to process raw audio data.
- Unregister the frame observers before you leave a channel.
The following figure shows the basic processing of raw audio data:
Prerequisites
Ensure that you have implemented the SDK quickstart in your project.
Implement raw audio processing
Follow these steps to implement raw audio data processing functionality in your app:
- Before joining a channel, create an
IAudioFrameObserver
instance and call theregisterAudioFrameObserver
method to register the audio observer. - Call
setRecordingAudioFrameParameters
,setPlaybackAudioFrameParameters
, andsetMixedAudioFrameParameters
to configure the audio frame format. - Implement
onRecordAudioFrame
,onPlaybackAudioFrame
,onPlaybackAudioFrameBeforeMixing
, andonMixedAudioFrame
callbacks. These callbacks receive and process audio frames. If the return value of these callbacks isfalse
, it indicates that the processing of the audio frames is invalid.
Refer to the following sample code to implement this logic:
Voice SDK uses a synchronous callback mechanism for processing raw audio data. When you save or rewrite data using the callbacks, consider the following best practices:
-
To ensure continuity of the audio stream, do not block the SDK thread by processing data directly in the callback function. Instead, make a deep copy of the received audio data and transfer the copied data to another thread for processing.
-
If you choose to process the audio data synchronously within the callback function, you must strictly control the processing time. For example, if the callback function is triggered every 10 milliseconds, then the processing time within the callback must be less than 10 milliseconds to prevent delays or interruptions in the audio stream.
Reference
This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.
Sample project
Agora provides an open-source example project ProcessAudioRawData for your reference. Download or view the project for a more detailed example.