我正在尝试使用SoundTouch C++库在Android应用程序中更改音频速度和音高。我已经通过JNI成功推送了Java byte []数组(来自.wav),返回了该数组,并使用AudioTrack对其进行了播放。

下一步是尝试通过SoundTouch管道推送样本byte []。我已经剖析了该库随附的SoundStretch控制台程序的源代码,并尝试对其进行调整。我正在使用立体声16位源进行测试。

使用当前的临时设置,我将忽略RIFF header ,并将其与.wav数据一起转换,因为Java AudioTrack对象不需要读取 header ,它只播放原始PCM。不通过SoundTouch发送而播放原始byte []只会在标题所在的位置单击一下。

通过SoundTouch管道发送后,我正在播放白噪声,该白噪声应该是音频的开始位置。我以为我在write()函数的末尾遇到问题,我正在将短字符转换为已签名的字符。在这里,控制台应用程序正在写入文件,而不是推送到 vector :

int res = (int)fwrite(temp, 1, numBytes, fptr);

我已经阅读了fwrite的文档,但是我对位旋转或音频处理的了解还不足以了解如何在char []中正确获取此信息而不是写入文件。我知道我在 Actor 表上失去了信息,但是我不确定如何纠正它。

如果有人动力十足,可以在这里找到SoundStretch来源:http://www.surina.net/soundtouch/sourcecode.html
extern "C" DLL_PUBLIC jbyteArray
Java_net_surina_soundtouch_SoundTouch_getMutatedBytes
(JNIEnv *env, jobject thiz, jbyteArray input, jint length)
{
    const int BUFF_SIZE = 2048000;

    SoundTouch soundTouch;

    jboolean isCopy;
    jbyte* ar = env->GetByteArrayElements(input, &isCopy);

    signed char* cBufferIn = (signed char*)ar;

    SAMPLETYPE* fBufferIn = new SAMPLETYPE[length];
    vector<signed char> fBufferOut;

    //converts the chars to floats per the SoundTouch console app.
    convertInput16(cBufferIn, fBufferIn, length);

    //channels, sampling rate, speed, pitch change
    setup(&soundTouch, 2, 44100, 1.0, 0);

    //transform floats from fBufferIn to fBufferout
    process(&soundTouch, fBufferIn, fBufferOut, BUFF_SIZE);

    signed char* res = &fBufferOut[0];

    jbyteArray result = env->NewByteArray(length);
    env->SetByteArrayRegion(result, 0, fBufferOut.size(), res);

    LOGV("fBufferOut Size: %d", fBufferOut.size());

    delete[] fBufferIn;

    return result;
}

处理():
static void process(SoundTouch* soundTouch, SAMPLETYPE* fBufferIn, vector<signed char>& fBufferOut, int BUFF_SIZE)
{
    int nSamples = BUFF_SIZE / 2; //2 bytes per sample, using 16 bit sample for testing
    int buffSizeSamples = BUFF_SIZE / 2; //2 channel stereo

    soundTouch->putSamples(fBufferIn, nSamples);

    do
    {
        nSamples = soundTouch->receiveSamples(fBufferIn, buffSizeSamples);
        write(fBufferIn, fBufferOut, nSamples / 2); //2 channels
    } while (nSamples != 0);

    soundTouch->flush();

    do
    {
        nSamples = soundTouch->receiveSamples(fBufferIn, buffSizeSamples);
        write(fBufferIn, fBufferOut, nSamples / 2);
        LOGV("NUMBER OF SAMPLES: %d", nSamples);
    } while (nSamples != 0);
}

写():
static void write(const float *bufferIn, vector<signed char>& bufferOut, int numElems)
{
    int numBytes;
    int bytesPerSample;

    if (numElems == 0) return;

    bytesPerSample = 16 / 8; //16 bit test sample / bits in a byte
    numBytes = numElems * bytesPerSample;
    short *temp = (short*)getConvBuffer(numBytes);

    switch (bytesPerSample)
    {

    case 2: //16 bit encoding per the SoundStretch console app
    {
              short *temp2 = (short *)temp;
              for (int i = 0; i < numElems; i++)
              {
                  short value = (short)saturate(bufferIn[i] * 32768.0f, -32768.0f, 32767.0f); //magic to me
                  temp2[i] = value; //works for little endian only.
              }
              break;
    }

    default:
        assert(false);
    }

    for (int i = 0; i < numElems; ++i)
    {
        bufferOut.push_back((signed char)temp[i]); //I think my problem is here.
    }

    delete[] temp;

    //bytesWritten += numBytes;
}

最佳答案

我只需要获取char []中的所有位:

for (int i = 0; i < numElems; ++i)
{
    bufferOut.push_back(temp[i] & 0xff);
    bufferOut.push_back((temp[i] >> 8) & 0xff);
}

关于c++ - 从SoundTouch音频库转换short []以进行播放,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20930127/

10-11 08:49