接收到的声音(UDP WiFi)存在问题,我想尽可能地清除它。所以一开始我想要切断某些频率以上的声音。显然,我从套接字获取原始数据,然后将其复制到输出缓冲区。我确信应该在那里完成切断。
你能建议我吗?
我目前的回调代码
static OSStatus outputCallback(void *udata,
AudioUnitRenderActionFlags *flags,
const AudioTimeStamp *ts,
UInt32 busnum,
UInt32 nframes,
AudioBufferList *buflist) {
NXAudioDevice *dev = (__bridge NXAudioDevice *) udata;
AudioBuffer *buf = buflist->mBuffers;
// Here I get new audioBufferData
NSData *data = [dev getAudioData];
if (!data) {
buf->mDataByteSize = 0;
return -1;
} else {
[data getBytes:buf->mData length:buf->mDataByteSize];
}
return noErr;
}
我为渲染回调找到了类似的东西,atm我想为outputCallback添加类似的内容。
OSStatus RenderFFTCallback (void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
RIOInterface* THIS = (RIOInterface *)inRefCon;
COMPLEX_SPLIT A = THIS->A;
void *dataBuffer = THIS->dataBuffer;
float *outputBuffer = THIS->outputBuffer;
FFTSetup fftSetup = THIS->fftSetup;
uint32_t log2n = THIS->log2n;
uint32_t n = THIS->n;
uint32_t nOver2 = THIS->nOver2;
uint32_t stride = 1;
int bufferCapacity = THIS->bufferCapacity;
SInt16 index = THIS->index;
AudioUnit rioUnit = THIS->ioUnit;
OSStatus renderErr;
UInt32 bus1 = 1;
renderErr = AudioUnitRender(rioUnit, ioActionFlags,
inTimeStamp, bus1, inNumberFrames, THIS->bufferList);
if (renderErr < 0) {
return renderErr;
}
// Fill the buffer with our sampled data. If we fill our buffer, run the
// fft.
int read = bufferCapacity - index;
if (read > inNumberFrames) {
memcpy((SInt16 *)dataBuffer + index, THIS->bufferList->mBuffers[0].mData, inNumberFrames*sizeof(SInt16));
THIS->index += inNumberFrames;
} else {
// If we enter this conditional, our buffer will be filled and we should
// perform the FFT.
memcpy((SInt16 *)dataBuffer + index, THIS->bufferList->mBuffers[0].mData, read*sizeof(SInt16));
// Reset the index.
THIS->index = 0;
/*************** FFT ***************/
// We want to deal with only floating point values here.
ConvertInt16ToFloat(THIS, dataBuffer, outputBuffer, bufferCapacity);
/**
Look at the real signal as an interleaved complex vector by casting it.
Then call the transformation function vDSP_ctoz to get a split complex
vector, which for a real signal, divides into an even-odd configuration.
*/
vDSP_ctoz((COMPLEX*)outputBuffer, 2, &A, 1, nOver2);
// Carry out a Forward FFT transform.
vDSP_fft_zrip(fftSetup, &A, stride, log2n, FFT_FORWARD);
// The output signal is now in a split real form. Use the vDSP_ztoc to get
// a split real vector.
vDSP_ztoc(&A, 1, (COMPLEX *)outputBuffer, 2, nOver2);
// Determine the dominant frequency by taking the magnitude squared and
// saving the bin which it resides in.
float dominantFrequency = 0;
int bin = -1;
for (int i=0; i<n; i+=2) {
float curFreq = MagnitudeSquared(outputBuffer[i], outputBuffer[i+1]);
if (curFreq > dominantFrequency) {
dominantFrequency = curFreq;
bin = (i+1)/2;
}
}
memset(outputBuffer, 0, n*sizeof(SInt16));
// Update the UI with our newly acquired frequency value.
[THIS->listener frequencyChangedWithValue:bin*(THIS->sampleRate/bufferCapacity)];
printf("Dominant frequency: %f bin: %d \n", bin*(THIS->sampleRate/bufferCapacity), bin);
}
return noErr;
}
答案 0 :(得分:0)
这并不像看起来那么容易。一种方法是使用FFT将数据移入频域,移除高频,然后使用反向FFT移回时域。 iOS中提供FFT功能。请参阅使用傅里叶变换vDSP Programming Guide。
一个起点是Apple的示例代码aurioTouch2。
回答评论:一个字节没有频率,只有一个幅度(响度)。基本上存在周期速率的幅度样本,例如44100Hz。低通音频的一种天真的方法是去除所有其他样本,但这不起作用,它只是将较高频率混叠成较低频率。
答案 1 :(得分:0)
您可以使用AudioUnit执行此操作:
@constant kAudioUnitSubType_LowPassFilter
A filter that passes frequencies below a specified cut-off frequency
@constant kAudioUnitSubType_HighPassFilter
A filter that passes frequencies above a specified cut-off frequency
@constant kAudioUnitSubType_BandPassFilter
A filter that passes frequencies between a low and high cut-off frequency.