开发者

About generating sound waveform in an ActionScript 3 Bitmap

开发者 https://www.devze.com 2023-04-08 22:18 出处:网络
I am generating Bitmap object to show the sound waveform of a loaded sound. The bitmap is 1024x120 and after it has been generated I shrink its size to 655x120. My problem is the player that loads the

I am generating Bitmap object to show the sound waveform of a loaded sound. The bitmap is 1024x120 and after it has been generated I shrink its size to 655x120. My problem is the player that loads the bitmap in task manager becomes 260MB heavy.

I am also adding some gradients and I also cache it as bitmap, but if I remove this properties I dont get any big difference in size.

I also can try to small its size but still I think the bitmap will be big.

Any idea how to compress or whatever solution to solve this problem?

thanks

Here is some code

These are the sound and Bitmap settigns of the function that generate the bitmap.

   public function generateBitmap(snd:Sound):void 
    {
        samples = new ByteArray();
        buffer = new BitmapData(1024, 120, false, backColor);
        screen = new Bitmap(buffer);
        rect = new Rectangle(0, 0, 1, 0);
        var left:Number;
        var right:Number;

        screen.x = 0;
        screen.y = 0;
        screen.width = 655;
        screen.height = 120;

        buffer.fillRect( buffer.rect, backColor );

Now I am doing some samples extraction

        var extract:Number = Math.floor ((snd.length / 1000) * 44100);
        playingTime = snd.length;
        ratio = playingTime / buffer.width;

        var lng:Number = snd.extract(samples, extract);
        samples.position = 0;
        step = samples.length / 4096;

        do 
        {
            step-- ;
        }
        while ( step % 4 );

Follows the drawing method of the bitmap.

           for (var c:int = 0; c < 4096; c++)
        {
            rect.x = c / 4;
            left = samples.readFloat() * 25;
            right = samples.readFloat() * 25;
            samples.position = c * step;

            // left channel 
            if (left > 0)
            {
                rect.y = 30 - left;
                rect.height = left;
            } 
            else
            {
                rect.y = 30;
                rect.height = -left;
            }
            buffer.fillRect( rect, leftChColor );

            // right channel 
            if (right > 0)
            {
                rect.y = 80 - right;
                rect.height = right;
            } 
            else
            {
                rect.y = 80;
                rect.height = -right;
            }
            buffer.fillRect( rect, rightChColor );
        }
        screen.width = screenWidth;
        screen.height = screenHeight;
        addChild( screen );
        }

Here is a reference of the code I have used. 开发者_运维技巧Tried it without my stuff in the player and get 193MB of RAM just for the flash player. So the code needs I guess the code needs refinement. Any idea or othere method to do the same stuff without eating so much RAM?


Before this line:

buffer = new BitmapData(1024, 120, false, backColor);

Add:

if ( buffer ) { buffer.dispose(); buffer = null; }
buffer = new BitmapData(1024, 120, false, backColor);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号