音效相关的跟其他3A的处理有点差异,需要prepare的过程,内部进行一些buffer的申请。
int ret = samiCore.SAMICoreCreateHandleByIdentify(SAMICoreIdentify.SAMICoreIdentify_Processor_TimeDomainPitchShifter,null); if (ret != SAMICoreCode.SAMI_OK) { System.out.println("SAMICoreCreateHandleByIdentify failed, ret " + ret); return ret; }else{ createHandleDone = true; }
SAMICoreProcessorPrepareParameter prepareParameter = new SAMICoreProcessorPrepareParameter(); prepareParameter.blockSize = max_block_size; prepareParameter.sampleRate = sampleRate; prepareParameter.numChannel = numChannel; SAMICoreProperty property = new SAMICoreProperty(); property.id = SAMICorePropertyId.SAMICorePropertyID_Processor_Prepare; property.type = SAMICoreDataType.SAMICoreDataType_ContextPrepareParameter; property.dataObjectArray = new Object[1]; property.dataObjectArray[0] = prepareParameter; property.dataArrayLen = 1; ret = samiCore.SAMICoreSetProperty(SAMICorePropertyId.SAMICorePropertyID_Processor_Prepare, property); if (ret != SAMICoreCode.SAMI_OK) { System.out.println("SAMICorePropertyID_Processor_ContextPrepare failed, ret " + ret); return ret; }
参数的设置可以分为两种情况:
在主线程中创建了 handle,但音频线程还 未启动 音频处理时。这种情况下,调用 离线参数 设置接口。
音频线程 已经启动 ,并正在进行音频处理时。这种情况下,调用 实时参数 设置接口
SAMICoreProperty offline_property = new SAMICoreProperty(); String param_str = "{\"parameters\": {\"Pitch Ratio\": 0.5} }"; offline_property.type = SAMICoreDataType.SAMICoreDataType_String; offline_property.id = SAMICorePropertyId.SAMICorePropertyID_Processor_SetParametersOffline; offline_property.dataObjectArray = new Object[1]; offline_property.dataObjectArray[0] = param_str; offline_property.dataArrayLen = 1; ret = samiCore.SAMICoreSetProperty(SAMICorePropertyId.SAMICorePropertyID_Processor_SetParametersOffline, offline_property); if (ret != SAMICoreCode.SAMI_OK) { System.out.println("SAMICorePropertyID_Processor_SetParametersOffline failed, ret " + ret); return ret; }
SAMICoreContextParameterEvent param_event = new SAMICoreContextParameterEvent(); param_event.parameterIndex = SAMICoreProcessorTimeDomainPitchShifterParameter.TimeDomainPitchShifter_Pitch_Ratio.getValue(); param_event.plainValue = 2.0f; SAMICoreProperty update_property = new SAMICoreProperty(); update_property.dataObjectArray = new Object[1]; update_property.dataObjectArray[0] = param_event; update_property.dataArrayLen = 1; update_property.type = SAMICoreDataType.SAMICoreDataType_ParameterEvent; update_property.id = SAMICorePropertyId.SAMICorePropertyID_Processor_ContextEmplaceParameterEventNowWithPlainValue; int ret = samiCore.SAMICoreSetProperty( SAMICorePropertyId.SAMICorePropertyID_Processor_ContextEmplaceParameterEventNowWithPlainValue, update_property); if(ret != SAMICoreCode.SAMI_OK) { System.out.println("SAMICorePropertyID_Processor_ContextEmplaceParameterEventNowWithPlainValue failed, ret " + ret); return ret; }
SAMICoreContextParameterEvent 描述了参数的位置和值
parameterIndex,参数下标,index可以在对应参数的枚举类中查看
plainValue,参数值,有三种数据类型:float/bool/choice,processor列表
以“TimeDomainPitchShifter”为例:
数据类型 | 内容 |
---|---|
Float | 直接填写需要的数值 |
Bool | 0 表示 false,1 表示 true |
Choice | "Interpolation Mode" 的 0 表示 "Linear",1 表示 "Lagrange4",以此类推 |
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;
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; }
if (createHandleDone){ samiCore.SAMICoreDestroyHandle(); }