8

如何将float *newAudiointofloat *channel1float* channel2interleave it back into 去交错newAudio

Novocaine *audioManager = [Novocaine audioManager];

__block float *channel1;
__block float *channel2;
[audioManager setInputBlock:^(float *newAudio, UInt32 numSamples, UInt32 numChannels) {
  // Audio comes in interleaved, so, 
  // if numChannels = 2, newAudio[0] is channel 1, newAudio[1] is channel 2, newAudio[2] is channel 1, etc. 

      // Deinterleave with vDSP_ctoz()/vDSP_ztoz(); and fill channel1 and channel2
      // ... processing on channel1 & channel2
      // Interleave channel1 and channel2 with vDSP_ctoz()/vDSP_ztoz(); to newAudio
}];

这两行代码会是什么样子?我不明白 ctoz/ztoz 的语法。

4

3 回答 3

12

我在 Novocaine 的附件类(如 Ringbuffer)中为去交错所做的工作:

float zero = 0.0;  
vDSP_vsadd(data, numChannels, &zero, leftSampleData, 1, numFrames);   
vDSP_vsadd(data+1, numChannels, &zero, rightSampleData, 1, numFrames);  

对于交错:

float zero = 0.0;  
vDSP_vsadd(leftSampleData, 1, &zero, data, numChannels, numFrames);   
vDSP_vsadd(rightSampleData, 1, &zero, data+1, numChannels, numFrames);  

更一般的做事方式是有一个数组数组,比如

int maxNumChannels = 2; 
int maxNumFrames = 1024;
float **arrays = (float **)calloc(maxNumChannels, sizeof(float *));  
for (int i=0; i < maxNumChannels; ++i) {  
    arrays[i] = (float *)calloc(maxNumFrames, sizeof(float));
}

[[Novocaine audioManager] setInputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels) {
    float zero = 0.0;
    for (int iChannel = 0; iChannel < numChannels; ++iChannel) {
        vDSP_vsadd(data, numChannels, &zero, arrays[iChannel], 1, numFrames);
    }
}];

这是我在 Novocaine 的 RingBuffer 附件类中大量使用的。我对 vDSP_vsadd 与 memcpy 的速度进行了计时,并且(非常非常令人惊讶)没有速度差异。

当然,您总是可以只使用环形缓冲区,省去麻烦

#import "RingBuffer.h"

int maxNumFrames = 4096
int maxNumChannels = 2
RingBuffer *ringBuffer = new RingBuffer(maxNumFrames, maxNumChannels)

[[Novocaine audioManager] setInputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels) {
    ringBuffer->AddNewInterleavedFloatData(data, numFrames, numChannels);
}];

[[Novocaine audioManager] setOuputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels) {
    ringBuffer->FetchInterleavedData(data, numFrames, numChannels);
}];

希望有帮助。

于 2012-04-29T16:58:42.323 回答
7

这是一个例子:

#include <Accelerate/Accelerate.h>

int main(int argc, const char * argv[])
{
    // Bogus interleaved stereo data
    float stereoInput [1024];
    for(int i = 0; i < 1024; ++i)
        stereoInput[i] = (float)i;

    // Buffers to hold the deinterleaved data
    float leftSampleData [1024 / 2];
    float rightSampleData [1024 / 2];

    DSPSplitComplex output = {
        .realp = leftSampleData,
        .imagp = rightSampleData
    };

    // Split the data.  The left (even) samples will end up in leftSampleData, and the right (odd) will end up in rightSampleData
    vDSP_ctoz((const DSPComplex *)stereoInput, 2, &output, 1, 1024 / 2);

    // Print the result for verification
    for(int i = 0; i < 512; ++i)
        printf("%d: %f + %f\n", i, leftSampleData[i], rightSampleData[i]);

    return 0;
}
于 2012-04-28T12:31:46.747 回答
3

sbooth 回答了如何使用 vDSP_ctoz 去交错。这是互补操作,即使用 vDSP_ztoc 进行交错。

#include <stdio.h>
#include <Accelerate/Accelerate.h>

int main(int argc, const char * argv[])
{
    const int NUM_FRAMES = 16;
    const int NUM_CHANNELS = 2;

    // Buffers for left/right channels
    float xL[NUM_FRAMES];
    float xR[NUM_FRAMES];

    // Initialize with some identifiable data
    for (int i = 0; i < NUM_FRAMES; i++)
    {
        xL[i] = 2*i;    // Even
        xR[i] = 2*i+1;  // Odd
    }

    // Buffer for interleaved data
    float stereo[NUM_CHANNELS*NUM_FRAMES];
    vDSP_vclr(stereo, 1, NUM_CHANNELS*NUM_FRAMES);

    // Interleave - take separate left & right buffers, and combine into
    // single buffer alternating left/right/left/right, etc.
    DSPSplitComplex    x = {xL, xR};
    vDSP_ztoc(&x, 1, (DSPComplex*)stereo, 2, NUM_FRAMES);

    // Print the result for verification. Should give output like
    //    i:     L,     R
    //    0:  0.00,  1.00
    //    1:  2.00,  3.00
    //    etc...
    printf(" i:     L,     R\n");
    for (int i = 0; i < NUM_FRAMES; i++)
    {
        printf("%2d: %5.2f, %5.2f\n", i, stereo[2*i], stereo[2*i+1]);
    }
    return 0;
}
于 2013-08-26T13:25:10.203 回答