You need to enable JavaScript to run this app.
导航
【Java】自动增益
最近更新时间:2023.07.07 11:16:17首次发布时间:2023.03.16 11:40:13
使用步骤

1. 创建handle

samiCore = new SAMICore();
SAMICore3ACreateParameter parameter = new SAMICore3ACreateParameter();
parameter.sampleRate = sampleRate;
parameter.numChannel = numChannel;
parameter.maxBlockSize = maxBlockSize;
int ret = samiCore.SAMICoreCreateHandleByIdentify(SAMICoreIdentify.SAMICoreIdentify_AGC, parameter);
if (ret != SAMICoreCode.SAMI_OK) {
  System.out.println("Create AGC handle failed, ret " + ret);
  return ret;
} else {
  createHandleDone = true;
}

parameter.maxBlockSize:表示每一次处理单个通道的最大sample数
注:AGC以每次传入的块数据为单位进行处理,不同数据块大小处理结果不同

2. 设置参数

AGC有三种可调节参数,根据需要进行调整

ID含义取值范围默认值
SAMICorePropertyID_AGC_SetTargetLevel设置最大的音量界限,改变增益不会超过这个值,单位是dbfs,比如默认值为3,即最大音量是-3dbfs[0,100]3
SAMICorePropertyID_AGC_SetGain设置音频的增益,单位是dbfs,比如默认值为9,即+9dbfs[0, 100]9
SAMICorePropertyID_AGC_SetEnableLimiter设置是否开启限制器0/11
SAMICoreProperty samiCoreProperty = new SAMICoreProperty();
samiCoreProperty.id = SAMICorePropertyId.SAMICorePropertyID_AGC_SetTargetLevel;
samiCoreProperty.type = SAMICoreDataType.SAMICoreDataType_Float;
samiCoreProperty.dataObjectArray = new Object[1];
samiCoreProperty.dataObjectArray[0] = data;
samiCoreProperty.dataArrayLen = 1;
int ret = samiCore.SAMICoreSetProperty(SAMICorePropertyId.SAMICorePropertyID_AGC,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 = maxBlockSize;
inBlock.audioData = new SAMICoreAudioBuffer[1];
inBlock.audioData[0] = inAudioBuffer;

outBlock.dataType = SAMICoreDataType.SAMICoreDataType_AudioBuffer;
outAudioBuffer = new SAMICoreAudioBuffer();
outAudioBuffer.numberChannels = numChannel;
outAudioBuffer.numberSamples = maxBlockSize;
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("AGC 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();
}