You need to enable JavaScript to run this app.
导航
【Java】预置音效
最近更新时间:2023.03.16 11:40:11首次发布时间:2023.03.16 11:40:11
使用步骤
  1. 创建handle

音效相关的跟其他3A的处理有点差异,需要prepare的过程,内部进行一些buffer的申请。

a. 创建handle

samiCore = new SAMICore();
// 创建句柄
SAMICoreBinaryContextCreateParameter parameter = new SAMICoreBinaryContextCreateParameter();
parameter.resourceBuffer =  FunctionHelper.readBinaryFile(presetFileName,context);
parameter.resourceLen = parameter.resourceBuffer.length;
int ret = samiCore.SAMICoreCreateHandleByIdentify(SAMICoreIdentify.SAMICoreIdentify_Processor_ContextWithBinaryRes,parameter);
if (ret != SAMICoreCode.SAMI_OK) {
    System.out.println("SAMICoreCreateHandleByIdentify failed, ret " + ret);
    return ret;
}else{
    createHandleDone = true;
}

b. prepare

SAMICoreProcessorPrepareParameter prepareParameter = new SAMICoreProcessorPrepareParameter();
prepareParameter.blockSize = max_block_size;
prepareParameter.sampleRate = sampleRate;
prepareParameter.numChannel = numChannel;
SAMICoreProperty property = new SAMICoreProperty();
property.id = SAMICorePropertyId.SAMICorePropertyID_Processor_ContextPrepare;
property.type = SAMICoreDataType.SAMICoreDataType_ContextPrepareParameter;
property.dataObjectArray = new Object[1];
property.dataObjectArray[0] = prepareParameter;
property.dataArrayLen = 1;
ret = samiCore.SAMICoreSetProperty(SAMICorePropertyId.SAMICorePropertyID_Processor_ContextPrepare, property);
if (ret != SAMICoreCode.SAMI_OK) {
    System.out.println("SAMICorePropertyID_Processor_ContextPrepare failed, ret " + ret);
    return ret;
}
  1. 初始化buffer

//初始化处理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[inAudioBuffer.numberChannels][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[outAudioBuffer.numberChannels][outAudioBuffer.numberSamples];
outBlock.audioData = new SAMICoreAudioBuffer[1];
outBlock.audioData[0] = outAudioBuffer;
  1. 处理数据

for (int numSamples = 0; numSamples < totalNumSamples;) {
    if((numSamples + inAudioBuffer.numberSamples) > totalNumSamples) {
        inAudioBuffer.numberSamples = totalNumSamples - numSamples;
        outAudioBuffer.numberSamples = totalNumSamples - numSamples;
    }

    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("DenoiseV2 process audio data failed, ret " + ret);
        return ret;
    }

    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];
        }
    }
    // update index
    numSamples = numSamples + inAudioBuffer.numberSamples;
  1. 动态更新配置

预置音效的使用场景一般是在k歌场景,需要实时更新配置,sdk提供了动态更新的接口,接口保证了线程安全,可在不同于process的线程更新,并且内部做了平滑处理。

SAMICoreBinaryContextCreateParameter updateParameter = new SAMICoreBinaryContextCreateParameter();
updateParameter.resourceBuffer = FunctionHelper.readBinaryFile(updatePresetFileName,context);
updateParameter.resourceLen = updateParameter.resourceBuffer.length;
SAMICoreProperty update_property = new SAMICoreProperty();
update_property.dataObjectArray  =  new Object[1];
update_property.dataObjectArray[0] = updateParameter;
update_property.dataArrayLen = 1;
update_property.type = SAMICoreDataType.SAMICoreDataType_ResourceParameter;
update_property.id = SAMICorePropertyId.SAMICorePropertyID_Processor_ContextUpdateFromBinaryRes;
int ret = samiCore.SAMICoreSetProperty(SAMICorePropertyId.SAMICorePropertyID_Processor_ContextUpdateFromBinaryRes, update_property);
if(ret != SAMICoreCode.SAMI_OK) {
    System.out.println("SAMICorePropertyID_Processor_ContextUpdateFromBinaryRes failed, ret " + ret);
    return ret;
}
  1. 释放handle

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