Raw audio processing
Updated
Pre and post-process captured video and audio data to achieve the desired playback effect.
In some use-cases, raw audio captured through the microphone must be processed to enhance the user experience or achieve the desired functionality 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 use-cases 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:
Process raw audio
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, call
setAudioFrameDelegateto set the audio frame delegate. - Call
setRecordingAudioFrameParameters,setPlaybackAudioFrameParameters, andsetMixedAudioFrameParametersto configure the audio frame format. - Implement
onRecordAudioFrame,onPlaybackAudioFrame,onPlaybackAudioFrameBeforeMixing, andonMixedAudioFramecallbacks. 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:
class RawAudioDataMain: BaseViewController {
var localVideo = Bundle.loadVideoView(type: .local, audioOnly: true)
var remoteVideo = Bundle.loadVideoView(type: .remote, audioOnly: true)
@IBOutlet weak var container: AGEVideoContainer!
// Define agoraKit variable
var agoraKit: AgoraRtcEngineKit!
// ...
// Initialize agoraKit and register the corresponding callbacks
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
// Set the audio frame delegate.
// You need to implement the AgoraAudioFrameDelegate protocol in this method
agoraKit.setAudioFrameDelegate(self)
// Configure the audio frames captured by each callback
agoraKit.setRecordingAudioFrameParametersWithSampleRate(44100, channel: 1, mode: .readWrite, samplesPerCall: 4410)
agoraKit.setMixedAudioFrameParametersWithSampleRate(44100, samplesPerCall: 4410)
agoraKit.setPlaybackAudioFrameParametersWithSampleRate(44100, channel: 1, mode: .readWrite, samplesPerCall: 4410)
// ...
// Under the current class, implement the extension of the AgoraAudioFrameDelegat protocol
extension RawAudioDataMain: AgoraAudioFrameDelegate {
// Implement the onRecordAudioFrame callback
func onRecordAudioFrame(_ frame: AgoraAudioFrame, channelId: String) -> Bool {
return true
}
// Implement the onPlaybackAudioFrame callback
func onPlaybackAudioFrame(_ frame: AgoraAudioFrame, channelId: String) -> Bool {
return true
}
// Implement the onMixedAudioFrame callback
func onMixedAudioFrame(_ frame: AgoraAudioFrame, channelId: String) -> Bool {
return true
}
// Implement the onPlaybackAudioFrameBeforeMixing callback
func onPlaybackAudioFrame(beforeMixing frame: AgoraAudioFrame, channelId: String, uid: UInt) -> Bool {
return true
}
}
}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 RawAudioData for your reference. Download or view the project for a more detailed example.
