繁体   English   中英

尝试在Android中读取.wav文件时使用ifstream(C ++)的替代方法

[英]Alternative for ifstream (c++) whilst trying to read a .wav file in android

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

输入是使用ifstream的wav文件路径。 我需要能够根据时间段访问从单个wav文件分割的不同pcm文件,并为每个段计算MFCC。 我在寻找一种方法来从java类将数据(原始pcm数据)获取到下面方法所示的缓冲区中时遇到麻烦。 (使用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);
}

但这是不成功的。 任何建议,我如何才能做到这一点?

在这里看看:

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

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

至于jbyte-这种类型取决于机器。 通常,它将是“ signed char”。 我建议将您的数据传递给C,然后将数组转换为char *。 它应该工作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM