You need to enable JavaScript to run this app.
导航
【OC】实时音量均衡
最近更新时间:2023.03.16 11:40:15首次发布时间:2023.03.16 11:40:15
实现

1. 创建handle

SAMICore_CreateParameter *createParameter = [[SAMICore_CreateParameter alloc] init];
createParameter.sampleRate = sample_rate_;
createParameter.maxBlockSize = block_size_;
createParameter.numChannel = 2;
int ret = 0;
SAMICore *handle = [[SAMICore alloc] initWithIdentify:SAMICore_Identify_LoudNorm2 param:createParameter result:&ret];
if(ret != SAMI_OK) return NO;

parameter.maxBlockSize:表示每一次处理单个通道的最大sample数

2. 设置参数

通过 SAMICoreSetProperty 设置target_lufs 参数。

  • target_lufs(默认值-16dB)(-24到-12,建议实验值为-12,-16,-20)
SAMICore_LoudNorm2Property *Loudnorm_property = [[SAMICore_LoudNorm2Property alloc] init];
Loudnorm_property.target_lufs = -16;
SAMICore_Property *core_property = [[SAMICore_Property alloc] init];
core_property.id = SAMICore_PropertyId_Processor_Loudnorm2;
core_property.data = Loudnorm_property;
core_property.writable = 0;
core_property.type = SAMICore_DataType_LoudNorm2;
SAMICore_PropertyId property_id = SAMICore_PropertyId_Processor_Loudnorm2;
ret = [handle_ setProperty:core_property withId:property_id];
if(ret != SAMI_OK) return NO;

3. 初始化buffer

SAMICore_AudioBuffer *in_audio_buffer
        = [[SAMICore_AudioBuffer alloc] initWithNumberSamples:pre_define_block_size
                                               numberChannels:num_channels
                                                 isInterleave:false];

SAMICore_AudioBuffer *out_audio_buffer
        = [[SAMICore_AudioBuffer alloc] initWithNumberSamples:pre_define_block_size
                                               numberChannels:num_channels
                                                 isInterleave:false];

SAMICore_AudioBlock *in_block = [[SAMICore_AudioBlock alloc] init];
in_block.dataType = SAMICore_DataType_AudioBuffer;
in_block.numberAudioData = 1;
in_block.audioData = in_audio_buffer;

SAMICore_AudioBlock *out_block = [[SAMICore_AudioBlock alloc] init];
out_block.dataType = SAMICore_DataType_AudioBuffer;
out_block.numberAudioData = 1;
out_block.audioData = out_audio_buffer;

numberChannels : 需要跟创建handle设置的一致
numberSamples:每一个处理的实际帧数,要求 numberSamples<= parameter.maxBlockSize

4.处理数据

for(int inx = 0; inx + pre_define_block_size < (int)num_frames;) {
    // build in_block
    for(auto c = 0; c < num_channels; ++c) {
        std::copy_n(in_samples[c].data() + inx, pre_define_block_size, ((float**)in_audio_buffer.data)[c]);
    }

    result = [sami_core_handle processWithInBlock:in_block outBlock:out_block];

    if(result != SAMI_OK) {
        std::cerr << "process error: " << result;
        exit(-1);
    }

    // write output block to file
    audio_encoder->writePlanarData((float**)out_audio_buffer.data, num_channels, pre_define_block_size);
    inx += pre_define_block_size;
}

注意检查 SAMICoreProcess 函数的返回值,处理输入不正确或者内部处理错误都会有返回值,错误码可以查看错误检查一节

5.释放handle

sami_core_handle = nil;