You need to enable JavaScript to run this app.
导航
【Java】实时音量均衡
最近更新时间:2023.03.16 11:40:14首次发布时间:2023.03.16 11:40:14
实现

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_LoudNorm2, parameter);
if (ret != SAMICoreCode.SAMI_OK) {
    System.out.println("Create LoudNorm2 handle failed, ret " + ret);
    return ret;
}

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

2. 设置参数

通过 SAMICoreSetProperty 设置target_lufs 参数。

  • target_lufs(默认值-16dB)(-24到-12,建议实验值为-12,-16,-20)
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);
}

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

5.释放handle

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