【发布时间】:2015-02-26 01:07:55
【问题描述】:
我有以下一段音频代码,我认为它非常适合在加速框架中使用 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 没有明显的好处。 我这样做正确吗?非常感谢。
对向量运算还是个新手,请放轻松 :)
如果我应该做一些明显的优化(加速框架之外),请随时向我指出,谢谢!
【问题讨论】:
-
这里假设 m_pVoiceArray[i]->doVoice(dLeft, dRight);正在修改 dLeft 和 dRight (因为它们是通过引用传递的,这是 C++?)我会让 doVoice 函数一次产生一堆样本,而不仅仅是一个。您可能大部分时间都花在开销查找数据和进行函数调用上。也就是说,反转帧处理循环中的循环顺序。否则,我想向您介绍我的朋友乘法运算符。
-
是的,它们是通过引用传递的,C++。
-
可能它并没有加快多少,因为每个样本的大部分时间都花在了 MAX_VOICES 循环中。您可以通过查看 Instruments 中每行源代码的时间使用情况来验证这一点。与每个(样本 * 语音)调用函数指针的成本相比,像您从循环中拉出的乘法是微不足道的。
-
是的,我一直在这样做,因为...正如你所说,在某些地方可以将计算从主处理循环中提取出来,或者减少计算频率。
标签: audio accelerate-framework vdsp