logo
Video Call
On this page

Get the Raw Audio Data

2024-01-02

Introduction

The SDK provides developers with the function to obtain raw audio data. The obtained raw audio data format is PCM. Developers can write this data to local devices to implement audio recording.

Download Sample Source Code

Please refer to Download Sample Source Code to obtain the source code.

For related source code, please check the files in the "/ZegoExpressExample/Examples/AdvancedAudioProcessing/OriginalAudioDataAcquisition" directory.

Usage Steps

The usage flow for Get the Raw Audio Data is as follows:

1 Initialize the SDK

Please refer to "Create the engine" in Quick Start - Implementation Process

2 Enable the raw audio data acquisition function

Developers can call the startAudioDataObserver interface to enable raw audio data callback monitoring. The callback audio data types are ZegoAudioDataCallbackBitMask, including ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_CAPTURED, ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_PLAYBACK, ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_MIXED, and ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_PLAYER. All four callback types need to be enabled.

// Enable the raw audio data acquisition function
ZegoAudioFrameParam param;
param.channel = ZEGO_AUDIO_CHANNEL_STEREO;
param.sampleRate = ZEGO_AUDIO_SAMPLE_RATE_8K;
unsigned int bitmask = ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_CAPTURED | ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_PLAYBACK | ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_MIXED | ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_PLAYER;
engine->startAudioDataObserver(bitmask, param);
Note

If necessary, please contact ZEGOCLOUD Technical Support.

3 Set callback to receive raw audio data and process it

Developers can call the setAudioDataHandler interface to set additional callbacks for receiving audio data. As needed, developers can implement callbacks onCapturedAudioData, onPlaybackAudioData, onMixedAudioData, and onPlayerAudioData, corresponding to the four audio data types in ZegoAudioDataCallbackBitMask respectively.

// Set raw audio data callback
class MyAudioDataHandler: public IZegoAudioDataHandler
{
public:
    // Local captured audio data, callback can be received after publishing stream
    virtual void onCapturedAudioData(const unsigned char* /*data*/, unsigned int /*dataLength*/, ZegoAudioFrameParam /*param*/) {

    }

    // Audio data played by SDK, when the engine is started and not in the playing stream state and not using the media player to play media files, the callback audio data is silent audio data
    virtual void onPlaybackAudioData(const unsigned char* /*data*/, unsigned int /*dataLength*/, ZegoAudioFrameParam /*param*/) {

    }

    // Callback for audio data mixed from local capture and SDK playback
    virtual void onMixedAudioData(const unsigned char* /*data*/, unsigned int /*dataLength*/, ZegoAudioFrameParam /*param*/) {

    }

    // Remote stream playing audio data, callback for each stream playing data after starting stream playing
    virtual void onPlayerAudioData(const unsigned char* /*data*/, unsigned int /*dataLength*/, ZegoAudioFrameParam /*param*/, const std::string& /*streamID*/) {

    }
};

engine->setAudioDataHandler(std::make_shared<MyAudioDataHandler>());

4 Stop audio data callback monitoring

If you want to stop audio data callback monitoring, you can call the stopAudioDataObserver interface.

engine->stopAudioDataObserver();

Previous

Custom Audio Processing

Next

AI Voice Changer

On this page

Back to top