In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

SiON Drawing Sounds

@author Will Costa
@author http://www.williancosta.com/blog
/**
 * Copyright will_costa ( http://wonderfl.net/user/will_costa )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/69L2
 */

package {
    import org.si.sion.SiONDriver;
    import org.si.sion.SiONVoice;
    import org.si.sion.utils.SiONPresetVoice;

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.BlurFilter;
    import flash.geom.Point;    

    /**
     * @author Will Costa
     * @author http://www.williancosta.com/blog
     */
    public class DrawingSounds extends Sprite {
        private var _bitmap : Bitmap;
        private var _bitmapData : BitmapData;
        private var _blur : BlurFilter;
        private var _container : Sprite;
        private var _driver : SiONDriver;
        private var _line : Line;
        private var _voice : SiONVoice;
        private var _point : Point;
        private var _presets : SiONPresetVoice;

        public function DrawingSounds() {
            _blur = new BlurFilter(10, 10, 1);
            _point = new Point(0, 0);
            _container = new Sprite();
            addChild(_container);
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStageHandler);
            initSion();
        }

        //----------------------------------
        //  Private Functions
        //----------------------------------
        private function initSion() : void {
            _driver = new SiONDriver();
            _presets = new SiONPresetVoice();
            _voice = _presets.categolies[2][15];
            _driver.play();
        }

        private function getNote(x : Number,y : Number) : int {
            var _x : Number = x / stage.stageWidth * 30;
            var _y : Number = y / stage.stageHeight * 30;
            var note : int = 30 + _x + _y;
            return note;
        }    

        //----------------------------------
        //  Event Handlers
        //----------------------------------
        private function onAddedToStageHandler(event : Event) : void {
            _bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
            _bitmap = new Bitmap(_bitmapData);
            _container.addChild(_bitmap);
            _container.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
            addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
        }

        private function onMouseDownHandler(event : MouseEvent) : void {
            _line = new Line();
            _container.addChild(_line);
            _driver.noteOn(getNote(mouseX, mouseY), _voice, 3);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
        }

        private function onMouseUpHandler(event : MouseEvent) : void {
            _line.dispose();
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
            stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
        }

        private function onMouseMoveHandler(event : MouseEvent) : void {
            _line.addPoint(mouseX, mouseY);
            _driver.noteOn(getNote(mouseX, mouseY), _voice, .5);
        }    

        private function onEnterFrameHandler(event : Event) : void {
            _bitmapData.draw(_container);
            _bitmapData.applyFilter(_bitmapData, _bitmapData.rect, _point, _blur);
        }
        
    }
}

import flash.display.BlendMode;
import flash.display.Sprite;
import flash.filters.GlowFilter;

internal class Line extends Sprite {
    private var _points : Array = [];
    private var _color : uint;

    public function Line() {
        blendMode = BlendMode.ADD;
        _color = Math.random() * 0xFFFFFF;
        filters = [new GlowFilter(_color, .3, 20, 20, 1)];
    }

    //----------------------------------
    //  Public Functions
    //----------------------------------
    public function addPoint(x : Number,y : Number) : void {
        _points.push({x:x, y:y});
        var p : Object = _points[0];
        graphics.clear();
        graphics.lineStyle(2, _color);
        graphics.moveTo(p.x, p.y);
        var n : int = _points.length;
        for (var i : int = 0;i < n;i++) {
            p = _points[i];
            graphics.lineTo(p.x, p.y);
        }
    }

    public function dispose() : void {
        filters = [];
        _points = [];
        graphics.clear();
        parent.removeChild(this); 
    }
}