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_LoudNorm, parameter); if (ret != SAMICoreCode.SAMI_OK) { System.out.println("Create LoudNorm handle failed, ret " + ret); return ret; }
parameter.maxBlockSize:表示每一次处理单个通道的最大sample数
将 source_lufs(源文件响度) 、source_peak(源文件峰值) (参考响度检测和【C】响度检测)和 target_lufs(目标响度) 参数组成对象,通过 SAMICoreSetProperty 设置参数。
target_lufs(默认值-16dB),(-24到-12,建议实验值为-12,-16,-20)
source_lufs(默认值0dB),(-inf:输入为零数据时,-70 - 0:正常范围)
source_peak(默认值99),(0到1,部分歌曲可能大于1,截取到1)
SAMICoreProperty samiCoreProperty = new SAMICoreProperty(); samiCoreProperty.id = SAMICorePropertyId.SAMICorePropertyID_Processor_Loudnorm; samiCoreProperty.type = SAMICoreDataType.SAMICoreDataType_LoudNorm; SAMICoreLoudNormProperty Loudnorm_property = new SAMICoreLoudNormProperty(); Loudnorm_property.source_lufs = sourceLufs; Loudnorm_property.source_peak = sourcePeak; Loudnorm_property.target_lufs = targetLufs; samiCoreProperty.dataObjectArray = new SAMICoreLoudNormProperty[1]; samiCoreProperty.dataObjectArray[0] = Loudnorm_property; samiCoreProperty.dataArrayLen = 1; int ret = samiCore.SAMICoreSetProperty(SAMICorePropertyId.SAMICorePropertyID_Processor_Loudnorm,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("LoudNorm 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(); }