我对 vDSP_zrip 和 AudioUnit 的使用和配置有疑问。事实上,我将 AudioUnit 配置为将打包数据保存为浮点数。我创建了一个循环缓冲区,当这个缓冲区已满时,我计算一个 fft。我有结果,但我不明白为什么 fft 输出不好(参见图)
音频单元配置:
// describe format
AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = 44100;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagsNativeEndian|kAudioFormatFlagIsPacked|kAudioFormatFlagIsFloat|kAudioFormatFlagIsNonInterleaved;
audioFormat.mFramesPerPacket = 1;
audioFormat.mChannelsPerFrame = 1; // mono
audioFormat.mBitsPerChannel = sizeof(float) * 8;
audioFormat.mBytesPerFrame = audioFormat.mChannelsPerFrame * sizeof(float);
audioFormat.mBytesPerPacket = audioFormat.mFramesPerPacket * audioFormat.mBytesPerFrame;
循环缓冲区:
_audioSample = new AudioSample(8192, 44100);
// in recording callback :
for(int i = 0; i < bufferList.mNumberBuffers; ++i)
{
if(!status)
{
if(_sample->needData())
_sample->put((float*)bufferList.mBuffers[i].mData,
bufferList.mBuffers[i].mDataByteSize);
[...]
}
}
vDSP 调用:
// get a split complex vector (real signal divided into an even-odd config
vDSP_ctoz((COMPLEX *)sample.get(), 2, &_complex, 1, _fftsize);
vDSP_fft_zrip(_fftsetup, &_complex, 1, _log2n, kFFTDirection_Forward);
// scale (from vDSP reference)
float scale = 1.0 / (2.0 * _samples);
vDSP_vsmul(_complex.realp, 1, &scale, _complex.realp, 1, _fftsize);
vDSP_vsmul(_complex.imagp, 1, &scale, _complex.imagp, 1, _fftsize);
_complex.imagp[0] = 0.0;
在哪里_fftsize = _audioSample.capacity()/2