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
IAudioFrameObserver
object and then callregisterAudioFrameObserver
to register an audio frame observer. - After you successfully register the audio frame observer, the SDK triggers the
getRecordAudioParams
,getPlaybackAudioParams
, orgetMixedAudioParams
callbacks. You can set the desired audio data format in the return values of these callbacks. - After you join the channel, the SDK triggers the
getObservedAudioFramePosition
andisMultipleChannelFrameWanted
callbacks 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
getObservedAudioFramePosition
andisMultipleChannelFrameWanted
, the SDK triggers theonRecordFrame
,onPlaybackFrame
,onPlaybackFrameBeforeMixing
/onPlaybackFrameBeforeMixingEx
, oronMixedFrame
callbacks 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
/onPlaybackFrameBeforeMixingEx
oronMixedFrame
callbacks 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
registerAudioFrameObserver
onRecordFrame
onPlaybackFrame
onPlaybackFrameBeforeMixing
onMixedFrame
isMultipleChannelFrameWanted
onPlaybackFrameBeforeMixingEx
getObservedAudioFramePosition
getRecordAudioParams
getPlaybackAudioParams
getMixedAudioParams
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 RTC Java SDK encapsulates the RTC C++ SDK, 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
registerAudioFrameObserver
method to register an audio observer, and implement anIAudioFrameObserver
class in this method. - After you successfully register the observer, the SDK sends the captured raw audio data via the
onRecordAudioFrame
,onPlaybackAudioFrame
,onPlaybackAudioFrameBeforeMixing
, oronMixedAudioFrame
callbacks. - 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
.so
library built from the C++ interface file (.cpp
file) via the Java interface file. - The Java interface file generates a
.h
file with thejavac -h -jni
command. The C++ interface file should include this file. - The C++ interface file calls the C++ method of the
.so
library 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.java
file in the sample project for the implementation.
- Run the following command to generate a
.h
file 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
.h
file. Refer to theio_agora_advancedvideo_rawdata_MediaPreProcessing.cpp
file in the sample project for the implementation.
- Build the C++ interface file via the NDK to generate a
.so
library. Use theSystem.loadLibrary()
method to load the generated.so
library 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
setCallback
method. ThesetCallback
method calls theregisterAudioFrameObserver
C++ method via JNI to register an audio frame observer.
- Implement the
onRecordAudioFrame
,onPlaybackAudioFrame
,onPlaybackAudioFrameBeforeMixing
, andonMixedAudioFrame
callbacks. Get the audio frames from the callbacks, and process the audio frames.