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

The Sounds of Math Music III

AS3 8bit sounds engine by Saqoosha built after following video
"Experimental music from very short C programs"
http://www.youtube.com/watch?v=GtQdIYUtAHg
and Kyle's visualization.
http://www.flickr.com/photos/kylemcdonald/6187863454/in/photostream/

@quasimondo: New Sounds
/**
 * Copyright Quasimondo ( http://wonderfl.net/user/Quasimondo )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/j5Ec
 */

// forked from Quasimondo's The Sounds of Math Music II
// forked from Quasimondo's Math Music - New Formulas
// forked from Saqoosha's Math Music
package {

    import com.bit101.components.Component;
    import com.bit101.components.PushButton;
    import com.bit101.components.VBox;

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.SampleDataEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;


    /**
     * AS3 8bit sounds engine by Saqoosha built after following video
     * "Experimental music from very short C programs"
     * http://www.youtube.com/watch?v=GtQdIYUtAHg
     * and Kyle's visualization.
     * http://www.flickr.com/photos/kylemcdonald/6187863454/in/photostream/
     *
     @quasimondo: New Sounds
     */
    [SWF(backgroundColor="0x000000", frameRate="30", width="475", height="475")]
    public class MathMusic extends Sprite {
        
        
        private static const FORMULAS:Array = [
            'Feinstoff',
            'Dig_Dug',
            'Dark_Passenger'
         ];


        private var _sound:Sound;
        private var _channel:SoundChannel;
        private var _t:Number = -1;
        private var _type:int = -1;
        private var _image:BitmapData;
        private var _px:int = 0;
        private var _py:int = 0;
        private var _control:Component;
        private var _buttons:Vector.<PushButton> = new Vector.<PushButton>();


        public function MathMusic() {
            _image = new BitmapData(256, 256, false, 0x0);
            var b:Bitmap = addChild(new Bitmap(_image)) as Bitmap;
            b.width = b.height = 475;
            
            var vbox:VBox = new VBox(this, 5, 5);
            for each (var f:String in FORMULAS) {
                var btn:PushButton = new PushButton(vbox, 0, 0, f.replace(/ +/g, '').toUpperCase(), _onClick);
                btn.toggle = true;
                btn.width = 232;
                _buttons.push(btn);
            }
            _control = vbox;
            _control.scaleX = _control.scaleY = 2;
            
            _sound = new Sound();
            _sound.addEventListener(SampleDataEvent.SAMPLE_DATA, _onSampleData);
            
            stage.addEventListener(MouseEvent.MOUSE_MOVE, function(e:*):void { _control.visible = true; });
            stage.addEventListener(Event.MOUSE_LEAVE, function(e:*):void { _control.visible = false; });
        }
        
        
        private function _play():void {
            for (var i:int = 0, n:int = _buttons.length; i < n; i++) {
                _buttons[i].selected = i == _type;
            }
            _t = 0;
            _px = 0;
            _py = 0;
            _image.fillRect(_image.rect, 0x0);
            if (_channel) _channel.stop();
            _channel = _sound.play();
        }


        private function _onClick(e:Event):void {
            var type:int = _buttons.indexOf(e.target);
            if (_type != type) {
                _type = type;
                _play();
            }
        }


        private var ot:int = -1;
        
        private function _onSampleData(e:SampleDataEvent):void {
            var i:int, t:int, tt:int, val:Number;
            _image.lock();
            for (i = 0; i < 8192; i++, _t++) {
                t = (_t * 8 / 44.1);
                switch (_type) {
                    case 0: tt = (((~4007607)+4007607)>=t?(((((((t>>12)&((t^(((t/(t>>9))&((t>>22)<<(6334354&0xf)))-((t>>21)-(~(699688|(t>>4))))))|(15863222>>(t&0xf))))<<(10757314&0xf))*(~(t>>14)))^(t|(5867936*(((~((t>>13)^2502780))+((t>>13)^2502780))|(~t)))))&(t>>3))>>(8516425&0xf)):(7085686%(t&((t&t)<<(7797626&0xf))))); break;
                    case 1: tt = (t*(((~((~(t>>2))|(t>>12)))<=2730446?t:(t>>9))&((((t>>9)|((t>>8)&13673646))|((t>>8)&13673646))^t)));break;
                    case 2: tt = ((((t>>12084395)-((t>>11)<<((((~(t>>9492815))%((t>>1370814640950)|t))%((t>>7185086)|t))&13401875)))+((t>>14655948)|t))^6999175);break;
                            }
                tt &= 0xff;
                val = tt / 0xff;
                e.data.writeFloat(val);
                e.data.writeFloat(val);
                if (t != ot) {
                    _image.setPixel(_px, _py, tt << 16 | tt << 8 | tt);
                    if (++_px == _image.width) {
                        _px = 0;
                        if (++_py == _image.height) {
                            _py = 0;
                        }
                    }
                }
                ot = t;
            }
            _image.unlock();
        }
    }
}