samiCore = new SAMICore(); // 创建句柄 SAMICore3ACreateParameter parameter = new SAMICore3ACreateParameter(); parameter.sampleRate = sampleRate; parameter.numChannel = numChannel; parameter.maxBlockSize = max_block_size; int ret = samiCore.SAMICoreCreateHandleByIdentify( SAMICoreIdentify.SAMICore_Identify_LoudNorm2, parameter); if (ret != SAMICoreCode.SAMI_OK) { System.out.println("Create LoudNorm2 handle failed, ret " + ret); return ret; }
parameter.maxBlockSize:表示每一次处理单个通道的最大sample数
通过 SAMICoreSetProperty 设置target_lufs 参数。
SAMICoreProperty samiCoreProperty = new SAMICoreProperty(); samiCoreProperty.id = SAMICorePropertyId.SAMICorePropertyID_Processor_Loudnorm2; samiCoreProperty.type = SAMICoreDataType.SAMICoreDataType_LoudNorm2; SAMICoreLoudNorm2Property Loudnorm_property = new SAMICoreLoudNorm2Property(); Loudnorm_property.target_lufs = targetLufs; samiCoreProperty.dataObjectArray = new SAMICoreLoudNorm2Property[1]; samiCoreProperty.dataObjectArray[0] = Loudnorm_property; samiCoreProperty.dataArrayLen = 1; int ret = samiCore.SAMICoreSetProperty(SAMICorePropertyId.SAMICorePropertyID_Processor_Loudnorm2,samiCoreProperty); if (ret != SAMICoreCode.SAMI_OK) { System.out.println("SAMICoreSetProperty failed, ret " + ret); }
inBlock = new SAMICoreBlock(); outBlock = new SAMICoreBlock(); inBlock.dataType = SAMICoreDataType.SAMICoreDataType_AudioBuffer; inAudioBuffer = new SAMICoreAudioBuffer(); inAudioBuffer.numberChannels = numChannel; inAudioBuffer.numberSamples = max_block_size; inAudioBuffer.data = new float[(int) inAudioBuffer.numberChannels][(int) inAudioBuffer.numberSamples]; inBlock.audioData = new SAMICoreAudioBuffer[1]; inBlock.audioData[0] = inAudioBuffer; outBlock.dataType = SAMICoreDataType.SAMICoreDataType_AudioBuffer; outAudioBuffer = new SAMICoreAudioBuffer(); outAudioBuffer.numberChannels = numChannel; outAudioBuffer.numberSamples = max_block_size; outAudioBuffer.data = new float[(int) outAudioBuffer.numberChannels][(int) outAudioBuffer.numberSamples]; 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) { inAudioBuffer.numberSamples = totalNumSamples - numSamples; outAudioBuffer.numberSamples = totalNumSamples - numSamples; } //拷贝处理数据到要处理的buffer for (int i = 0; i < inAudioBuffer.numberChannels; i++) { for (int j = 0; j < inAudioBuffer.numberSamples; j++) { inAudioBuffer.data[i][j] = in_planar_float_data[i][numSamples + j]; } } //调用处理函数 int ret = samiCore.SAMICoreProcess(inBlock, outBlock); if (ret != SAMICoreCode.SAMI_OK) { System.out.println("LoudNorm2 process audio data failed, ret " + ret); return ret; } //从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]; } } numSamples = numSamples + inAudioBuffer.numberSamples; }
注意检查 SAMICoreProcess 函数的返回值,处理输入不正确或者内部处理错误都会有返回值,错误码可以查看错误检查一节
if (createHandleDone){ samiCore.SAMICoreDestroyHandle(); }