You need to enable JavaScript to run this app.
导航
【Java】预处理音量均衡
最近更新时间:2023.03.17 16:45:47首次发布时间:2023.03.17 16:45:47
实现

1. 创建handle

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数

2. 设置参数

将 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);
}

3. 初始化buffer

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

4.处理数据

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 函数的返回值,处理输入不正确或者内部处理错误都会有返回值,错误码可以查看错误检查一节

5.释放handle

if (createHandleDone){
    samiCore.SAMICoreDestroyHandle();
}