SAMICoreExecutorContextCreateParameter
参数介绍
参数 | 类型 | 说明 |
---|---|---|
sampleRate | int | 入参,指音频的采样率 |
maxBlockSize | int | 入参,每次输入音频的最大的大小,算法需要根据此字段提前分配内存等,接下来每次送入process的大小不能超过该值 |
numChannel | int | 入参,音频的通道数 |
modelBuffer | String | 入参,模型的内容,模型详见回声消除介绍小节 |
modelLen | int | 入参,模型的内容的长度 |
bussinessInfo | String | 入参, 表示调用的业务方信息 |
numAudioBuffer | int | 入参, 回声消除v3为2 |
configInfo | String | 入参, 见下文configInfo |
configInfo
参数 | 类型 | 说明 |
---|---|---|
utility | string | 入参,固定设置为CommonAecUtility |
enable_stereo | bool | 入参,默认值:false; |
enable_pre_delay | bool | 入参,默认值:false; |
enable_pre_process | bool | 入参,默认值:false; true时候打开防爆音前处理 |
enable_after_process | bool | 入参,默认值:false; true时候打开单双讲检测后处理,在单讲场景提供更好的效果 |
after_process_param | json-map | 入参,默认值:见下文 单双讲检测后处理的配置参数 |
参数 | 含义 | 范围 |
---|---|---|
dtd_threshold | 控制单双讲检测的阈值 | 0<=x<=1,默认取0.2 |
st_ramp | 控制检测到单讲后gain的衰退速率 | 0<=x<=1,默认取0.2 |
dt_ramp | 控制检测到双讲后gain的增益速率 | 0<=x<=1,默认取0.2 |
举例:
SAMICoreExecutorContextCreateParameter parameter = new SAMICoreExecutorContextCreateParameter(); parameter.sampleRate = sampleRate; parameter.numChannel = numChannel; parameter.maxBlockSize = maxBlockSize; parameter.modelBuffer = FunctionHelper.readBinaryFile(modelFileName); parameter.modelLen = parameter.modelBuffer.length; parameter.bussinessInfo= "java demo"; parameter.numberAudioData = 2; parameter.configInfo="{\"utility\":\"CommonAecUtility\", \"enable_stereo\":true, \"enable_pre_delay\":true, \"enable_pre_process\":true, \"enable_after_process\":true, \"after_process_param\": {\"dtd_threshold\":0.2, \"st_ramp\":0.2, \"dt_ramp\":0.2}}"; int ret = samiCore.SAMICoreCreateHandleByIdentify( SAMICoreIdentify.SAMICoreIdentify_EngineExecutor_CE_AEC, parameter); if (ret != SAMICoreCode.SAMI_OK) { System.out.println("Create AecV3 handle failed, ret " + ret); return ret; }
SAMICoreBlock inBlock = new SAMICoreBlock(); SAMICoreBlock outBlock = new SAMICoreBlock(); inBlock.dataType = SAMICoreDataType.SAMICoreDataType_AudioBuffer; SAMICoreAudioBuffer micAudioBuffer = new SAMICoreAudioBuffer(); micAudioBuffer.numberChannels = numChannel; micAudioBuffer.numberSamples = maxBlockSize; SAMICoreAudioBuffer refAudioBuffer = new SAMICoreAudioBuffer(); refAudioBuffer.numberChannels = numChannel; refAudioBuffer.numberSamples = maxBlockSize; inBlock.audioData = new SAMICoreAudioBuffer[2]; inBlock.audioData[0] = micAudioBuffer; inBlock.audioData[1] = refAudioBuffer; outBlock.dataType = SAMICoreDataType.SAMICoreDataType_AudioBuffer; SAMICoreAudioBuffer outAudioBuffer = new SAMICoreAudioBuffer(); outAudioBuffer.numberChannels = numChannel; outAudioBuffer.numberSamples = maxBlockSize; outBlock.audioData = new SAMICoreAudioBuffer[1]; outBlock.audioData[0] = outAudioBuffer;
numberChannels : 需要跟创建handle设置的一致
numberSamples:每一个处理的实际帧数,要求 numberSamples<= parameter.maxBlockSize
for (int numSamples = 0; numSamples < totalNumSamples;) { //更新实际输入输出的帧数 if((numSamples + inAudioBuffer.numberSamples) > totalNumSamples) { micAudioBuffer.numberSamples = totalNumSamples - numSamples; refAudioBuffer.numberSamples = totalNumSamples - numSamples; outAudioBuffer.numberSamples = totalNumSamples - numSamples; } //拷贝处理数据到要处理的buffer for (int i = 0; i < inAudioBuffer.numberChannels; i++) { for (int j = 0; j < inAudioBuffer.numberSamples; j++) { micAudioBuffer.data[i][j] = in_mic_planar_float_data[i][numSamples + j]; refAudioBuffer.data[i][j] = in_ref_planar_float_data[i][numSamples + j]; } } numSamples = numSamples + micAudioBuffer.numberSamples; //调用处理函数 int ret = samiCore.SAMICoreProcess(inBlock, outBlock); if (ret != SAMICoreCode.SAMI_OK && ret != SAMICoreCode.SAMI_ENGINE_INPUT_NEED_MORE_DAT) { System.out.println("AecV3 process audio data failed, ret " + ret); return ret; } if(outAudioBuffer.numberSamples>0){ //从outAudioBuffer获取处理后的数据 for (int i = 0; i < inAudioBuffer.numberChannels; i++) { for (int j = 0; j < inAudioBuffer.numberSamples; j++) { out_planar_float_data[i][j+numSamples] = outAudioBuffer.data[i][j]; } } } }
由于算法存在缓存部分数据,因此当音频已经全部送入算法后,需要调用这个接口,将所有缓存的数据取出
SAMICoreProperty flushProperty = new SAMICoreProperty(); flushProperty.id=SAMICorePropertyId.SAMICorePropertyID_Common_Flush; samiCore.SAMICoreGetPropertyById(SAMICorePropertyId.SAMICorePropertyID_Common_Flush,flushProperty); SAMICoreAudioBuffer[] outAudioBufferArray = (SAMICoreAudioBuffer[])flushProperty.dataObjectArray; int bufcnt = flushProperty.dataArrayLen; for(int i=0;i<outAudioBufferArray[0].numberChannels;i++){ for(int j=0;j<outAudioBufferArray[0].numberSamples;j++){ out_planar_float_data[i][j+outPutSampleCnt] = outAudioBufferArray[0].data[i][j]; } }
当处理完成后,后续有新数据需要处理,而又不想重复创建handle时,可以调用reset接口,之后就可以当做一个新实例环境来用
SAMICoreProperty resetProperty = new SAMICoreProperty(); resetProperty.id = SAMICorePropertyId.SAMICorePropertyID_Common_Reset; resetProperty.type = SAMICoreDataType.SAMICoreDataType_Null; int ret = samiCore.SAMICoreSetProperty(SAMICorePropertyId.SAMICorePropertyID_Common_Reset,samiCoreProperty); if (ret != SAMICoreCode.SAMI_OK) { System.out.println("SAMICoreSetProperty failed, ret " + ret); return ret; }
samiCore.SAMICoreDestroyHandle();