Main.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:comp="com.borealkiss.controls.*"
    creationComplete="init(event)"
    backgroundColor="#101010" viewSourceURL="srcview/index.html">
    
    <mx:VBox width="100%" height="100%" >
        <mx:Canvas id="_canvas" 
            width="400"
            height="200"/>
        <comp:FrequencySlider target="{_sound}"/>        
        <mx:Label text="Frequency, kHz"/>        
    </mx:VBox>
    
    <!--
    <mx:Style source="css/Fx049.css" />
    -->
    
    <mx:Script>
        <![CDATA[
            import com.borealkiss.media.GenerativeSineWave;
            
            private const PADDING:Number = 10;
            private var _channel:SoundChannel;
            private var _rectWidth:Number;
            []
            private var _sound:GenerativeSineWave;
            
            private function init(e:Event):void{
                _sound = new GenerativeSineWave();
                _channel = _sound.play();
                addEventListener(Event.ENTER_FRAME,onEnterFrame);
                _rectWidth = (_canvas.width - 2*PADDING)/256;
            }
            
            private function onEnterFrame(e:Event):void{                                
                var ba:ByteArray = new ByteArray();
                SoundMixer.computeSpectrum(ba,true,0);
                
                var g:Graphics = _canvas.graphics;
                g.clear();
                g.beginFill(0x005885);
                
                for (var i:int=0; i<256; i++){
                    var x:Number = PADDING + i*_rectWidth;
                    var y:Number = _canvas.height - PADDING;
                    var rectHeight:Number = -1*ba.readFloat()*100;
                    g.drawRect(x,y,_rectWidth,rectHeight);
                }
                g.endFill();
            }
        ]]>
    </mx:Script>
</mx:Application>

FrequencySlider.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:HSlider xmlns:mx="http://www.adobe.com/2006/mxml"
            width="400"
            buttonMode="true"
            labels="[0,1,2,3,4,5,6,7,8,9,10,11]" 
            minimum="0" 
            maximum="11"
            tickInterval="1"
            value="4"
            change="onChange(event)">    
    
    <mx:Script>
        <![CDATA[
            import com.borealkiss.media.GenerativeSineWave;
            private var _target:GenerativeSineWave;
            
            public function set target(target:GenerativeSineWave):void{
                _target = target;
            }
        
            private function onChange(e:Event):void{
                //The value is in kHz so must be multiplied by 1000.
                _target.frequency = e.target.value*1000;
            }
        ]]>
    </mx:Script>
</mx:HSlider>

GenerativeSineWave.as

package com.borealkiss.media{
    import flash.media.Sound;
    import flash.events.SampleDataEvent;
    import flash.utils.ByteArray;
    
    public class GenerativeSineWave extends Sound{
        private var _frequency:Number;
        private const LENGTH:int = 4410;
        private const SAMPLE_RATE:Number = 44100;
        
        /**
         * Constructor
         */ 
        public function GenerativeSineWave(frequency:Number=4000){
            _frequency = frequency;
            addEventListener(SampleDataEvent.SAMPLE_DATA,onSampleData);
        }
        
        public function get frequency():Number{
            return _frequency;
        }
        
        public function set frequency(value:Number):void{
            _frequency = value;
        }
        
        private function onSampleData(e:SampleDataEvent):void{
            var ba:ByteArray = e.data as ByteArray;
            for (var i:int=0; i<LENGTH; i++){
                var theta:Number = 2*Math.PI*(i+e.position)*_frequency/SAMPLE_RATE;
                ba.writeFloat(Math.sin(theta));
                ba.writeFloat(Math.sin(theta));
            }
        }
    }
}
Powered by blog Boreal Kiss 2008.