1

I'm trying to do something simple:

send a byteArray to a C library, fill up a buffer and read the memory, but I think I have float conversion problems.

Here is what I do in AS:

var memory:ByteArray = gstate.ds;
// get sound
var soundBytes:ByteArray = new ByteArray();
_source.extract(soundBytes, 4096);
soundBytes.position = 0;
// send sound
_lib.processSound(soundBytes);
// get sound back to play it
memory.position = _dataPosition
event.data.writeBytes(memory);

The C code:

My buffer variable:

float *buffer;

The buffer is intialize at the beginning:

static AS3_Val initByteArray(void* self, AS3_Val args) {
    AS3_ArrayValue(args, "IntType", &bufferSize);
    buffer = (float*)malloc(bufferSize*sizeof(float));
    return AS3_Int((int)buffer);
}

And I process the sound:

AS3_Val src = AS3_Undefined();
AS3_ArrayValue( args,"AS3ValType", &src );
// get length
AS3_Val lengthOfArgs = AS3_GetS(src,"length");
int len = AS3_IntValue(lengthOfArgs);
// set position to 0 and write the buffer
AS3_ByteArray_seek(src, 0, SEEK_SET);
AS3_Trace(AS3_GetS(src, "bytesAvailable"));
AS3_ByteArray_readBytes(buffer, src, len);

Now I get the error:

Exception fault: RangeError: Error #2004: One of the parameters is invalid. at flash.media::Sound/play()

The memory seems to be increased correctly when I write in the buffer, 32768 for 4096 * 8 (4 for float times 2 for left and right channel).

When I trace the bytes in flash before sending the byteArray, I get some numbers like this (at a specific position of 1421 to compare):

soundBytes.position = 1421 * 8;
trace(soundBytes.readFloat()); // trace -0.000030517578125 (left)
trace(soundBytes.readFloat()); // trace 0.000030517578125 (right)

I can trace the same number in C:

AS3_ByteArray_seek(src, 1421*8, SEEK_SET);
AS3_Val emptyParams = AS3_Array("");
AS3_Val readFloat = AS3_GetS(src, "readFloat");
AS3_Trace(readFloat);
AS3_Val left = AS3_Call(readFloat, src, emptyParams);
AS3_Trace(left); // trace -0.000030517578125 (left)
AS3_Val right = AS3_Call(readFloat, src, emptyParams);
AS3_Trace(right); // trace 0.000030517578125 (right)

But when I trace the buffer:

AS3_Trace(AS3_Number(buffer[1421*2])); // trace 2.5783891743576634e-43
AS3_Trace(AS3_Number(buffer[(1421*2)+1])); trace 7.847271400218976e-44

And in flash after calling the lib:

soundBytes.position = 1421 * 8;
trace(soundBytes.readFloat()); // trace 2.5783891743576634e-43
trace(soundBytes.readFloat()); // trace 7.847271400218976e-44

I looks like the number has been converted or has a different type/format:

-0.000030517578125 vs 2.5783891743576634e-43

I'm not that fluent in C, any idea of what's happening?

Thanks for reading.

Romu

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Soundstep
  • 618
  • 2
  • 6
  • 11
  • I believe you're not passing the data between alchemy and flash correctly. I wish I had more time right now to post more code but take a look through this source and see how they're doing it: http://code.google.com/p/flash-kikko/source/browse/#svn%2Ftrunk%2FshineMP3_alchemy%2Fsrc%2Ffr%2Fkikko%2Flab –  Apr 06 '11 at 16:47
  • Here is the answer (endian was the problem): http://forums.adobe.com/message/3597977#3597977 – Soundstep Apr 06 '11 at 18:26
  • Heh, yeah that would be a problem to have all your bytes backwards :-) – Adam Smith Apr 06 '11 at 21:28
  • Also see: http://stackoverflow.com/a/10231060/89218 – paleozogt Jun 05 '12 at 15:39

0 Answers0