重要更新:我已经找到答案并将它们放在这个简单的开源库中:http ://bartolsthoorn.github.com/NVDSP/看看,如果你遇到问题,它可能会为你节省相当多的时间IOS中的音频过滤器!
^
我创建了一个(实时)音频缓冲区(float *data
),其中包含几个sin(theta)
不同频率的波。
下面的代码显示了我是如何创建缓冲区的,我尝试做一个带通滤波器,但它只是将信号变成噪声/光点:
// Multiple signal generator
__block float *phases = nil;
[audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels)
{
float samplingRate = audioManager.samplingRate;
NSUInteger activeSignalCount = [tones count];
// Initialize phases
if (phases == nil) {
phases = new float[10];
for(int z = 0; z <= 10; z++) {
phases[z] = 0.0;
}
}
// Multiple signals
NSEnumerator * enumerator = [tones objectEnumerator];
id frequency;
UInt32 c = 0;
while(frequency = [enumerator nextObject])
{
for (int i=0; i < numFrames; ++i)
{
for (int iChannel = 0; iChannel < numChannels; ++iChannel)
{
float theta = phases[c] * M_PI * 2;
if (c == 0) {
data[i*numChannels + iChannel] = sin(theta);
} else {
data[i*numChannels + iChannel] = data[i*numChannels + iChannel] + sin(theta);
}
}
phases[c] += 1.0 / (samplingRate / [frequency floatValue]);
if (phases[c] > 1.0) phases[c] = -1;
}
c++;
}
// Normalize data with active signal count
float signalMulti = 1.0 / (float(activeSignalCount) * (sqrt(2.0)));
vDSP_vsmul(data, 1, &signalMulti, data, 1, numFrames*numChannels);
// Apply master volume
float volume = masterVolumeSlider.value;
vDSP_vsmul(data, 1, &volume, data, 1, numFrames*numChannels);
if (fxSwitch.isOn) {
// H(s) = (s/Q) / (s^2 + s/Q + 1)
// http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
// BW 2.0 Q 0.667
// http://www.rane.com/note170.html
//The order of the coefficients are, B1, B2, A1, A2, B0.
float Fs = samplingRate;
float omega = 2*M_PI*Fs; // w0 = 2*pi*f0/Fs
float Q = 0.50f;
float alpha = sin(omega)/(2*Q); // sin(w0)/(2*Q)
// Through H
for (int i=0; i < numFrames; ++i)
{
for (int iChannel = 0; iChannel < numChannels; ++iChannel)
{
data[i*numChannels + iChannel] = (data[i*numChannels + iChannel]/Q) / (pow(data[i*numChannels + iChannel],2) + data[i*numChannels + iChannel]/Q + 1);
}
}
float b0 = alpha;
float b1 = 0;
float b2 = -alpha;
float a0 = 1 + alpha;
float a1 = -2*cos(omega);
float a2 = 1 - alpha;
float *coefficients = (float *) calloc(5, sizeof(float));
coefficients[0] = b1;
coefficients[1] = b2;
coefficients[2] = a1;
coefficients[3] = a2;
coefficients[3] = b0;
vDSP_deq22(data, 2, coefficients, data, 2, numFrames);
free(coefficients);
}
// Measure dB
[self measureDB:data:numFrames:numChannels];
}];
我的目标是为这个缓冲区制作一个 10 波段均衡器,使用vDSP_deq22
,该方法的语法是:
vDSP_deq22(<float *vDSP_A>, <vDSP_Stride vDSP_I>, <float *vDSP_B>, <float *vDSP_C>, <vDSP_Stride vDSP_K>, <vDSP_Length __vDSP_N>)
参见:http: //developer.apple.com/library/mac/#documentation/Accelerate/Reference/vDSPRef/Reference/ reference.html#//apple_ref/doc/c_ref/vDSP_deq22
论据:
float *vDSP_A is the input data
float *vDSP_B are 5 filter coefficients
float *vDSP_C is the output data
我必须制作 10 个过滤器(10 次vDSP_deq22
)。然后我为每个频段设置增益并将它们组合在一起。但是我为每个滤波器提供什么系数?我知道这vDSP_deq22
是一个二阶(巴特沃斯)IIR 滤波器,但我如何将它变成带通滤波器?
现在我有三个问题:
a) 我必须对音频缓冲区进行去交错和交错吗?我知道将 stride 设置为 2 只是在通道上过滤,但我如何过滤另一个,stride 1 会将两个通道作为一个处理。
b)我是否必须在缓冲区进入vDSP_deq22
方法之前对其进行转换/处理?如果是这样,我是否也必须将其恢复正常?
c) 我应该为 10 秒设置什么系数值vDSP_deq22
?
我已经尝试了几天,但我一直无法弄清楚这一点,请帮助我!