Algorithm to make interleaved buffer to non-interleaved

Hi,

I have an interleaved audio buffer: short* Buffer.
And I have to do this conversion

[A][B][C][A][B][C][A][B][C] -> [A][A][A][B][B][B][C][C][C]

Is there some pattern or specific algorithm?
Thanks,
Daniele.
I'm a complete novice at this, but I thought I'd take a stab anyway. In the above example, could you have three separate buffers as like an intermediate processing stage? For example, as the interleaved buffer is read, it puts all the A things into the first buffer, the Bs into the second and so on. At the end, it concatenates the three buffers to produce your output.

I have no idea if I'm understanding the problem correctly...I'm looking forward to learning something here too :).
Solved.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

/*    
    Eg. with 3 channels.

    audioBuffer is the interleaved input buffer:

        1         2         3           n     <- nBufferFrames
 i = 1  2  3 | 4  5  6 | 7  8  9 |   
    [1][2][3]|[1][2][3]|[1][2][3]|..[1][2][3]

    the output must be:

 j = 1  2  3 | 4  5  6 | 7  8  9
    [1][1][1]|[2][2][2]|[3][3][3]|...


    i  | pos | iCh | j |  (NumOfChannels*iCh)+j
 ------|-----|-----|---|---------------------------
    0  |  0  |  0  | 0 |          0
    1  |  3  |  1  | 1 |          3
    2  |  6  |  2  | 2 |          6
 ------|-----|-----|---|---------------------------
    3  |  1  |  0  | 3 |          1
    4  |  4  |  1  | 4 |          4
    5  |  7  |  2  | 5 |          7
 ------|-----|-----|---|---------------------------
    6  |  2  |  0  | 6 |          2
    7  |  5  |  1  | 7 |          5
    8  |  8  |  2  | 8 |          8

*/

UINT BufferLen = (nBufferFrames * NumOfChannels);

for (short i=0, j=0, iCh = 0; i<BufferLen; i++, iCh++, j++)
{
  outBuffer[(NumOfChannels*iCh)+j] = audioBuffer[i];
  if (iCh = NumOfChannels) iCh = 0;
}
Thanks for posting what you got :).
Hmmm...just a question though. Let's say I have a BufferLen of 9, NumOfChannels of 3, with audioBuffer like this:

1 4 7 2 5 8 3 6 9

The corresponding outBuffer should be:

1 2 3 4 5 6 7 8 9

Now, when your 'for' loop starts, it puts audioBuffer[0] into outBuffer[0]. Good so far. The next iteration puts audioBuffer[1] into outBuffer[4]. That's not correct! It should put it into outBuffer[3], right?

What about this instead:

1
2
3
4
	for(int i=0; i<BufferLen; i++)
	{
		outBuffer[(i/NumChannels) + NumChannels*(i%NumChannels)] = audioBuffer[i];
	}

Hi Sammy,

I'm sorry but I don't understand what you mean. :-)
The buffer contains sample for audio channels so what is "1 4 7 2 5 8 3 6 9" ?

The schema is fixed:
- if I have 2 channels, the buffer is : [1][2][1][2][1][2]..
- if I have 3 channels, the buffer is : [1][2][3][1][2][3]..

and so on..and the result should be: [1][1][1]...[2][2][2] and [1][1][1]..[2][2][2]..[3][3][3]

Cheers,
Daniele.
Hi DBarzo,

Thanks a lot for your response. I clearly didn't understand the problem haha. I've learnt something today, which is what I was hoping for.

Cheers :).

Sam
The buffer contains sample for audio channels


Actually, I have to say I still don't understand what this means practically. Does that mean that a [1] means that 'something' should sample channel 1, [2] means it should sample channel 2 etc? I'd love it if you could spare the time to give me a real-world example...
The subject is quite big, so if u are interested, start here : http://en.wikipedia.org/wiki/Pulse_code_modulation

When you record from a sound card, the driver puts samples in a buffer.
A sample is the value of the signal in a particular instant.

When you have more than one channel (eg. usually the normal sound cards has 2 channels: LEFT and RIGHT), the samples in the buffer are interleaved:

[SAMPLE 1 CH1][SAMPLE 1 CH2][SAMPLE 2 CH1][SAMPLE 2 CH2] and so on..

Hope to be clear,
regards.
Daniele.
Topic archived. No new replies allowed.