0

So I found some C code to decompress certain PCM samples, and I want to do the same in AS3. I have tried to do so, but it doesn't seem to be working. I know little to no C, although I'm quite fluent in AS3, so I might be translating it incorrectly.

Here is the C code:

BYTE  bShift; // shift value
char *inputBuffer[dwSize];

SHORT iCurValue;
DWORD i;

iCurValue=0x0000;

for (i=0;i<dwSize;i++)
{
    iCurValue+=(signed short)inputBuffer[i];
    Output(iCurValue<<bShift);
}

(Output() is a placeholder for the action to do on the decompressed sample value.)

Here is a function I made in AS3 which is supposed to do the same thing:

private function pcmDecompress(pcmBytes:ByteArray, shift:int):ByteArray
{
    var input:ByteArray = pcmBytes;
    var output:ByteArray = new ByteArray();
    output.endian = Endian.LITTLE_ENDIAN;

    var iCurValue:int = 0x0000;

    for (var i:int = 0; i < input.length; i++)
    {
        iCurValue += input[i];
        output.writeShort(iCurValue << shift);
    }

    return output;
}

Afterwards I encode a WAV file and save it, using output as the data section containing the samples. The WAV file plays, but all I hear is a lot of static.

Any help would be greatly appreciated.

puggsoy
  • 1,270
  • 1
  • 11
  • 34

1 Answers1

0

Depends on your platform, but on a 32-bit system:

  1. The "short" data type in C is usually 16 bits wide.
  2. The "int" datatype in AS3 is 32 bits wide.

Now look at these lines of C:

char *inputBuffer[dwSize];

and

iCurValue+=(signed short)inputBuffer[i];

The first says that we have a stream of bytes (your Delta PCM-encoded waveform).

The second tells the computer to treat these as a stream of 16-bit values. Why? Because your C code probably assumes a 16-bit sample size. Your AS3 code does not do this part (interpreting the byte array as a short array).

If I were doing this, I would change

iCurValue += input[i];

to be something like:

iCurValue += input.readShort();

So you have been reading the bytestream as if it were ints, and thereby, reading garbage (which explains what you're hearing).

Hope that helps!

Rahul Banerjee
  • 2,343
  • 15
  • 16
  • That seems so obvious now. I tried it though (replacing the `for` loop with a `while` loop) but I still get static. Plus, there are an odd number of bytes in `input`, so I get an "end of file" error unless I break out of the loop when there's 1 byte left. I am on a 64-bit system though, would that make a difference? – puggsoy Apr 08 '13 at 15:26