录制的声音比原始声音慢

时间:2015-08-13 11:54:10

标签: c loops audio opensl libsndfile

我试图实现循环功能。基本上我将音乐样本保存在缓冲区中。 然后当我按下循环按钮时,我按时设置初始循环,当按下循环时设置循环输出时间。 为了得到循环部分,我循环时间 - 循环时间,这给了我循环的秒数。然后我乘以秒*采样率(44100),分配一个具有该大小的新缓冲区并用声音数据填充它。 一切都很好但是当我通过Open SL ES收听循环缓冲区时,声音比原始声音慢。 我真的不知道为什么......

SLmillisecond loopInPosition, loopOutPosition;
float loopDuration;
const char *lastPath;
short *sampleBuffer, *loopBuffer;
int xSampleBufferSize, xLoopBuffersize;

void playBuffer();

// get the position in the music sample when loop in or out button is pressed
SLmillisecond getCurrentPosition(jint playerIndex, SLmillisecond* whereToSave) {

    Player player = players[playerIndex];
    if (player.isReady == 1) {
        (*(player.playerPlayItf))->GetPosition(player.playerPlayItf,
                whereToSave);

        __android_log_print(ANDROID_LOG_DEBUG, "jajaja",
                ">>NATIVE position is: %d", *whereToSave);
    }
}

void loopIn(JNIEnv* env,
        jclass clazz, jint selectedSlot, jstring filename) {

    lastPath = (*env)->GetStringUTFChars(env, filename, NULL);
    assert(NULL != path);

    getCurrentPosition(selectedSlot, &loopInPosition);
}

void loopOut(JNIEnv* env,
        jclass clazz, jint selectedSlot) {

    getCurrentPosition(selectedSlot, &loopOutPosition);

    loopDuration = (float) ((float) (loopOutPosition - loopInPosition) / 1000);

    __android_log_print(ANDROID_LOG_DEBUG, "jajaja",
            ">>NATIVE loop duration is: %f", loopDuration);

    SF_INFO sndInfo;
    SNDFILE *sndFile = sf_open(lastPath, SFM_READ, &sndInfo);

    if (sndInfo.format != (SF_FORMAT_WAV | SF_FORMAT_PCM_16)) {
        fprintf(stderr, "Input should be 16bit Wav\n");
        sf_close(sndFile);
    } else {

        __android_log_print(ANDROID_LOG_DEBUG, "jajaja",
                ">>NATIVE File info samplerate %d, channels %d, format %d, sections %d, seekable %d",
                sndInfo.samplerate, sndInfo.channels, sndInfo.format,
                sndInfo.sections, sndInfo.seekable);
    }

    // Sample Buffer
    int sampleBufferSize = sndInfo.frames * sizeof(short);
    xSampleBufferSize = sampleBufferSize;
    sampleBuffer = malloc(sampleBufferSize);


    __android_log_print(ANDROID_LOG_DEBUG, "jajaja",
            ">>NATIVE allocated sample buffer: size %d, frames %d",
            sampleBufferSize, sndInfo.frames);

    sf_readf_short(sndFile, sampleBuffer, sndInfo.frames);

    // Loop Buffer
    int loopBufferSize = loopDuration * 44100 * sizeof(short);
    int loopBufferFrames = loopDuration * 44100;
    xLoopBuffersize = loopBufferSize;
    loopBuffer = malloc(loopBufferSize);

    __android_log_print(ANDROID_LOG_DEBUG, "jajaja",
            ">>NATIVE allocated loop buffer: size %d, frames %d",
            loopBufferSize, loopBufferFrames);

    int index, i = 0;
    int startIndex = (int) ((float) (loopInPosition / 1000) * 44100);

    __android_log_print(ANDROID_LOG_DEBUG, "jajaja",
            ">>NATIVE start index is %d", startIndex);

    for (index = startIndex, i = 0; index < startIndex + loopBufferFrames;
            ++index, ++i) {

        loopBuffer[i] = sampleBuffer[index];
    }
}

感谢您的时间

0 个答案:

没有答案