我正在编写一个 iPhone 应用程序,它应该记录用户的声音,并将音频数据输入到库中进行修改,例如改变速度和音调。我从 Apple 的 SpeakHere 示例代码开始:
http://developer.apple.com/library/ios/#samplecode/SpeakHere/Introduction/Intro.html http://developer.apple.com/library/ios/#samplecode/SpeakHere/Introduction/Intro.html
该项目为录制用户的声音并回放奠定了基础。效果很好。
现在我正在深入研究代码,我需要弄清楚如何将音频数据输入 SoundTouch 库(http://www.surina.net/soundtouch/ http://www.surina.net/soundtouch/)来改变音调。我在阅读代码的过程中熟悉了音频队列框架,并找到了从录音中接收音频数据的地方。
本质上,你打电话AudioQueueNewInput
创建一个新的输入队列。您传递一个回调函数,每次有音频数据块可用时都会调用该函数。正是在这个回调中,我需要将数据块传递到 SoundTouch 中。
我已完成所有设置,但从 SoundTouch 库播放的噪音非常静态(几乎与原始噪音相似)。如果我不通过 SoundTouch 传递它并播放原始音频,它就可以正常工作。
基本上,我遗漏了一些关于我得到的实际数据所代表的内容。我假设我得到了一个流short
s 是样本,每个通道 1 个样本。这就是 SoundTouch 所期望的,所以它一定是不正确的。
这是设置音频队列的代码,以便您可以了解它是如何配置的。
void AQRecorder::SetupAudioFormat(UInt32 inFormatID)
{
memset(&mRecordFormat, 0, sizeof(mRecordFormat));
UInt32 size = sizeof(mRecordFormat.mSampleRate);
XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareSampleRate,
&size,
&mRecordFormat.mSampleRate), "couldn't get hardware sample rate");
size = sizeof(mRecordFormat.mChannelsPerFrame);
XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareInputNumberChannels,
&size,
&mRecordFormat.mChannelsPerFrame), "couldn't get input channel count");
mRecordFormat.mFormatID = inFormatID;
if (inFormatID == kAudioFormatLinearPCM)
{
// if we want pcm, default to signed 16-bit little-endian
mRecordFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
mRecordFormat.mBitsPerChannel = 16;
mRecordFormat.mBytesPerPacket = mRecordFormat.mBytesPerFrame = (mRecordFormat.mBitsPerChannel / 8) * mRecordFormat.mChannelsPerFrame;
mRecordFormat.mFramesPerPacket = 1;
}
}
这是实际设置它的部分代码:
SetupAudioFormat(kAudioFormatLinearPCM);
// create the queue
XThrowIfError(AudioQueueNewInput(
&mRecordFormat,
MyInputBufferHandler,
this /* userData */,
NULL /* run loop */, NULL /* run loop mode */,
0 /* flags */, &mQueue), "AudioQueueNewInput failed");
最后,这是处理新音频数据的回调:
void AQRecorder::MyInputBufferHandler(void *inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp *inStartTime,
UInt32 inNumPackets,
const AudioStreamPacketDescription *inPacketDesc) {
AQRecorder *aqr = (AQRecorder *)inUserData;
try {
if (inNumPackets > 0) {
CAStreamBasicDescription queueFormat = aqr->DataFormat();
SoundTouch *soundTouch = aqr->getSoundTouch();
soundTouch->putSamples((const SAMPLETYPE *)inBuffer->mAudioData,
inBuffer->mAudioDataByteSize / 2 / queueFormat.NumberChannels());
SAMPLETYPE *samples = (SAMPLETYPE *)malloc(sizeof(SAMPLETYPE) * 10000 * queueFormat.NumberChannels());
UInt32 numSamples;
while((numSamples = soundTouch->receiveSamples((SAMPLETYPE *)samples, 10000))) {
// write packets to file
XThrowIfError(AudioFileWritePackets(aqr->mRecordFile,
FALSE,
numSamples * 2 * queueFormat.NumberChannels(),
NULL,
aqr->mRecordPacket,
&numSamples,
samples),
"AudioFileWritePackets failed");
aqr->mRecordPacket += numSamples;
}
free(samples);
}
// if we're not stopping, re-enqueue the buffe so that it gets filled again
if (aqr->IsRunning())
XThrowIfError(AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL), "AudioQueueEnqueueBuffer failed");
} catch (CAXException e) {
char buf[256];
fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
}
}
你可以看到我正在传递数据inBuffer->mAudioData
到 SoundTouch。在我的回调中,字节到底代表什么,即如何从中提取样本mAudioData
?