我正在尝试将 SoundTouch C++ 库用于 Android 应用程序中的音频速度和音高变化。我已经成功地通过 JNI 推送了一个 Java byte[] 数组(来自 .wav),返回它,并用 .wav 播放它AudioTrack
。
下一步是尝试通过 SoundTouch 管道推送样本 byte[]。我已经剖析了库中包含的 SoundStretch 控制台程序的源代码,并尝试对其进行调整。我正在使用立体声 16 位源进行测试。
使用我当前的临时设置,我忽略了 RIFF 标头并将其与 .wav 数据一起转换,因为 JavaAudioTrack
对象不需要读取标头,它只播放原始 PCM。在不通过 SoundTouch 发送的情况下播放原始字节 [] 只会导致在标题所在的位置单击一下。
通过 SoundTouch 管道发送后,我正在播放应该是音频开头的白噪声。我假设我在write()
函数结束时遇到了问题,我将short's 转换为签名字符。在这里,控制台应用程序正在写入文件,而不是推送到向量:
int res = (int)fwrite(temp, 1, numBytes, fptr);
我已经阅读了文档,fwrite
但我对位旋转或音频处理知之甚少,无法知道如何在 char[] 中正确获取此信息,而不是写入文件。我知道我正在丢失演员表的信息,但我不确定如何纠正它。
如果有人有额外的动力,可以在这里找到 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;
}