views:

74

answers:

1

Hi, I'm looking for some help converting as3 code to pixelbender code in an attempt to improve the performance of my application.

This as3 code goes as follows. I scan through the Number values of a bytearray in chunks. Lets say this chunk lenght was 100 numbers I read 2 numbers (left and right) and try find the maximum values. The numbers in my bytearray are PCM data so there is millions of them and this code can often take a long time to execute, especially on a low spec machine.

The whole aim of this is to render a waveform as quickly as possible. I know very little about pixel bender. I can basically make a new file and create a shaderJob of it in flash but I'm really uncertain how to approach this...

I guess I'm really asking how do I pass pixelbender either

A) a "chunk" of numbers and get it to pass me back 2 maximum values (left and right)

or

B) my entire bytearray and get pixel bender to do the chunking stuff for me

var spritePixelIndex:Number=0;
            var spriteSize:Number;
            spriteSize=_sizes[_numberOfZoomLevels - 1];
            blockSize=Math.floor(_pcmLength / spriteSize);
                chunksize=blockSize * 100;


for (var i:int=0; i < chunksize; ++i)
            {
                if (_pcmData.bytesAvailable)
                {
                    var la:Number=_pcmData.readFloat();
                    var ra:Number=_pcmData.readFloat();
                    var l:Number=la > 0.0 ? la : -la;
                    var r:Number=ra > 0.0 ? ra : -ra;

                    ++_divCount;
                    var ml:Number=0;
                    var mr:Number=0;
                    var a_ml:Number=ml > 0.0 ? ml : -ml;
                    var a_mr:Number=mr > 0.0 ? mr : -mr;

                    ml=a_ml > (l) ? ml : l;
                    mr=a_mr > (r) ? mr : r;
                }
}
+2  A: 

Pixel Bender is not the right tool for this task, since it does not support loops and thus cannot search through an array of data to return you single values. It can also not pass values over to the next processed chunk, which would be nice since then you could just compare the current chunk with the previously found maximum, like in a bucket chain.

Instead I recommend you to have a look at Alchemy and use its fast bytearray opcodes to quickly search through your data. Joa Ebert and Burak Kalayci have both build helper tools that allow you to use these opcodes even without the need to program in C: http://philippe.elsass.me/2010/05/as3-fast-memory-access-without-alchemy/

Quasimondo
I think my main problem was that there was so many calculations going on that the main thread was getting hung up and everything was halting.If somebody could show me pixelbender code to accept 2 numbers and return the biggest I could try calling that instead of doing the calculation in my chunk loop and see if theres any performance performance
dubbeat
also I think that the evaluatePixel method is basically a loop. So I pass the data in as a bytearry or represented as a bitmap it could do the trick?
dubbeat
Again, yes pixel bender runs the kernel over each pixel or Vector entry that you pass it, but each time it runs the code it has no knowledge about what is going on in the other pixels and it has no way to pass information on except for returning a changed pixel. If you use Alchemy that code can run in the background and will not block the execution of your main program.
Quasimondo
The simple way of keeping actionscript from getting blocked is to check getTimer():parseChunks( i:int ){t = getTimer();while ( i < chunksize }
Quasimondo
Yes, I think using Azoth might be easier, Burak has some usage examples on his page, too: http://www.buraks.com/azoth/index.html
Quasimondo
Though - if you do not use Alchemy directly you will still have to write your own non-blocking loop (like the one in the example above)
Quasimondo