我有以下音频代码,我认为这是在加速框架中使用vDSP的不错的选择。
// --- get pointers for buffer lists
float* left = (float*)audio->mBuffers[0].mData;
float* right = numChans == 2 ? (float*)audio->mBuffers[1].mData : NULL;
float dLeftAccum = 0.0;
float dRightAccum = 0.0;
float fMix = 0.25; // -12dB HR per note
// --- the frame processing loop
for(UInt32 frame=0; frame<inNumberFrames; ++frame)
{
// --- zero out for each trip through loop
dLeftAccum = 0.0;
dRightAccum = 0.0;
float dLeft = 0.0;
float dRight = 0.0;
// --- synthesize and accumulate each note's sample
for(int i=0; i<MAX_VOICES; i++)
{
// --- render
if(m_pVoiceArray[i])
m_pVoiceArray[i]->doVoice(dLeft, dRight);
// --- accumulate and scale
dLeftAccum += fMix*(float)dLeft;
dRightAccum += fMix*(float)dRight;
}
// --- accumulate in output buffers
// --- mono
left[frame] = (float)dLeftAccum;
// --- stereo
if(right) right[frame] = (float)dRightAccum;
}
// needed???
// mAbsoluteSampleFrame += inNumberFrames;
return noErr;
因此,我将其修改为使用vDSP,在帧块的末尾乘以fMix。
// --- the frame processing loop
for(UInt32 frame=0; frame<inNumberFrames; ++frame)
{
// --- zero out for each trip through loop
dLeftAccum = 0.0;
dRightAccum = 0.0;
float dLeft = 0.0;
float dRight = 0.0;
// --- synthesize and accumulate each note's sample
for(int i=0; i<MAX_VOICES; i++)
{
// --- render
if(m_pVoiceArray[i])
m_pVoiceArray[i]->doVoice(dLeft, dRight);
// --- accumulate and scale
dLeftAccum += (float)dLeft;
dRightAccum += (float)dRight;
}
// --- accumulate in output buffers
// --- mono
left[frame] = (float)dLeftAccum;
// --- stereo
if(right) right[frame] = (float)dRightAccum;
}
vDSP_vsmul(left, 1, &fMix, left, 1, inNumberFrames);
vDSP_vsmul(right, 1, &fMix, right, 1, inNumberFrames);
// needed???
// mAbsoluteSampleFrame += inNumberFrames;
return noErr;
但是,我的CPU使用率仍然保持不变。
我在这里看不到使用vDSP的明显好处。
我这样做正确吗?非常感谢。
vector 操作还是新手,请放心:)
如果我应该做一些明显的优化(在加速框架之外),请随时向我指出,谢谢!
最佳答案
您正在进行 vector call ,以音频采样率对每个采样执行2个乘法。如果您的采样率为192kHz,那么您只说的是每秒384000次乘法-不足以在现代CPU上进行注册。此外,您要将现有的乘法移动到另一个地方。如果您查看生成的程序集,我猜想编译器会相当不错地优化您的原始代码,并且您需要第二个循环的事实会抵消vDSP调用中的任何加速。
另一个需要注意的重要事项是,当 vector 数据在16字节边界上对齐时,所有vDSP功能都将更好地工作。如果您看一下SSE2指令集(我相信vDSP会大量使用),您会发现许多指令具有用于对齐数据的版本和用于未对齐数据的另一个版本。
在gcc中对齐数据的方式是这样的:
float inVector[8] = {1, 2, 3, 4, 5, 6, 7, 8} __attribute__ ((aligned(16)));
或者,如果您要在堆上分配,请查看
aligned_malloc
是否可用。
关于audio - 使用加速的框架,没有明显的加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28732771/