You need to enable JavaScript to run this app.
导航
【OC】降噪/去混响-V2
最近更新时间:2023.09.14 10:28:57首次发布时间:2023.03.16 11:40:12
使用步骤

1. 创建handle

算法模型详见降噪/去混响/去啸叫介绍小节

// create SAMICore
SAMICore_CreateParameter *create_param = [[SAMICore_CreateParameter alloc] init];
create_param.sampleRate = sample_rate;
create_param.maxBlockSize = pre_define_block_size;
create_param.modelBuffer = reinterpret_cast<char*>(modelBin.data());
create_param.modelLen = modelBin.size();
create_param.numChannel = 2;
int result = 0;
SAMICore *sami_core_handle = [[SAMICore alloc] initWithIdentify:SAMICore_Identify_DENOISE_V2 param:create_param result:&result];
if(result != SAMI_OK) {
    std::cerr << "create handler failed: " << result;
    exit(-1);
}

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

2. 设置参数

参数介绍
SAMICore_PropertyId_Denoise_V2_Rate降噪比率:可选范围 0.0 ~ 1.0,默认是1.0,降噪比例最大。

SAMICore_PropertyId_Denoise_V2_Mode

多声道独立降噪。该模式会对每个声道单独进行降噪处理。(denoise_mode 为 1)
多声道混合后再降噪。该模式会先mix所有声道音频,进行降噪处理,再将降噪结果拷贝到所有声道。(denoise_mode 为 0)

SAMICore_Property *core_property = [[SAMICore_Property alloc] init];
core_property.id = SAMICore_PropertyId_Denoise_V2_Rate;
core_property.data = &denoise_rate;
core_property.dataLen = sizeof(float);
core_property.type = SAMICore_DataType_Float;
SAMICore_PropertyId property_id = SAMICore_PropertyId_Denoise_V2;
result = [sami_core_handle setProperty:core_property withId:property_id];
if(result != SAMI_OK) {
    std::cerr << "set property failed: " << result;
    exit(-1);
}

SAMICore_Property *core_property = [[SAMICore_Property alloc] init];
core_property.id = SAMICore_PropertyId_Denoise_V2_Mode;
core_property.data = [[NSNumber alloc] initWithFloat:denoise_rate];
core_property.type = SAMICore_DataType_Float;
SAMICore_PropertyId property_id = SAMICore_PropertyId_Denoise_V2;
result = [sami_core_handle setProperty:core_property withId:property_id];
if(result != SAMI_OK) {
    std::cerr << "set property failed: " << result;
    exit(-1);
}

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;