Starting from API version 22, OHAudioSuite provides the real-time preview capability for audio authoring (API version 22 supports only the equalizer effect, while API version 23 and later support other effects). For example, you can use the preset sound effects in the equalizer to change the style of music.
To use the real-time preview capability provided by OHAudioSuite, include the corresponding header files.
target_link_libraries(entry PUBLIC libohaudio.so) target_link_libraries(entry PUBLIC libohaudiosuite.so)
To use APIs related to audio creation and playback, include the <native_audio_suite_base.h>, <native_audio_suite_engine.h>, <native_audiostreambuilder.h>, and <native_audiorenderer.h> header files.
#include <ohaudiosuite/native_audio_suite_base.h> #include <ohaudiosuite/native_audio_suite_engine.h> #include <ohaudio/native_audiorenderer.h> #include <ohaudio/native_audiostreambuilder.h>
Figure 1 Real-time preview diagram

You can implement a simple real-time preview feature for an equalizer effect node by following the steps below. This section uses the equalizer effect as an example to demonstrate the real-time preview process. For details about other effect nodes, see Audio Effects (C/C++).
Initialize and create an OHAudioSuite pipeline (including the input node, equalizer node, and output node).
struct AudioDataInfo {
uint8_t *buffer = nullptr; // Audio data.
int32_t bufferSize = 0; // Total size of the audio data.
int32_t totalWriteSize = 0; // Total size of the processed audio data.
int32_t totalReadSize = 0; // Total size of the audio data that has been read.
}; // The sample API does not include return value verification. Add verification logic in actual use.
// Callback used by the input node to request data.
static int32_t InputNodeWriteDataCallBack(OH_AudioNode *audioNode, void *userData, void *audioData,
int32_t audioDataSize, bool *finished)
{
if ((audioNode == nullptr) || (userData == nullptr) || (audioData == nullptr) || (audioDataSize <= 0) ||
(finished == nullptr)) {
return -1;
}
struct AudioDataInfo *info = static_cast<struct AudioDataInfo *>(userData);
// Size of the audio data to be processed.
int32_t actualDataSize = std::min(audioDataSize, info->bufferSize - info->totalWriteSize);
// Write the PCM audio data to audioData.
if (actualDataSize > 0) {
std::copy(info->buffer + info->totalWriteSize, info->buffer + info->totalWriteSize + actualDataSize,
static_cast<uint8_t *>(audioData));
}
info->totalWriteSize += actualDataSize;
// All audio data has been processed.
if (info->totalWriteSize >= info->bufferSize) {
*finished = true;
}
return actualDataSize;
} // The sample API does not include return value verification. Add verification logic in actual use.
// Create the engine.
OH_AudioSuiteEngine_Create(&audioSuiteEngine);
// Create a pipeline for real-time preview.
OH_AudioSuiteEngine_CreatePipeline(audioSuiteEngine, &audioSuitePipeline,
OH_AudioSuite_PipelineWorkMode::AUDIOSUITE_PIPELINE_REALTIME_MODE);
// Create a node builder.
OH_AudioNodeBuilder *nodeBuilder = nullptr;
OH_AudioSuiteNodeBuilder_Create(&nodeBuilder);
OH_AudioSuiteNodeBuilder_SetNodeType(nodeBuilder, OH_AudioNode_Type::INPUT_NODE_TYPE_DEFAULT);
// Configure the audio data format. Set the sampling rate, channel layout, number of channels, bit depth, and encoding format based on the audio data to be processed.
OH_AudioFormat audioFormatInput;
audioFormatInput.samplingRate = OH_Audio_SampleRate::SAMPLE_RATE_48000;
audioFormatInput.channelLayout = OH_AudioChannelLayout::CH_LAYOUT_STEREO;
audioFormatInput.channelCount = CHANNEL_COUNT;
audioFormatInput.sampleFormat = OH_Audio_SampleFormat::AUDIO_SAMPLE_S16LE;
audioFormatInput.encodingType = OH_Audio_EncodingType::AUDIO_ENCODING_TYPE_RAW;
OH_AudioSuiteNodeBuilder_SetFormat(nodeBuilder, audioFormatInput);
// Set the audio stream callback.
void *userData = static_cast<void *>(audioInfo);
OH_AudioSuiteNodeBuilder_SetRequestDataCallback(nodeBuilder, InputNodeWriteDataCallBack, userData);
// Create the input node.
OH_AudioSuiteEngine_CreateNode(audioSuitePipeline, nodeBuilder, &inputNode);
// Reset the builder configuration and set the node type to equalizer.
OH_AudioSuiteNodeBuilder_Reset(nodeBuilder);
OH_AudioSuiteNodeBuilder_SetNodeType(nodeBuilder, OH_AudioNode_Type::EFFECT_NODE_TYPE_EQUALIZER);
// Create the equalizer node.
OH_AudioSuiteEngine_CreateNode(audioSuitePipeline, nodeBuilder, &eqNode);
// Set the equalizer node effect to default.
OH_AudioSuiteEngine_SetEqualizerFrequencyBandGains(eqNode, OH_EQUALIZER_PARAM_DEFAULT);
// Reset the builder configuration and set the node type to output.
OH_AudioSuiteNodeBuilder_Reset(nodeBuilder);
OH_AudioSuiteNodeBuilder_SetNodeType(nodeBuilder, OH_AudioNode_Type::OUTPUT_NODE_TYPE_DEFAULT);
// Configure the audio data format. Set the sampling rate, channel layout, number of channels, bit depth, and encoding format based on the expected output audio format.
OH_AudioFormat audioFormatOutput;
audioFormatOutput.samplingRate = OH_Audio_SampleRate::SAMPLE_RATE_48000;
audioFormatOutput.channelLayout = OH_AudioChannelLayout::CH_LAYOUT_STEREO;
audioFormatOutput.channelCount = CHANNEL_COUNT;
audioFormatOutput.sampleFormat = OH_Audio_SampleFormat::AUDIO_SAMPLE_S16LE;
audioFormatOutput.encodingType = OH_Audio_EncodingType::AUDIO_ENCODING_TYPE_RAW;
OH_AudioSuiteNodeBuilder_SetFormat(nodeBuilder, audioFormatOutput);
// Create the output node.
OH_AudioSuiteEngine_CreateNode(audioSuitePipeline, nodeBuilder, &outputNode);
// Destroy the node builder.
OH_AudioSuiteNodeBuilder_Destroy(nodeBuilder);
// Connect the nodes to form a network.
OH_AudioSuiteEngine_ConnectNodes(inputNode, eqNode);
OH_AudioSuiteEngine_ConnectNodes(eqNode, outputNode);
Offline editing and real-time preview differ in how the pipeline is created.
The processing duration of OH_AudioSuiteEngine_RenderFrame() depends on the number of effect nodes connected in the pipeline. Pay attention to the processing duration to avoid stuttering during real-time preview.
Refer to Using OHAudio for Audio Playback (C/C++) to complete the audio playback development.
In the player callback function, copy the processed data to the buffer of the OH_AudioRenderer instance to enable real-time preview during audio playback.
// The sample API does not include return value validation. Add validation logic in actual use.
static OH_AudioData_Callback_Result AudioRendererOnWriteData(OH_AudioRenderer *renderer, void *userData,
void *audioData, int32_t audioDataSize)
{
bool finishedFlag = false;
int32_t writeSize = 0;
OH_AudioSuite_Result result = OH_AudioSuiteEngine_RenderFrame(static_cast<OH_AudioSuitePipeline *>(userData),
audioData, audioDataSize, &writeSize, &finishedFlag);
if (result != OH_AudioSuite_Result::AUDIOSUITE_SUCCESS) {
// The audio creation rendering fails.
return AUDIO_DATA_CALLBACK_RESULT_INVALID;
}
// The audio creation rendering is complete.
if (finishedFlag) {
// Developer-defined behavior.
}
return AUDIO_DATA_CALLBACK_RESULT_VALID;
} // The sample API does not include return value verification. Add verification logic in actual use.
// Create the builder.
OH_AudioStreamBuilder_Create(&rendererBuilder, OH_AudioStream_Type::AUDIOSTREAM_TYPE_RENDERER);
OH_AudioStreamBuilder_SetSamplingRate(rendererBuilder, OH_Audio_SampleRate::SAMPLE_RATE_48000);
OH_AudioStreamBuilder_SetChannelCount(rendererBuilder, CHANNEL_COUNT);
OH_AudioStreamBuilder_SetSampleFormat(rendererBuilder, AUDIOSTREAM_SAMPLE_S16LE);
OH_AudioStreamBuilder_SetEncodingType(rendererBuilder, AUDIOSTREAM_ENCODING_TYPE_RAW);
OH_AudioStreamBuilder_SetRendererInfo(rendererBuilder, AUDIOSTREAM_USAGE_MUSIC);
// If the sampling rate is 11025, use 40 ms for calculation.
int32_t frameSize = RENDER_FRAME_DURATION_MS * audioFormatOutput.samplingRate * audioFormatOutput.channelCount *
SAMPLE_FORMAT_S16LE_BYTE_SIZE / MS_PER_SECOND;
// Set the audioDataSize length (size of the data to be played).
OH_AudioStreamBuilder_SetFrameSizeInCallback(rendererBuilder, frameSize);
// Configure the callback for writing audio data.
OH_AudioStreamBuilder_SetRendererWriteDataCallback(rendererBuilder, AudioRendererOnWriteData,
static_cast<void *>(audioSuitePipeline));
// Start the pipeline.
OH_AudioSuiteEngine_StartPipeline(audioSuitePipeline);
// You can create a renderer stream to play audio.
// ...
// ...
// Stop the pipeline.
OH_AudioSuiteEngine_StopPipeline(audioSuitePipeline); Destroy resources.
// The sample API does not include return value validation. Add validation logic in actual use. // Destroy the stream builder. OH_AudioStreamBuilder_Destroy(rendererBuilder); // Destroy the nodes. OH_AudioSuiteEngine_DestroyNode(inputNode); OH_AudioSuiteEngine_DestroyNode(eqNode); OH_AudioSuiteEngine_DestroyNode(outputNode); // Destroy the pipeline. OH_AudioSuiteEngine_DestroyPipeline(audioSuitePipeline); // Destroy the engine. OH_AudioSuiteEngine_Destroy(audioSuiteEngine);
During real-time audio preview, you cannot create new effect nodes. You can only modify the parameters of existing effect nodes.
For details about error codes, see OH_AudioSuite_Result.