1

我有以下一段音频代码,我认为它可以很好地在加速框架中使用 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 没有明显的好处。我这样做正确吗?非常感谢。

对矢量操作还是陌生的,对我来说放轻松:)

如果我应该做一些明显的优化(在加速框架之外),请随时向我指出,谢谢!

4

1 回答 1

1

您的矢量呼叫正在以音频采样率对每个样本执行 2 次乘法。如果您的采样率为 192kHz,那么您只是在谈论每秒 384000 次乘法 - 不足以在现代 CPU 上注册。此外,您正在现有乘法移动到另一个地方。如果您查看生成的程序集,我猜编译器相当不错地优化了您的原始代码,并且 vDSP 调用中的任何加速都将被您需要第二个循环的事实所抵消。

另一个需要注意的重要事项是,当矢量数据在 16 字节边界上对齐时,所有 vDSP 功能都会更好地工作。如果您查看 SSE2 指令集(我确信 vDSP 大量使用),您会发现许多指令都有一个用于对齐数据的版本和另一个用于未对齐数据的版本。

在 gcc 中对齐数据的方式是这样的:

float inVector[8] = {1, 2, 3, 4, 5, 6, 7, 8} __attribute__ ((aligned(16)));

或者,如果您在堆上分配,请查看是否aligned_malloc可用。

于 2015-02-26T04:10:49.510 回答