尝试在android中读取.wav文件时ifstream(c ++)的替代方案

时间:2017-10-17 13:43:07

标签: java android c++ java-native-interface mfcc

我正在尝试从存储在SD卡上的wav文件计算MFCC系数。我正在使用图书馆:https://github.com/dspavankumar/compute-mfcc

输入是使用ifstream的wav文件路径。我需要能够根据时间段访问从单个wav文件中拆分的不同pcm文件,并计算每个段的MFCC。我无法找到一种方法将这些数据(原始pcm数据)从java类中获取到下面方法中显示的缓冲区中。 (使用JNI)

int process (std::ifstream &wavFp, std::ofstream &mfcFp) {
    // Read the wav header    
    wavHeader hdr;
    int headerSize = sizeof(wavHeader);

    wavFp.read((char *) &hdr, headerSize);

    // Check audio format
    if (hdr.AudioFormat != 1 || hdr.bitsPerSample != 16) {
        std::cerr << "Unsupported audio format, use 16 bit PCM Wave" << 
    std::endl;
        return 1;
    }
    // Check sampling rate
    if (hdr.SamplesPerSec != fs) {
        std::cerr << "Sampling rate mismatch: Found " << hdr.SamplesPerSec << " instead of " << fs <<std::endl;
        return 1;
    }

    // Initialise buffer
    uint16_t bufferLength = winLengthSamples-frameShiftSamples;
    int16_t* buffer = new int16_t[bufferLength];
    int bufferBPS = (sizeof buffer[0]);

    // Read and set the initial samples        
    wavFp.read((char *) buffer, bufferLength*bufferBPS);
    for (int i=0; i<bufferLength; i++)
        prevsamples[i] = buffer[i];        
    delete [] buffer;

    // Recalculate buffer size
    bufferLength = frameShiftSamples;
    buffer = new int16_t[bufferLength];

    // Read data and process each frame
    wavFp.read((char *) buffer, bufferLength*bufferBPS);
    while (wavFp.gcount() == bufferLength*bufferBPS && !wavFp.eof()) {
        mfcFp << processFrame(buffer, bufferLength);
        wavFp.read((char *) buffer, bufferLength*bufferBPS);
    }
    delete [] buffer;
    buffer = nullptr;
    return 0;
}

`

好像我无法直接从c ++库访问sd卡上的wav文件。所以我尝试传入jbytearray并使用:

将其转换为char *
extern "C"
JNIEXPORT void JNICALL
Java_com_example_nikhar_mst04v10_RecordActivity_doMFCC(JNIEnv *env, jobject 
instance,
                                                   jbyteArray wavBytes_) {
int len = env ->GetArrayLength(wavBytes_);
char* buf = new char[len];
env->GetByteArrayRegion(wavBytes_,0,len, reinterpret_cast<jbyte *>(buf));

// TODO

 /* env->ReleaseStringUTFChars(wavPath_, wavPath);
env->ReleaseStringUTFChars(mfccPath_, mfccPath);*/
// Assign variables
int numCepstra =  12;
int numFilters =  40;
int samplingRate = 16000;
int winLength = 25;
int frameShift = 10;
int lowFreq = 50;
int highFreq = samplingRate/2;

// Initialise MFCC class instance
MFCC mfccComputer (samplingRate, numCepstra, winLength, frameShift, 
numFilters, lowFreq, highFreq);
mfccComputer.process(buf);
}

但这不成功。有关如何完成此任务的任何建议?

1 个答案:

答案 0 :(得分:0)

看看这里:

http://jnicookbook.owsiak.org/recipe-No-012/

熟悉在Java和C之间传递数组。

至于jbyte - 此类型取决于机器。通常,它将是#34;签名字符#34;。我建议将数据传递给C,然后将数组转换为char *。它应该工作。

但是!确保在目标系统的情况下仔细检查如何声明jbyte。你可以在这里找到它:$ YOUR_PLATFORM_NAME / jni_md.h