Raw Audio Data
Introduction
During the audio transmission process, you can pre- and post-process the captured audio data to achieve the desired playback effect.
Agora provides the raw data function for you to process the audio data according to your scenarios. This function enables you to pre-process the captured audio signal before sending it to the encoder, or to post-process the decoded audio signal.
Sample project
Agora provides an open-source sample project that implements processing raw audio data using Java APIs on GitHub. You can try the demo and view the source code.
Implementation
Before using the raw data functions, ensure that you have implemented the basic real-time audio functions in your project.
Process raw audio data using Java APIs
To call Java APIs in your project to implement the raw audio data functions, do the following:
- Before joining a channel, create an
IAudioFrameObserverobject and then callregisterAudioFrameObserverto register an audio frame observer. - After you successfully register the audio frame observer, the SDK triggers the
getRecordAudioParams,getPlaybackAudioParams, orgetMixedAudioParamscallbacks. You can set the desired audio data format in the return values of these callbacks. - After you join the channel, the SDK triggers the
getObservedAudioFramePositionandisMultipleChannelFrameWantedcallbacks when capturing each audio frame. In the return values of these callbacks, you can set the audio observation positions and whether to receive raw audio data from multiple channels. - According to the return values of
getObservedAudioFramePositionandisMultipleChannelFrameWanted, the SDK triggers theonRecordFrame,onPlaybackFrame,onPlaybackFrameBeforeMixing/onPlaybackFrameBeforeMixingEx, oronMixedFramecallbacks to send you the captured raw audio data. - Process the captured audio data according to your scenarios. You can send the processed audio data with the
onRecordFrame,onPlaybackFrame,onPlaybackFrameBeforeMixing/onPlaybackFrameBeforeMixingExoronMixedFramecallbacks according to your scenarios.
API call sequence
The following diagram shows how to implement the raw audio data function in your project:
Sample code
API reference
registerAudioFrameObserveronRecordFrameonPlaybackFrameonPlaybackFrameBeforeMixingonMixedFrameisMultipleChannelFrameWantedonPlaybackFrameBeforeMixingExgetObservedAudioFramePositiongetRecordAudioParamsgetPlaybackAudioParamsgetMixedAudioParams
Process raw audio data using JNI and C++ APIs
Before using the raw data function, ensure that you have implemented the basic real-time audio function in your project.
IAudioFrameObserver class to capture and modify raw audio data. Therefore, you can use Java to call the C++ API via the JNI (Java Native Interface). Since the Video SDK for Java encapsulates the Video SDK for C++, you can include the .h file in the SDK to directly call the C++ methods.Follow these steps to implement the raw audio data function in your project:
- Use the JNI and C++ interface files to generate a shared library in the project, and use Java to call the raw audio data interface of the Agora C++ SDK.
- Before joining a channel, call the
registerAudioFrameObservermethod to register an audio observer, and implement anIAudioFrameObserverclass in this method. - After you successfully register the observer, the SDK sends the captured raw audio data via the
onRecordAudioFrame,onPlaybackAudioFrame,onPlaybackAudioFrameBeforeMixing, oronMixedAudioFramecallbacks. - Process the captured raw audio data according to your needs. Then, you can either play it yourself directly or send it to the SDK via the callbacks mentioned in step 3 per your requirements.
Call the Agora C++ API in a Java project
The following diagram shows the basic flow of calling the Agora C++ API in a Java project:
- The Java project loads the
.solibrary built from the C++ interface file (.cppfile) via the Java interface file. - The Java interface file generates a
.hfile with thejavac -h -jnicommand. The C++ interface file should include this file. - The C++ interface file calls the C++ method of the
.solibrary in the Agora Android SDK by including the header files from the Agora Android SDK.
API call sequence
The following diagram shows how to implement the raw audio data function in your project:
registerAudioFrameObserver, onRecordAudioFrame, onPlaybackAudioFrame, onMixedAudioFrame, and onPlaybackAudioFrameBeforeMixing are all C++ methods and callbacks.Sample code
Create a JNI interface
Create a Java interface file and a C++ interface file separately via the JNI interface. Make sure to build the C++ interface file as a .so library.
- Create a Java interface file to call the C++ API. The interface file should declare the relevant Java methods for calling C++. Refer to the
MediaPreProcessing.javafile in the sample project for the implementation.
- Run the following command to generate a
.hfile from the Java interface file:
- Create a C++ interface file. The C++ interface file exports the corresponding methods from the C++ SDK based on the generated
.hfile. Refer to theio_agora_advancedvideo_rawdata_MediaPreProcessing.cppfile in the sample project for the implementation.
- Build the C++ interface file via the NDK to generate a
.solibrary. Use theSystem.loadLibrary()method to load the generated.solibrary in the Java interface file. See the following CMake file.
Implement the raw audio data function in a Java project
- Implement an interface that maps to the C++ methods in a Java interface file.
- Call the
setCallbackmethod. ThesetCallbackmethod calls theregisterAudioFrameObserverC++ method via JNI to register an audio frame observer.
- Implement the
onRecordAudioFrame,onPlaybackAudioFrame,onPlaybackAudioFrameBeforeMixing, andonMixedAudioFramecallbacks. Get the audio frames from the callbacks, and process the audio frames.