flash10 - flash9.events.SampleDataEvent

This is a simple example of using the SampleDataEvent to create a sound. It also shows how you can dynamically create BitmapData to be used for MovieClip button state. The SampleDataEvent is only available in flash 10.

// author: JLM at justinfront
import flash.display.MovieClip;
import flash.Lib;

import flash.events.MouseEvent;
import flash.events.Event;

import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Point;
import flash.geom.Rectangle;

//sound
import flash.events.SampleDataEvent;
import flash.utils.ByteArray;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.media.SoundMixer;
import flash.media.Sound;


class CreatingSoundBytesAndBitmap extends MovieClip
{
    
    public static var _this:            MovieClip;
    private var snd:                    Sound;
    private var play_mc:                MovieClip;
    private var byteArray:              ByteArray;
    private var sndCopy:                Sound;
    private var play_bitmapData:        BitmapData;
    private var amountOfSound:          Int;
    private var playSkin_bitmap:        BitmapData;
    private var _soundTransform:        SoundTransform;
    public inline static var _red_:     Int             = 0x993333;
    public inline static var _black_:    Int            = 0x2a2a2a;
    
    
    public static function main(): Void { _this = new CreatingSoundBytesAndBitmap(); }
    public function new()
    {
        
        super();

        snd         = new Sound();
        var chanel  = new SoundChannel();
        
        sndCopy     = new Sound();
        byteArray   = new ByteArray();
        
        snd.addEventListener( SampleDataEvent.SAMPLE_DATA , recordSound );
        sndCopy.addEventListener( SampleDataEvent.SAMPLE_DATA , soundReplay );
        
        chanel = snd.play();
        var soundTransform  = chanel.soundTransform;
        soundTransform.volume = 0;
        chanel.soundTransform = soundTransform;
        
        createPlayBtn();   
        
    }
    
    
    private function createPlayBtn()
    {
        
        play_mc = new MovieClip();
        play_mc.x = 50;
        play_mc.y = 50;
        Lib.current.addChild( play_mc );
        
        play_mc.graphics.lineStyle( 0, _black_, 1 );
        play_mc.graphics.beginFill( _black_, 1 );
        play_mc.graphics.lineTo( 18, 0 );
        play_mc.graphics.lineTo( 18, 16 );
        play_mc.graphics.lineTo( 0, 16 );
        play_mc.graphics.lineTo( 0, 0 );
        play_mc.graphics.endFill();
        
        play_mc.graphics.lineStyle( 0, _red_, 1 );
        play_mc.graphics.beginFill( _red_, 1 );
        play_mc.graphics.moveTo( 5, 2 );
        play_mc.graphics.lineTo( 16, 8 );
        play_mc.graphics.lineTo( 5, 14 );
        play_mc.graphics.endFill();
        
        //by copying it to a bitmap we can later swap the graphic out if we wanted to create rollover or other states, 
        // graphics.clear() can be used to clear graphics we create
        play_bitmapData = copyToBitmapWithTransparency( play_mc );
        
        playSkin_bitmap = new BitmapData( 18, 16, true, 0x00000 );
        play_mc.addChild( new Bitmap( playSkin_bitmap ) );
        playSkin_bitmap.draw( play_bitmapData );
        play_mc.graphics.clear();
        play_mc.mouseChildren   = false;
        play_mc.buttonMode      = true;
        
        play_mc.addEventListener( MouseEvent.MOUSE_DOWN, beep );
    }
    
    
    public function beep( e: Event )
    {
        
        byteArray.position = 0;
        sndCopy.play();
        
    }
    
    
    function recordSound( event: SampleDataEvent )
    {
        amountOfSound++;
        for ( c in 0...1234 )
        {
            var sample:Float = Math.sin( ( cast( c + event.position )/Math.PI/2 ) )*0.01;
            event.data.writeFloat( sample );
            event.data.writeFloat( sample );
        }
        
        event.data.position = 0;
        for ( s in 0...1234 )
        {
            
            try
            {
            byteArray.writeFloat( event.data.readFloat() );
            byteArray.writeFloat( event.data.readFloat() );
            } catch ( e: Dynamic )
            {
                trace(s);
            } 
        }
    }
    
    
    function soundReplay( event: SampleDataEvent )
    {
        
        for ( c in 0...1234 )
        {
            try
            {
                event.data.writeFloat( byteArray.readFloat());
                event.data.writeFloat( byteArray.readFloat());
            }
            catch ( e:Dynamic )
            {
                 trace(c);
            }
        }
        
    }
    
    
    public function copyToBitmapWithTransparency( mc: MovieClip ): BitmapData
    {
        
        var wide:       Int             = Std.int( mc.width );
        var hi:         Int             = Std.int( mc.height );
        
        var point:      Point           = new Point( 0, 0 );
        var rect:       Rectangle       = new Rectangle( 0 , 0, wide, hi );
        var abitmap:    BitmapData      = new BitmapData( wide, hi, true, 0x0000000 );
        abitmap.draw( mc );
        abitmap.copyPixels( abitmap, rect, point, abitmap, point, false );
        return abitmap;
    
    }
    
}
-swf createSoundBytesAndBitmap.swf
-swf-version 10
-main CreatingSoundBytesAndBitmap
-swf-header 200:200:30:000000
version #8120, modified 2010-02-08 06:30:25 by JLM