You need to enable JavaScript to run this app.
导航
【Java】非流式节拍检测
最近更新时间:2023.03.16 11:40:14首次发布时间:2023.03.16 11:40:14
实现

1. 创建handle

非流式节拍检测的跟其他功能的差异点在于处理必须一次性把所有要处理的数据送进去之后再获取结果,所以createParam.maxBlockSize应该是要处理的文件的帧数

SAMICoreExtractorCreateParam createParam = new SAMICoreExtractorCreateParam();
createParam.sampleRate = sampleRate;
createParam.numChannel = numChannel;
createParam.maxBlockSize = totalNumSamples;
int ret = samiCore.SAMICoreCreateHandleByIdentify(
        SAMICoreIdentify.SAMICoreIdentify_Extractor_BeatTrackingOffline, createParam);
if (ret != SAMICoreCode.SAMI_OK) {
    System.out.println("Create SAMICoreIdentify_Extractor_BeatTracking failed, ret " + ret);
    return ret;
}else {
    createHandleDone = true;
}

2. 加载模型

SAMICoreProperty property = new SAMICoreProperty();
property.id = SAMICorePropertyId.SAMICorePropertyId_LoadModelBinary;
property.type = SAMICoreDataType.SAMICoreDataType_ModelBin;
property.dataByteArray = FunctionHelper.readBinaryFile(modelFileName, context);
property.dataArrayLen = property.dataByteArray.length;
ret = samiCore.SAMICoreSetProperty(SAMICorePropertyId.SAMICorePropertyId_LoadModelBinary, property);
if (ret != SAMICoreCode.SAMI_OK) {
    System.out.println("BeatTracking SetProperty failed, ret " + ret);
    return ret;
}

3. 初始化buffer

inBlock = new SAMICoreBlock();
inBlock.dataType = SAMICoreDataType.SAMICoreDataType_AudioBuffer;
inAudioBuffer = new SAMICoreAudioBuffer();
inAudioBuffer.numberChannels = numChannel;
inAudioBuffer.numberSamples = totalNumSamples;
inAudioBuffer.isInterleave = 0;
inAudioBuffer.data = new float[inAudioBuffer.numberChannels][inAudioBuffer.numberSamples];
inBlock.audioData = new SAMICoreAudioBuffer[1];
inBlock.audioData[0] = inAudioBuffer;

4. 处理数据并获取结果

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][j];
    }
}

int ret = samiCore.SAMICoreProcess(inBlock, null);
if (ret != SAMICoreCode.SAMI_OK) {
    System.out.println("BeatTracking process audio data failed, ret " + ret);
    return ret;
}

// 获取检测结果
SAMICoreProperty samiCoreProperty = new SAMICoreProperty();
ret = samiCore.SAMICoreGetPropertyById(SAMICorePropertyId.SAMICorePropertyID_OverallFeatures, samiCoreProperty);
if (ret==SAMICoreCode.SAMI_OK && samiCoreProperty!= null){
    for (Object temp:samiCoreProperty.dataObjectArray){
        SAMICoreFeatureSet feature_set = (SAMICoreFeatureSet) samiCoreProperty.dataObjectArray[0];
        for(int i = 0; i < feature_set.numFeatureTypes; ++i) {
            if(feature_set.set[i].featureID == SAMICorePropertyId.SAMICorePropertyID_OverallFeature_TRACKING_OFFLINE_OVERALL.getValue()) {
                SAMICoreFeatureArray feature = feature_set.set[i];
                if (feature != null){
                    for (int j = 0; j < feature.numFeatures; j++) {
                        float timestamp = feature.array[j].time;
                        for (int k = 0; k < feature.array[j].numValues; k++) {
                            float beat = feature.array[j].values[k];
                            System.out.println("timestamp:" + timestamp + "; beat:" + beat);
                        }
                    }
                }
            }
        }
    }
}

5. 释放handle

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