同样是Apple官方的SpeakHere,要下载代码的,请查看上一篇文章。
void AQRecorder::StartRecord(CFStringRef inRecordFile)
{
mSoundTouch.setSampleRate(44100);//mRecordFormat.mSampleRate
mSoundTouch.setChannels(1);//mRecordFormat.mChannelsPerFrame
mSoundTouch.setTempoChange(1.0);
mSoundTouch.setPitchSemiTones(9);
mSoundTouch.setRateChange(-0.7);
mSoundTouch.setSetting(SETTING_SEQUENCE_MS, 40);
mSoundTouch.setSetting(SETTING_SEEKWINDOW_MS, 16);
mSoundTouch.setSetting(SETTING_OVERLAP_MS, 8);
//Only use one of the following two options
// mSoundTouch.setSetting(SETTING_USE_QUICKSEEK, 0);
// mSoundTouch.setSetting(SETTING_USE_AA_FILTER, !(0));
// mSoundTouch.setSetting(SETTING_AA_FILTER_LENGTH, 32);
// ____________________________________________________________________________________
// AudioQueue callback function, called when an input buffers has been filled.
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) {
UInt32 audioDataByteSize = inBuffer->mAudioDataByteSize;
CAStreamBasicDescription queueFormat = aqr->DataFormat();
SoundTouch *soundTouch = aqr->GetSoundTouch();
uint nSamples = audioDataByteSize/queueFormat.mBytesPerPacket;
soundTouch->putSamples((const SAMPLETYPE *)inBuffer->mAudioData,nSamples);
SAMPLETYPE *samples = (SAMPLETYPE *)malloc(audioDataByteSize);
UInt32 numSamples;
do {
memset(samples, 0, audioDataByteSize);
numSamples = soundTouch->receiveSamples((SAMPLETYPE *)samples, nSamples);
// write packets to file
XThrowIfError(AudioFileWritePackets(aqr->mRecordFile,
FALSE,
numSamples*queueFormat.mBytesPerPacket,
NULL,
aqr->mRecordPacket,
&numSamples,
samples),
"AudioFileWritePackets failed");
aqr->mRecordPacket += numSamples;
} while (numSamples!=0);
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));
}
}
原因大家自己应该能看明白。